Skip to content

Commit 2f5da71

Browse files
committed
Revised to use faster method of detecting the perimeter point from the angle.
1 parent 87c3f70 commit 2f5da71

1 file changed

Lines changed: 14 additions & 50 deletions

File tree

v3/src/geom/rectangle/PerimeterPoint.js

Lines changed: 14 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,41 @@
11
var Point = require('../point/Point');
2-
var MATH_CONST = require('../../math/const');
3-
4-
// deg = degrees (0-360)
2+
var DegToRad = require('../../math/DegToRad');
53

64
/**
75
* [description]
86
*
97
* @function Phaser.Geom.Rectangle.PerimeterPoint
108
* @since 3.0.0
119
*
12-
* @param {Phaser.Geom.Rectangle} rect - [description]
13-
* @param {integer} deg - [description]
10+
* @param {Phaser.Geom.Rectangle} rectangle - [description]
11+
* @param {integer} angle - [description]
1412
* @param {Phaser.Geom.Point} [out] - [description]
1513
*
1614
* @return {Phaser.Geom.Point} [description]
1715
*/
18-
var PerimeterPoint = function (rect, deg, out)
16+
var PerimeterPoint = function (rectangle, angle, out)
1917
{
2018
if (out === undefined) { out = new Point(); }
2119

22-
var theta = deg * MATH_CONST.DEG_TO_RAD;
23-
24-
while (theta < -Math.PI)
25-
{
26-
theta += MATH_CONST.PI2;
27-
}
20+
angle = DegToRad(angle);
2821

29-
while (theta > Math.PI)
30-
{
31-
theta -= MATH_CONST.PI2;
32-
}
22+
var s = Math.sin(angle);
23+
var c = Math.cos(angle);
3324

34-
var rectAtan = Math.atan2(rect.height, rect.width);
35-
var tanTheta = Math.tan(theta);
36-
var thetaBounds = Math.PI - rectAtan;
37-
var region;
38-
var xFactor = 1;
39-
var yFactor = 1;
25+
var dx = (c > 0) ? rectangle.width / 2 : rectangle.width / -2;
26+
var dy = (s > 0) ? rectangle.height / 2 : rectangle.height / -2;
4027

41-
if (theta > -rectAtan && theta <= rectAtan)
42-
{
43-
region = 1;
44-
yFactor = -1;
45-
}
46-
else if (theta > rectAtan && theta <= thetaBounds)
28+
if (Math.abs(dx * s) < Math.abs(dy * c))
4729
{
48-
region = 2;
49-
yFactor = -1;
50-
}
51-
else if (theta > thetaBounds || theta <= -thetaBounds)
52-
{
53-
region = 3;
54-
xFactor = -1;
30+
dy = (dx * s) / c;
5531
}
5632
else
5733
{
58-
region = 4;
59-
xFactor = -1;
34+
dx = (dy * c) / s;
6035
}
6136

62-
out.x = rect.x + (rect.width / 2);
63-
out.y = rect.y + (rect.height / 2);
64-
65-
if (region === 1 || region === 3)
66-
{
67-
out.x += xFactor * (rect.width / 2); // "Z0"
68-
out.y += yFactor * (rect.width / 2) * tanTheta;
69-
}
70-
else
71-
{
72-
out.x += xFactor * (rect.height / (2 * tanTheta)); // "Z1"
73-
out.y += yFactor * (rect.height / 2);
74-
}
37+
out.x = dx + rectangle.centerX;
38+
out.y = dy + rectangle.centerY;
7539

7640
return out;
7741
};

0 commit comments

Comments
 (0)