mirror of
https://github.com/photonstorm/phaser
synced 2025-02-25 03:47:24 +00:00
commit
87c3215216
2 changed files with 20 additions and 43 deletions
|
@ -38,7 +38,7 @@ var GetCircleToCircle = function (circleA, circleB, out)
|
||||||
|
|
||||||
if (y0 === y1)
|
if (y0 === y1)
|
||||||
{
|
{
|
||||||
x = (r1 * r1) - (r0 * r0) - (x1 * x1) + (x0 * x0);
|
x = ((r1 * r1) - (r0 * r0) - (x1 * x1) + (x0 * x0)) / (2 * (x0 - x1));
|
||||||
|
|
||||||
coefficientA = 1;
|
coefficientA = 1;
|
||||||
coefficientB = -2 * y1;
|
coefficientB = -2 * y1;
|
||||||
|
|
|
@ -37,66 +37,43 @@ var GetLineToCircle = function (line, circle, out)
|
||||||
var cy = circle.y;
|
var cy = circle.y;
|
||||||
var cr = circle.radius;
|
var cr = circle.radius;
|
||||||
|
|
||||||
// We determine the line equation
|
var lDirX = lx2 - lx1;
|
||||||
var leadingCoefficient, originOrdinate;
|
var lDirY = ly2 - ly1;
|
||||||
|
var oDirX = lx1 - cx;
|
||||||
|
var oDirY = ly1 - cy;
|
||||||
|
|
||||||
if (lx1 === lx2)
|
var coefficientA = lDirX * lDirX + lDirY * lDirY;
|
||||||
{
|
var coefficientB = 2 * (lDirX * oDirX + lDirY * oDirY);
|
||||||
// Linear function
|
var coefficientC = oDirX * oDirX + oDirY * oDirY - cr * cr;
|
||||||
leadingCoefficient = lx1;
|
|
||||||
originOrdinate = 0;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Constant function
|
|
||||||
leadingCoefficient = (ly2 - ly1) / (lx2 - lx1);
|
|
||||||
originOrdinate = ly1 - leadingCoefficient * lx1;
|
|
||||||
}
|
|
||||||
|
|
||||||
var coefficientA = (leadingCoefficient * leadingCoefficient) + 1;
|
|
||||||
var coefficientB = (-2 * cx) - (2 * cy * leadingCoefficient) + (2 * leadingCoefficient * originOrdinate);
|
|
||||||
var coefficientC = (cx * cx) + (cy * cy) + (originOrdinate * originOrdinate) - (cr * cr) - (2 * cy * originOrdinate);
|
|
||||||
|
|
||||||
var lambda = (coefficientB * coefficientB) - (4 * coefficientA * coefficientC);
|
var lambda = (coefficientB * coefficientB) - (4 * coefficientA * coefficientC);
|
||||||
|
|
||||||
var x, y;
|
var x, y;
|
||||||
|
|
||||||
var xMin = Math.min(lx1, lx2);
|
|
||||||
var yMin = Math.min(ly1, ly2);
|
|
||||||
|
|
||||||
var xMax = Math.max(lx1, lx2);
|
|
||||||
var yMax = Math.max(ly1, ly2);
|
|
||||||
|
|
||||||
if (lambda === 0)
|
if (lambda === 0)
|
||||||
{
|
{
|
||||||
x = (-coefficientB / (2 * coefficientA));
|
var root = -coefficientB / (2 * coefficientA);
|
||||||
y = ((leadingCoefficient * x) + originOrdinate);
|
x = lx1 + root * lDirX;
|
||||||
if (Math.min(xMin, x) === xMin &&
|
y = ly1 + root * lDirY;
|
||||||
Math.min(yMin, y) === yMin &&
|
if (root >= 0 && root <= 1)
|
||||||
Math.max(xMax, x) === xMax &&
|
|
||||||
Math.max(yMax, y) === yMax)
|
|
||||||
{
|
{
|
||||||
out.push(new Point(x, y));
|
out.push(new Point(x, y));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (lambda > 0)
|
else if (lambda > 0)
|
||||||
{
|
{
|
||||||
x = (-coefficientB + Math.sqrt(lambda)) / (2 * coefficientA);
|
var root1 = (-coefficientB - Math.sqrt(lambda)) / (2 * coefficientA);
|
||||||
y = ((leadingCoefficient * x) + originOrdinate);
|
x = lx1 + root1 * lDirX;
|
||||||
if (Math.min(xMin, x) === xMin &&
|
y = ly1 + root1 * lDirY;
|
||||||
Math.min(yMin, y) === yMin &&
|
if (root1 >= 0 && root1 <= 1)
|
||||||
Math.max(xMax, x) === xMax &&
|
|
||||||
Math.max(yMax, y) === yMax)
|
|
||||||
{
|
{
|
||||||
out.push(new Point(x, y));
|
out.push(new Point(x, y));
|
||||||
}
|
}
|
||||||
|
|
||||||
x = (-coefficientB - Math.sqrt(lambda)) / (2 * coefficientA);
|
var root2 = (-coefficientB + Math.sqrt(lambda)) / (2 * coefficientA);
|
||||||
y = ((leadingCoefficient * x) + originOrdinate);
|
x = lx1 + root2 * lDirX;
|
||||||
if (Math.min(xMin, x) === xMin &&
|
y = ly1 + root2 * lDirY;
|
||||||
Math.min(yMin, y) === yMin &&
|
if (root2 >= 0 && root2 <= 1)
|
||||||
Math.max(xMax, x) === xMax &&
|
|
||||||
Math.max(yMax, y) === yMax)
|
|
||||||
{
|
{
|
||||||
out.push(new Point(x, y));
|
out.push(new Point(x, y));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue