mirror of
https://github.com/photonstorm/phaser
synced 2024-11-27 07:01:20 +00:00
Added Rectangle.PerimeterPoint method for getting the point on the perimeter of a rectangle based on the given angle.
This commit is contained in:
parent
032f6e7084
commit
b3b495e53c
2 changed files with 67 additions and 0 deletions
66
v3/src/geom/rectangle/PerimeterPoint.js
Normal file
66
v3/src/geom/rectangle/PerimeterPoint.js
Normal file
|
@ -0,0 +1,66 @@
|
|||
var MATH_CONST = require('../../math/const');
|
||||
|
||||
// deg = degrees
|
||||
|
||||
var PerimeterPoint = function (rect, deg, out)
|
||||
{
|
||||
if (out === undefined) { out = { x: 0, y: 0 }; }
|
||||
|
||||
var theta = deg * MATH_CONST.DEG_TO_RAD;
|
||||
|
||||
while (theta < -Math.PI)
|
||||
{
|
||||
theta += MATH_CONST.PI2;
|
||||
}
|
||||
|
||||
while (theta > Math.PI)
|
||||
{
|
||||
theta -= MATH_CONST.PI2;
|
||||
}
|
||||
|
||||
var rectAtan = Math.atan2(rect.height, rect.width);
|
||||
var tanTheta = Math.tan(theta);
|
||||
var thetaBounds = Math.PI - rectAtan;
|
||||
var region;
|
||||
var xFactor = 1;
|
||||
var yFactor = 1;
|
||||
|
||||
if (theta > -rectAtan && theta <= rectAtan)
|
||||
{
|
||||
region = 1;
|
||||
yFactor = -1;
|
||||
}
|
||||
else if (theta > rectAtan && theta <= thetaBounds)
|
||||
{
|
||||
region = 2;
|
||||
yFactor = -1;
|
||||
}
|
||||
else if (theta > thetaBounds || theta <= -thetaBounds)
|
||||
{
|
||||
region = 3;
|
||||
xFactor = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
region = 4;
|
||||
xFactor = -1;
|
||||
}
|
||||
|
||||
out.x = rect.x + (rect.width / 2);
|
||||
out.y = rect.y + (rect.height / 2);
|
||||
|
||||
if (region === 1 || region === 3)
|
||||
{
|
||||
out.x += xFactor * (rect.width / 2); // "Z0"
|
||||
out.y += yFactor * (rect.width / 2) * tanTheta;
|
||||
}
|
||||
else
|
||||
{
|
||||
out.x += xFactor * (rect.height / (2 * tanTheta)); // "Z1"
|
||||
out.y += yFactor * (rect.height / 2);
|
||||
}
|
||||
|
||||
return out;
|
||||
};
|
||||
|
||||
module.exports = PerimeterPoint;
|
|
@ -27,6 +27,7 @@ Rectangle.Offset = require('./Offset');
|
|||
Rectangle.OffsetPoint = require('./OffsetPoint');
|
||||
Rectangle.Overlaps = require('./Overlaps');
|
||||
Rectangle.Perimeter = require('./Perimeter');
|
||||
Rectangle.PerimeterPoint = require('./PerimeterPoint');
|
||||
Rectangle.Random = require('./Random');
|
||||
Rectangle.Scale = require('./Scale');
|
||||
Rectangle.Union = require('./Union');
|
||||
|
|
Loading…
Reference in a new issue