Merge pull request #4384 from florianvazelle/master

Add out argument in intersect functions
This commit is contained in:
Richard Davey 2019-04-09 17:51:04 +01:00 committed by GitHub
commit 6622f4dbce
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 530 additions and 0 deletions

View file

@ -0,0 +1,88 @@
/**
* @author Florian Vazelle
* @author Geoffrey Glaive
* @copyright 2019 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var Point = require('../point/Point');
var CircleToCircle = require('./CircleToCircle');
/**
* Checks if two Circles intersect and returns the intersection points as a Point object array.
*
* @function Phaser.Geom.Intersects.GetCircleToCircle
* @since 3.0.0
*
* @param {Phaser.Geom.Circle} circleA - The first Circle to check for intersection.
* @param {Phaser.Geom.Circle} circleB - The second Circle to check for intersection.
* @param {array} [out] - An optional array in which to store the points of intersection.
*
* @return {array} An array with the points of intersection if objects intersect, otherwise an empty array.
*/
var GetCircleToCircle = function (circleA, circleB, out)
{
if (out === undefined) { out = []; }
if (CircleToCircle(circleA, circleB))
{
var x0 = circleA.x;
var y0 = circleA.y;
var r0 = circleA.radius;
var x1 = circleB.x;
var y1 = circleB.y;
var r1 = circleB.radius;
var coefficientA, coefficientB, coefficientC, lambda, x;
if (y0 === y1)
{
x = (r1 * r1) - (r0 * r0) - (x1 * x1) + (x0 * x0);
coefficientA = 1;
coefficientB = -2 * y1;
coefficientC = (x1 * x1) + (x * x) - (2 * x1 * x) + (y1 * y1) - (r1 * r1);
lambda = (coefficientB * coefficientB) - (4 * coefficientA * coefficientC);
if (lambda === 0)
{
out.push(new Point(x, (-coefficientB / (2 * coefficientA))));
}
else if (lambda > 0)
{
out.push(new Point(x, (-coefficientB + Math.sqrt(lambda)) / (2 * coefficientA)));
out.push(new Point(x, (-coefficientB - Math.sqrt(lambda)) / (2 * coefficientA)));
}
}
else
{
var v1 = (x0 - x1) / (y0 - y1);
var n = (r1 * r1 - r0 * r0 - x1 * x1 + x0 * x0 - y1 * y1 + y0 * y0) / (2 * (y0 - y1));
coefficientA = (v1 * v1) + 1;
coefficientB = (2 * y0 * v1) - (2 * n * v1) - (2 * x0);
coefficientC = (x0 * x0) + (y0 * y0) + (n * n) - (r0 * r0) - (2 * y0 * n);
lambda = (coefficientB * coefficientB) - (4 * coefficientA * coefficientC);
if (lambda === 0)
{
x = (-coefficientB / (2 * coefficientA));
out.push(new Point(x, (n - (x * v1))));
}
else if (lambda > 0)
{
x = (-coefficientB + Math.sqrt(lambda)) / (2 * coefficientA);
out.push(new Point(x, (n - (x * v1))));
x = (-coefficientB - Math.sqrt(lambda)) / (2 * coefficientA);
out.push(new Point(x, (n - (x * v1))));
}
}
}
return out;
};
module.exports = GetCircleToCircle;

View file

@ -0,0 +1,44 @@
/**
* @author Florian Vazelle
* @author Geoffrey Glaive
* @copyright 2019 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var GetLineToCircle = require('./GetLineToCircle');
var CircleToRectangle = require('./CircleToRectangle');
/**
* Checks for intersection between a circle and a rectangle,
* and returns the intersection points as a Point object array.
*
* @function Phaser.Geom.Intersects.GetCircleToRectangle
* @since 3.0.0
*
* @param {Phaser.Geom.Circle} circle - The circle to be checked.
* @param {Phaser.Geom.Rectangle} rect - The rectangle to be checked.
* @param {array} [out] - An optional array in which to store the points of intersection.
*
* @return {array} An array with the points of intersection if objects intersect, otherwise an empty array.
*/
var GetCircleToRectangle = function (circle, rect, out)
{
if (out === undefined) { out = []; }
if (CircleToRectangle(circle, rect))
{
var lineA = rect.getLineA();
var lineB = rect.getLineB();
var lineC = rect.getLineC();
var lineD = rect.getLineD();
GetLineToCircle(lineA, circle, out);
GetLineToCircle(lineB, circle, out);
GetLineToCircle(lineC, circle, out);
GetLineToCircle(lineD, circle, out);
}
return out;
};
module.exports = GetCircleToRectangle;

