forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetBounds.js
More file actions
179 lines (135 loc) · 4.34 KB
/
Copy pathGetBounds.js
File metadata and controls
179 lines (135 loc) · 4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
var Rectangle = require('../../geom/rectangle/Rectangle');
var RotateAround = require('../../math/RotateAround');
var Vector2 = require('../../math/Vector2');
var GetBounds = {
getCenter: function (output)
{
if (output === undefined) { output = new Vector2(); }
output.x = this.x - (this.displayWidth * this.originX) + (this.displayWidth / 2);
output.y = this.y - (this.displayHeight * this.originY) + (this.displayHeight / 2);
return output;
},
getTopLeft: function (output)
{
if (output === undefined) { output = new Vector2(); }
output.x = this.x - (this.displayWidth * this.originX);
output.y = this.y - (this.displayHeight * this.originY);
if (this.rotation !== 0)
{
RotateAround(output, this.x, this.y, this.rotation);
}
return output;
},
getTopRight: function (output)
{
if (output === undefined) { output = new Vector2(); }
output.x = (this.x - (this.displayWidth * this.originX)) + this.displayWidth;
output.y = this.y - (this.displayHeight * this.originY);
if (this.rotation !== 0)
{
RotateAround(output, this.x, this.y, this.rotation);
}
return output;
},
getBottomLeft: function (output)
{
if (output === undefined) { output = new Vector2(); }
output.x = this.x - (this.displayWidth * this.originX);
output.y = (this.y - (this.displayHeight * this.originY)) + this.displayHeight;
if (this.rotation !== 0)
{
RotateAround(output, this.x, this.y, this.rotation);
}
return output;
},
getBottomRight: function (output)
{
if (output === undefined) { output = new Vector2(); }
output.x = (this.x - (this.displayWidth * this.originX)) + this.displayWidth;
output.y = (this.y - (this.displayHeight * this.originY)) + this.displayHeight;
if (this.rotation !== 0)
{
RotateAround(output, this.x, this.y, this.rotation);
}
return output;
},
getBounds: function (output)
{
if (output === undefined) { output = new Rectangle(); }
// We can use the output object to temporarily store the x/y coords in:
this.getTopLeft(output);
var TLx = output.x;
var TLy = output.y;
this.getTopRight(output);
var TRx = output.x;
var TRy = output.y;
this.getBottomLeft(output);
var BLx = output.x;
var BLy = output.y;
this.getBottomRight(output);
var BRx = output.x;
var BRy = output.y;
output.x = Math.min(TLx, TRx, BLx, BRx);
output.y = Math.min(TLy, TRy, BLy, BRy);
output.width = Math.max(TLx, TRx, BLx, BRx) - output.x;
output.height = Math.max(TLy, TRy, BLy, BRy) - output.y;
return output;
},
// Works but only if originX/Y = 0
/*
getBounds: function (output)
{
if (output === undefined) { output = new Rectangle(); }
var x = this.x;
var y = this.y;
var w = this.displayWidth;
var h = this.displayHeight;
var r = this.rotation;
var wct = w * Math.cos(r);
var hct = h * Math.cos(r);
var wst = w * Math.sin(r);
var hst = h * Math.sin(r);
var xMin = x;
var xMax = x;
var yMin = y;
var yMax = y;
if (r > 0)
{
if (r < 1.5707963267948966)
{
// 0 < theta < 90
yMax = y + hct + wst;
xMin = x - hst;
xMax = x + wct;
}
else
{
// 90 <= theta <= 180
yMin = y + hct;
yMax = y + wst;
xMin = x - hst + wct;
}
}
else if (r > -1.5707963267948966)
{
// -90 < theta <= 0
yMin = y + wst;
yMax = y + hct;
xMax = x + wct - hst;
}
else
{
// -180 <= theta <= -90
yMin = y + wst + hct;
xMin = x + wct;
xMax = x - hst;
}
output.x = xMin;
output.y = yMin;
output.width = xMax - xMin;
output.height = yMax - yMin;
return output;
}
*/
};
module.exports = GetBounds;