|
1 | 1 | var Point = require('../point/Point'); |
2 | | -var MATH_CONST = require('../../math/const'); |
3 | | - |
4 | | -// deg = degrees (0-360) |
| 2 | +var DegToRad = require('../../math/DegToRad'); |
5 | 3 |
|
6 | 4 | /** |
7 | 5 | * [description] |
8 | 6 | * |
9 | 7 | * @function Phaser.Geom.Rectangle.PerimeterPoint |
10 | 8 | * @since 3.0.0 |
11 | 9 | * |
12 | | - * @param {Phaser.Geom.Rectangle} rect - [description] |
13 | | - * @param {integer} deg - [description] |
| 10 | + * @param {Phaser.Geom.Rectangle} rectangle - [description] |
| 11 | + * @param {integer} angle - [description] |
14 | 12 | * @param {Phaser.Geom.Point} [out] - [description] |
15 | 13 | * |
16 | 14 | * @return {Phaser.Geom.Point} [description] |
17 | 15 | */ |
18 | | -var PerimeterPoint = function (rect, deg, out) |
| 16 | +var PerimeterPoint = function (rectangle, angle, out) |
19 | 17 | { |
20 | 18 | if (out === undefined) { out = new Point(); } |
21 | 19 |
|
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); |
28 | 21 |
|
29 | | - while (theta > Math.PI) |
30 | | - { |
31 | | - theta -= MATH_CONST.PI2; |
32 | | - } |
| 22 | + var s = Math.sin(angle); |
| 23 | + var c = Math.cos(angle); |
33 | 24 |
|
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; |
40 | 27 |
|
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)) |
47 | 29 | { |
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; |
55 | 31 | } |
56 | 32 | else |
57 | 33 | { |
58 | | - region = 4; |
59 | | - xFactor = -1; |
| 34 | + dx = (dy * c) / s; |
60 | 35 | } |
61 | 36 |
|
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; |
75 | 39 |
|
76 | 40 | return out; |
77 | 41 | }; |
|
0 commit comments