From 5805adf2144ce70c23d6224f4c2ec65c1e074f7b Mon Sep 17 00:00:00 2001 From: Richard Davey Date: Sun, 1 Jan 2017 15:30:54 +0000 Subject: [PATCH] Added Intersection functions and PointsAABB. --- v3/src/geom/intersects/CircleVsRectangle.js | 21 +++----- .../intersects/GetRectangleIntersection.js | 19 +++++++ .../geom/intersects/RectangleVsRectangle.js | 11 ++++ v3/src/geom/intersects/RectangleVsValues.js | 13 +++++ v3/src/geom/point/GetRectangleFromPoints.js | 54 +++++++++++++++++++ 5 files changed, 105 insertions(+), 13 deletions(-) create mode 100644 v3/src/geom/intersects/GetRectangleIntersection.js create mode 100644 v3/src/geom/intersects/RectangleVsRectangle.js create mode 100644 v3/src/geom/intersects/RectangleVsValues.js create mode 100644 v3/src/geom/point/GetRectangleFromPoints.js diff --git a/v3/src/geom/intersects/CircleVsRectangle.js b/v3/src/geom/intersects/CircleVsRectangle.js index 2f7fc4edf..985c0a740 100644 --- a/v3/src/geom/intersects/CircleVsRectangle.js +++ b/v3/src/geom/intersects/CircleVsRectangle.js @@ -1,36 +1,31 @@ -var CircleVsRectangle = function (circle, rectangle) +var CircleVsRectangle = function (circle, rect) { - var halfWidth = rectangle.width / 2; - var halfHeight = rectangle.height / 2; + var halfWidth = rect.width / 2; + var halfHeight = rect.height / 2; - var cx = Math.abs(circle.x - rectangle.x - halfWidth); + var cx = Math.abs(circle.x - rect.x - halfWidth); var xDist = halfWidth + circle.radius; - if (cx > xDist) + if (cx <= halfWidth || cx > xDist) { return false; } - var cy = Math.abs(circle.y - rectangle.y - halfHeight); + var cy = Math.abs(circle.y - rect.y - halfHeight); var yDist = halfHeight + circle.radius; - if (cy > yDist) + if (cy <= halfHeight || cy > yDist) { return false; } - if (cx <= halfWidth || cy <= halfHeight) - { - return true; - } - var xCornerDist = cx - halfWidth; var yCornerDist = cy - halfHeight; var xCornerDistSq = xCornerDist * xCornerDist; var yCornerDistSq = yCornerDist * yCornerDist; var maxCornerDistSq = circle.radius * circle.radius; - return xCornerDistSq + yCornerDistSq <= maxCornerDistSq; + return (xCornerDistSq + yCornerDistSq <= maxCornerDistSq); }; module.exports = CircleVsRectangle; diff --git a/v3/src/geom/intersects/GetRectangleIntersection.js b/v3/src/geom/intersects/GetRectangleIntersection.js new file mode 100644 index 000000000..452ba1bc2 --- /dev/null +++ b/v3/src/geom/intersects/GetRectangleIntersection.js @@ -0,0 +1,19 @@ +var Rectangle = require('../rectangle/Rectangle'); +var RectangleVsRectangle = require('./RectangleVsRectangle'); + +var GetRectangleIntersection = function (rectA, rectB, output) +{ + if (output === undefined) { output = new Rectangle(); } + + if (RectangleVsRectangle(rectA, rectB)) + { + output.x = Math.max(rectA.x, rectB.x); + output.y = Math.max(rectA.y, rectB.y); + output.width = Math.min(rectA.right, rectB.right) - output.x; + output.height = Math.min(rectA.bottom, rectB.bottom) - output.y; + } + + return output; +}; + +module.exports = GetRectangleIntersection; diff --git a/v3/src/geom/intersects/RectangleVsRectangle.js b/v3/src/geom/intersects/RectangleVsRectangle.js new file mode 100644 index 000000000..c5690fc4d --- /dev/null +++ b/v3/src/geom/intersects/RectangleVsRectangle.js @@ -0,0 +1,11 @@ +var RectangleVsRectangle = function (rectA, rectB) +{ + if (rectA.width <= 0 || rectA.height <= 0 || rectB.width <= 0 || rectB.height <= 0) + { + return false; + } + + return !(rectA.right < rectB.x || rectA.bottom < rectB.y || rectA.x > rectB.right || rectA.y > rectB.bottom); +}; + +module.exports = RectangleVsRectangle; diff --git a/v3/src/geom/intersects/RectangleVsValues.js b/v3/src/geom/intersects/RectangleVsValues.js new file mode 100644 index 000000000..df194b5d8 --- /dev/null +++ b/v3/src/geom/intersects/RectangleVsValues.js @@ -0,0 +1,13 @@ +var RectangleVsValues = function (rect, left, right, top, bottom, tolerance) +{ + if (tolerance === undefined) { tolerance = 0; } + + return !( + left > rect.right + tolerance || + right < rect.left - tolerance || + top > rect.bottom + tolerance || + bottom < rect.top - tolerance + ); +}; + +module.exports = RectangleVsValues; diff --git a/v3/src/geom/point/GetRectangleFromPoints.js b/v3/src/geom/point/GetRectangleFromPoints.js new file mode 100644 index 000000000..0130fe988 --- /dev/null +++ b/v3/src/geom/point/GetRectangleFromPoints.js @@ -0,0 +1,54 @@ +var Rectangle = require('../rectangle/Rectangle'); + +/** +* Calculates the Axis Aligned Bounding Box (or aabb) from an array of points. +* +* @method Phaser.Rectangle#aabb +* @param {Phaser.Point[]} points - The array of one or more points. +* @param {Phaser.Rectangle} [out] - Optional Rectangle to store the value in, if not supplied a new Rectangle object will be created. +* @return {Phaser.Rectangle} The new Rectangle object. +* @static +*/ +var GetRectangleFromPoints = function (points, out) +{ + if (out === undefined) { out = new Rectangle(); } + + var xMax = Number.NEGATIVE_INFINITY; + var xMin = Number.POSITIVE_INFINITY; + var yMax = Number.NEGATIVE_INFINITY; + var yMin = Number.POSITIVE_INFINITY; + + for (var i = 0; i < points.length; i++) + { + var point = points[i]; + + if (point.x > xMax) + { + xMax = point.x; + } + + if (point.x < xMin) + { + xMin = point.x; + } + + if (point.y > yMax) + { + yMax = point.y; + } + + if (point.y < yMin) + { + yMin = point.y; + } + } + + out.x = xMin; + out.y = yMin; + out.width = xMax - xMin; + out.height = yMax - yMin; + + return out; +}; + +module.exports = GetRectangleFromPoints;