From b5b86595d9226e0398bf8cd2c56a813fa88bb472 Mon Sep 17 00:00:00 2001 From: Richard Davey Date: Sun, 1 Jan 2017 14:33:41 +0000 Subject: [PATCH] Circle.isEmpty and Circle Intersection functions. --- v3/src/geom/circle/Circle.js | 5 +++ v3/src/geom/intersects/CircleVsCircle.js | 8 +++++ v3/src/geom/intersects/CircleVsRectangle.js | 36 +++++++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 v3/src/geom/intersects/CircleVsCircle.js create mode 100644 v3/src/geom/intersects/CircleVsRectangle.js diff --git a/v3/src/geom/circle/Circle.js b/v3/src/geom/circle/Circle.js index ad9e28411..425eeda94 100644 --- a/v3/src/geom/circle/Circle.js +++ b/v3/src/geom/circle/Circle.js @@ -39,6 +39,11 @@ Circle.prototype = { this.y = y; return this; + }, + + isEmpty: function () + { + return (this._radius <= 0); } }; diff --git a/v3/src/geom/intersects/CircleVsCircle.js b/v3/src/geom/intersects/CircleVsCircle.js new file mode 100644 index 000000000..63149a50f --- /dev/null +++ b/v3/src/geom/intersects/CircleVsCircle.js @@ -0,0 +1,8 @@ +var DistanceBetween = require('../../math/distance/DistanceBetween'); + +var CircleVsCircle = function (circleA, circleB) +{ + return (DistanceBetween(circleA.x, circleA.y, circleB.x, circleB.y) <= (circleA.radius + circleB.radius)); +}; + +module.exports = CircleVsCircle; diff --git a/v3/src/geom/intersects/CircleVsRectangle.js b/v3/src/geom/intersects/CircleVsRectangle.js new file mode 100644 index 000000000..2f7fc4edf --- /dev/null +++ b/v3/src/geom/intersects/CircleVsRectangle.js @@ -0,0 +1,36 @@ +var CircleVsRectangle = function (circle, rectangle) +{ + var halfWidth = rectangle.width / 2; + var halfHeight = rectangle.height / 2; + + var cx = Math.abs(circle.x - rectangle.x - halfWidth); + var xDist = halfWidth + circle.radius; + + if (cx > xDist) + { + return false; + } + + var cy = Math.abs(circle.y - rectangle.y - halfHeight); + var yDist = halfHeight + circle.radius; + + if (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; +}; + +module.exports = CircleVsRectangle;