-
Notifications
You must be signed in to change notification settings - Fork 48
/
ellipse-line.js
40 lines (39 loc) · 1.16 KB
/
ellipse-line.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
/**
* ellipse-line collision
* adapted from http://csharphelper.com/blog/2017/08/calculate-where-a-line-segment-and-an-ellipse-intersect-in-c/
* @param {number} xe center of ellipse
* @param {number} ye center of ellipse
* @param {number} rex radius-x of ellipse
* @param {number} rey radius-y of ellipse
* @param {number} x1 first point of line
* @param {number} y1 first point of line
* @param {number} x2 second point of line
* @param {number} y2 second point of line
*/
module.exports = function ellipseLine(xe, ye, rex, rey, x1, y1, x2, y2)
{
x1 -= xe
x2 -= xe
y1 -= ye
y2 -= ye
var A = Math.pow(x2 - x1, 2) / rex / rex + Math.pow(y2 - y1, 2) / rey / rey
var B = 2 * x1 * (x2 - x1) / rex / rex + 2 * y1 * (y2 - y1) / rey / rey
var C = x1 * x1 / rex / rex + y1 * y1 / rey / rey - 1
var D = B * B - 4 * A * C
if (D === 0)
{
var t = -B / 2 / A
return t >= 0 && t <= 1
}
else if (D > 0)
{
var sqrt = Math.sqrt(D)
var t1 = (-B + sqrt) / 2 / A
var t2 = (-B - sqrt) / 2 / A
return (t1 >= 0 && t1 <= 1) || (t2 >= 0 && t2 <= 1)
}
else
{
return false
}
}