View file

@ -0,0 +1,109 @@
/**
* @author Florian Vazelle
* @author Geoffrey Glaive
* @copyright 2019 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var Point = require('../point/Point');
var LineToCircle = require('./LineToCircle');
/**
* Checks for intersection between the line segment and circle,
* and returns the intersection points as a Point object array.
*
* @function Phaser.Geom.Intersects.GetLineToCircle
* @since 3.0.0
*
* @param {Phaser.Geom.Line} line - The line segment to check.
* @param {Phaser.Geom.Circle} circle - The circle to check against the line.
* @param {array} [out] - An optional array in which to store the points of intersection.
*
* @return {array} An array with the points of intersection if objects intersect, otherwise an empty array.
*/
var GetLineToCircle = function (line, circle, out)
{
if (out === undefined) { out = []; }
if (LineToCircle(line, circle))
{
var lx1 = line.x1;
var ly1 = line.y1;
var lx2 = line.x2;
var ly2 = line.y2;
var cx = circle.x;
var cy = circle.y;
var cr = circle.radius;
// We determine the line equation
var leadingCoefficient, originOrdinate;
if (lx1 === lx2)
{
// Linear function
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 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)
{
x = (-coefficientB / (2 * coefficientA));
y = ((leadingCoefficient * x) + originOrdinate);
if (Math.min(xMin, x) === xMin &&
Math.min(yMin, y) === yMin &&
Math.max(xMax, x) === xMax &&
Math.max(yMax, y) === yMax)
{
out.push(new Point(x, y));
}
}
else if (lambda > 0)
{
x = (-coefficientB + Math.sqrt(lambda)) / (2 * coefficientA);
y = ((leadingCoefficient * x) + originOrdinate);
if (Math.min(xMin, x) === xMin &&
Math.min(yMin, y) === yMin &&
Math.max(xMax, x) === xMax &&
Math.max(yMax, y) === yMax)
{
out.push(new Point(x, y));
}
x = (-coefficientB - Math.sqrt(lambda)) / (2 * coefficientA);
y = ((leadingCoefficient * x) + originOrdinate);
if (Math.min(xMin, x) === xMin &&
Math.min(yMin, y) === yMin &&
Math.max(xMax, x) === xMax &&
Math.max(yMax, y) === yMax)
{
out.push(new Point(x, y));
}
}
}
return out;
};
module.exports = GetLineToCircle;

View file

@ -0,0 +1,54 @@
/**
* @author Florian Vazelle
* @author Geoffrey Glaive
* @copyright 2019 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var Point = require('../point/Point');
var LineToLine = require('./LineToLine');
var LineToRectangle = require('./LineToRectangle');
/**
* Checks for intersection between the Line and a Rectangle shape,
* and returns the intersection points as a Point object array.
*
* @function Phaser.Geom.Intersects.GetLineToRectangle
* @since 3.0.0
*
* @param {Phaser.Geom.Line} line - The Line to check for intersection.
* @param {(Phaser.Geom.Rectangle|object)} rect - The Rectangle to check for intersection.
* @param {array} [out] - An optional array in which to store the points of intersection.
*
* @return {array} An array with the points of intersection if objects intersect, otherwise an empty array.
*/
var GetLineToRectangle = function (line, rect, out)
{
if (out === undefined) { out = []; }
if (LineToRectangle(line, rect))
{
var lineA = rect.getLineA();
var lineB = rect.getLineB();
var lineC = rect.getLineC();
var lineD = rect.getLineD();
var output = [ new Point(), new Point(), new Point(), new Point() ];
var result = [
LineToLine(lineA, line, output[0]),
LineToLine(lineB, line, output[1]),
LineToLine(lineC, line, output[2]),
LineToLine(lineD, line, output[3])
];
for (var i = 0; i < 4; i++)
{
if (result[i]) { out.push(output[i]); }
}
}
return out;
};
module.exports = GetLineToRectangle;

View file

