Circle.isEmpty and Circle Intersection functions.

This commit is contained in:
Richard Davey 2017-01-01 14:33:41 +00:00
parent 14fe0b1713
commit b5b86595d9
3 changed files with 49 additions and 0 deletions

View file

@ -39,6 +39,11 @@ Circle.prototype = {
this.y = y;
return this;
},
isEmpty: function ()
{
return (this._radius <= 0);
}
};

View file

@ -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;

View file

@ -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;