-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathellipse-helper.js
144 lines (135 loc) · 4.56 KB
/
ellipse-helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// from http://yehar.com/blog/?p=2926
var MAX_ITERATIONS = 10
var innerPolygonCoef, outerPolygonCoef, initialized
function initialize()
{
innerPolygonCoef = []
outerPolygonCoef = []
for (var t = 0; t <= MAX_ITERATIONS; t++)
{
var numNodes = 4 << t
innerPolygonCoef[t] = 0.5 / Math.cos(4 * Math.acos(0) / numNodes)
outerPolygonCoef[t] = 0.5 / (Math.cos(2 * Math.acos(0) / numNodes) * Math.cos(2 * Math.acos(0) / numNodes))
}
initialized = true
}
function iterate(x, y, c0x, c0y, c2x, c2y, rr)
{
for (var t = 1; t <= MAX_ITERATIONS; t++)
{
var c1x = (c0x + c2x) * innerPolygonCoef[t]
var c1y = (c0y + c2y) * innerPolygonCoef[t]
var tx = x - c1x
var ty = y - c1y
if (tx * tx + ty * ty <= rr)
{
return true
}
var t2x = c2x - c1x
var t2y = c2y - c1y
if (tx * t2x + ty * t2y >= 0 && tx * t2x + ty * t2y <= t2x * t2x + t2y * t2y &&
(ty * t2x - tx * t2y >= 0 || rr * (t2x * t2x + t2y * t2y) >= (ty * t2x - tx * t2y) * (ty * t2x - tx * t2y)))
{
return true
}
var t0x = c0x - c1x
var t0y = c0y - c1y
if (tx * t0x + ty * t0y >= 0 && tx * t0x + ty * t0y <= t0x * t0x + t0y * t0y &&
(ty * t0x - tx * t0y <= 0 || rr * (t0x * t0x + t0y * t0y) >= (ty * t0x - tx * t0y) * (ty * t0x - tx * t0y)))
{
return true
}
var c3x = (c0x + c1x) * outerPolygonCoef[t]
var c3y = (c0y + c1y) * outerPolygonCoef[t]
if ((c3x - x) * (c3x - x) + (c3y - y) * (c3y - y) < rr)
{
c2x = c1x
c2y = c1y
continue
}
var c4x = c1x - c3x + c1x
var c4y = c1y - c3y + c1y
if ((c4x - x) * (c4x - x) + (c4y - y) * (c4y - y) < rr)
{
c0x = c1x
c0y = c1y
continue
}
var t3x = c3x - c1x
var t3y = c3y - c1y
if (ty * t3x - tx * t3y <= 0 || rr * (t3x * t3x + t3y * t3y) > (ty * t3x - tx * t3y) * (ty * t3x - tx * t3y))
{
if (tx * t3x + ty * t3y > 0)
{
if (Math.abs(tx * t3x + ty * t3y) <= t3x * t3x + t3y * t3y || (x - c3x) * (c0x - c3x) + (y - c3y) * (c0y - c3y) >= 0)
{
c2x = c1x
c2y = c1y
continue
}
} else if (-(tx * t3x + ty * t3y) <= t3x * t3x + t3y * t3y || (x - c4x) * (c2x - c4x) + (y - c4y) * (c2y - c4y) >= 0)
{
c0x = c1x
c0y = c1y
continue
}
}
return false
}
return false // Out of iterations so it is unsure if there was a collision. But have to return something.
}
// Test for collision between an ellipse of horizontal radius w0 and vertical radius h0 at (x0, y0) and
// an ellipse of horizontal radius w1 and vertical radius h1 at (x1, y1)
function ellipseEllipse(x0, y0, w0, h0, x1, y1, w1, h1)
{
if (!initialized)
{
initialize()
}
var x = Math.abs(x1 - x0) * h1
var y = Math.abs(y1 - y0) * w1
w0 *= h1
h0 *= w1
var r = w1 * h1
if (x * x + (h0 - y) * (h0 - y) <= r * r || (w0 - x) * (w0 - x) + y * y <= r * r || x * h0 + y * w0 <= w0 * h0
|| ((x * h0 + y * w0 - w0 * h0) * (x * h0 + y * w0 - w0 * h0) <= r * r * (w0 * w0 + h0 * h0) && x * w0 - y * h0 >= -h0 * h0 && x * w0 - y * h0 <= w0 * w0))
{
return true
}
else
{
if ((x - w0) * (x - w0) + (y - h0) * (y - h0) <= r * r || (x <= w0 && y - r <= h0) || (y <= h0 && x - r <= w0))
{
return iterate(x, y, w0, 0, 0, h0, r * r)
}
return false
}
}
// Test for collision between an ellipse of horizontal radius w and vertical radius h at (x0, y0) and
// a circle of radius r at (x1, y1)
function ellipseCircle(x0, y0, w, h, x1, y1, r)
{
if (!initialized)
{
initialize()
}
var x = Math.abs(x1 - x0)
var y = Math.abs(y1 - y0)
if (x * x + (h - y) * (h - y) <= r * r || (w - x) * (w - x) + y * y <= r * r || x * h + y * w <= w * h
|| ((x * h + y * w - w * h) * (x * h + y * w - w * h) <= r * r * (w * w + h * h) && x * w - y * h >= -h * h && x * w - y * h <= w * w))
{
return true
}
else
{
if ((x - w) * (x - w) + (y - h) * (y - h) <= r * r || (x <= w && y - r <= h) || (y <= h && x - r <= w))
{
return iterate(x, y, w, 0, 0, h, r * r)
}
return false
}
}
module.exports = {
ellipseCircle: ellipseCircle,
ellipseEllipse: ellipseEllipse
}