@ -0,0 +1,45 @@
/**
* @author Florian Vazelle
* @author Geoffrey Glaive
* @copyright 2019 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var GetLineToRectangle = require('./GetLineToRectangle');
var RectangleToRectangle = require('./RectangleToRectangle');
/**
* Checks if two Rectangles intersect and returns the intersection points as a Point object array.
*
* A Rectangle intersects another Rectangle if any part of its bounds is within the other Rectangle's bounds. As such, the two Rectangles are considered "solid". A Rectangle with no width or no height will never intersect another Rectangle.
*
* @function Phaser.Geom.Intersects.GetRectangleToRectangle
* @since 3.0.0
*
* @param {Phaser.Geom.Rectangle} rectA - The first Rectangle to check for intersection.
* @param {Phaser.Geom.Rectangle} rectB - The second Rectangle to check for intersection.
* @param {array} [out] - An optional array in which to store the points of intersection.
*
* @return {array} An array with the points of intersection if objects intersect, otherwise an empty array.
*/
var GetRectangleToRectangle = function (rectA, rectB, out)
{
if (out === undefined) { out = []; }
if (RectangleToRectangle(rectA, rectB))
{
var lineA = rectA.getLineA();
var lineB = rectA.getLineB();
var lineC = rectA.getLineC();
var lineD = rectA.getLineD();
GetLineToRectangle(lineA, rectB, out);
GetLineToRectangle(lineB, rectB, out);
GetLineToRectangle(lineC, rectB, out);
GetLineToRectangle(lineD, rectB, out);
}
return out;
};
module.exports = GetRectangleToRectangle;

View file

@ -0,0 +1,42 @@
/**
* @author Florian Vazelle
* @author Geoffrey Glaive
* @copyright 2019 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var RectangleToTriangle = require('./RectangleToTriangle');
var GetLineToRectangle = require('./GetLineToRectangle');
/**
* Checks for intersection between Rectangle shape and Triangle shape,
* and returns the intersection points as a Point object array.
*
* @function Phaser.Geom.Intersects.GetRectangleToTriangle
* @since 3.0.0
*
* @param {Phaser.Geom.Rectangle} rect - Rectangle object to test.
* @param {Phaser.Geom.Triangle} triangle - Triangle object to test.
* @param {array} [out] - An optional array in which to store the points of intersection.
*
* @return {array} An array with the points of intersection if objects intersect, otherwise an empty array.
*/
var GetRectangleToTriangle = function (rect, triangle, out)
{
if (out === undefined) { out = []; }
if (RectangleToTriangle(rect, triangle))
{
var lineA = triangle.getLineA();
var lineB = triangle.getLineB();
var lineC = triangle.getLineC();
GetLineToRectangle(lineA, rect, out);
GetLineToRectangle(lineB, rect, out);
GetLineToRectangle(lineC, rect, out);
}
return out;
};
module.exports = GetRectangleToTriangle;

View file

@ -0,0 +1,43 @@
/**
* @author Florian Vazelle
* @author Geoffrey Glaive
* @copyright 2019 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var GetLineToCircle = require('./GetLineToCircle');
var TriangleToCircle = require('./TriangleToCircle');
/**
* Checks if a Triangle and a Circle intersect, and returns the intersection points as a Point object array.
*
* A Circle intersects a Triangle if its center is located within it or if any of the Triangle's sides intersect the Circle. As such, the Triangle and the Circle are considered "solid" for the intersection.
*
* @function Phaser.Geom.Intersects.GetTriangleToCircle
* @since 3.0.0
*
* @param {Phaser.Geom.Triangle} triangle - The Triangle to check for intersection.
* @param {Phaser.Geom.Circle} circle - The Circle to check for intersection.
* @param {array} [out] - An optional array in which to store the points of intersection.
*
* @return {array} An array with the points of intersection if objects intersect, otherwise an empty array.
*/
var GetTriangleToCircle = function (triangle, circle, out)
{
if (out === undefined) { out = []; }
if (TriangleToCircle(triangle, circle))
{
var lineA = triangle.getLineA();
var lineB = triangle.getLineB();
var lineC = triangle.getLineC();
GetLineToCircle(lineA, circle, out);
GetLineToCircle(lineB, circle, out);
GetLineToCircle(lineC, circle, out);
}
return out;
};
module.exports = GetTriangleToCircle;

View file

@ -0,0 +1,53 @@
/**
* @author Florian Vazelle
* @author Geoffrey Glaive
* @copyright 2019 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var Point = require('../point/Point');
var TriangleToLine = require('./TriangleToLine');
var LineToLine = require('./LineToLine');
/**
* Checks if a Triangle and a Line intersect, and returns the intersection points as a Point object array.
*
* The Line intersects the Triangle if it starts inside of it, ends inside of it, or crosses any of the Triangle's sides. Thus, the Triangle is considered "solid".
*
* @function Phaser.Geom.Intersects.GetTriangleToLine
* @since 3.0.0
*
* @param {Phaser.Geom.Triangle} triangle - The Triangle to check with.
* @param {Phaser.Geom.Line} line - The Line to check with.
* @param {array} [out] - An optional array in which to store the points of intersection.
*
* @return {array} An array with the points of intersection if objects intersect, otherwise an empty array.
*/
var GetTriangleToLine = function (triangle, line, out)
{
if (out === undefined) { out = []; }
if (TriangleToLine(triangle, line))
{
var lineA = triangle.getLineA();
var lineB = triangle.getLineB();
var lineC = triangle.getLineC();
var output = [ new Point(), new Point(), new Point() ];
var result = [
LineToLine(lineA, line, output[0]),
LineToLine(lineB, line, output[1]),
LineToLine(lineC, line, output[2])
];
for (var i = 0; i < 3; i++)
{
if (result[i]) { out.push(output[i]); }
}
}
return out;
};
module.exports = GetTriangleToLine;

View file

@ -0,0 +1,43 @@
/**
* @author Florian Vazelle
* @author Geoffrey Glaive
* @copyright 2019 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var TriangleToTriangle = require('./TriangleToTriangle');
var GetTriangleToLine = require('./GetTriangleToLine');
/**
* Checks if two Triangles intersect, and returns the intersection points as a Point object array.
*
* A Triangle intersects another Triangle if any pair of their lines intersects or if any point of one Triangle is within the other Triangle. Thus, the Triangles are considered "solid".
*
* @function Phaser.Geom.Intersects.GetTriangleToTriangle
* @since 3.0.0
*
* @param {Phaser.Geom.Triangle} triangleA - The first Triangle to check for intersection.
* @param {Phaser.Geom.Triangle} triangleB - The second Triangle to check for intersection.
* @param {array} [out] - An optional array in which to store the points of intersection.
*
* @return {array} An array with the points of intersection if objects intersect, otherwise an empty array.
*/
var GetTriangleToTriangle = function (triangleA, triangleB, out)
{
if (out === undefined) { out = []; }
if (TriangleToTriangle(triangleA, triangleB))
{
var lineA = triangleB.getLineA();
var lineB = triangleB.getLineB();
var lineC = triangleB.getLineC();
GetTriangleToLine(triangleA, lineA, out);
GetTriangleToLine(triangleA, lineB, out);
GetTriangleToLine(triangleA, lineC, out);
}
return out;
};
module.exports = GetTriangleToTriangle;

View file

@ -12,7 +12,16 @@ module.exports = {
CircleToCircle: require('./CircleToCircle'), CircleToCircle: require('./CircleToCircle'),
CircleToRectangle: require('./CircleToRectangle'), CircleToRectangle: require('./CircleToRectangle'),
GetCircleToCircle: require('./GetCircleToCircle'),
GetCircleToRectangle: require('./GetCircleToRectangle'),
GetLineToCircle: require('./GetLineToCircle'),
GetLineToRectangle: require('./GetLineToRectangle'),
GetRectangleIntersection: require('./GetRectangleIntersection'), GetRectangleIntersection: require('./GetRectangleIntersection'),
GetRectangleToRectangle: require('./GetRectangleToRectangle'),
GetRectangleToTriangle: require('./GetRectangleToTriangle'),
GetTriangleToCircle: require('./GetTriangleToCircle'),
GetTriangleToLine: require('./GetTriangleToLine'),
GetTriangleToTriangle: require('./GetTriangleToTriangle'),
LineToCircle: require('./LineToCircle'), LineToCircle: require('./LineToCircle'),
LineToLine: require('./LineToLine'), LineToLine: require('./LineToLine'),
LineToRectangle: require('./LineToRectangle'), LineToRectangle: require('./LineToRectangle'),