-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPoint.js
336 lines (290 loc) · 7.92 KB
/
Point.js
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
export class Point
{
/** @type {number} */
#x = 0;
/** @type {number} */
#y = 0;
/**
* Create a new Point
* @param {number|float} x
* @param {number|float} y
*/
constructor(x = 0, y = 0) {
this.x = x;
this.y = y;
}
/** @returns {number} */
get x() {
return this.#x;
}
/** @returns {number} */
get y() {
return this.#y;
}
/** @param {number} value */
set x(value) {
this.#x = Math.round(value);
}
/** @param {number} value */
set y(value) {
this.#y = Math.round(value);
}
// Angles
/**
* Calculate the angle from the origin to the target in a clockwise direction
* @param {Point} origin
* @param {Point} target
* @param {number|float} offset
* @param {boolean} relative
* @returns {float} The angle in degrees
*/
static angleBetween(origin, target, offset = 0, relative = false)
{
const degreesPerRadian = 180 / Math.PI;
const distanceX = origin.distanceX(target);
const distanceY = origin.distanceY(target);
const negativeX = origin.x > target.x;
const negativeY = origin.y > target.y;
let angle = 0;
let offsetAngle = 0;
if (negativeX === true && negativeY === true) {
offsetAngle = 180;
} else if (negativeX === false && negativeY === true) {
offsetAngle = 270;
} else if (negativeX === false && negativeY === false) {
offsetAngle = 0;
} else {
offsetAngle = 90;
}
angle = negativeX === negativeY
? Math.atan(distanceY / distanceX)
: Math.atan(distanceX / distanceY);
angle = (angle * degreesPerRadian) + offsetAngle;
return relative === true
? Point.relativeAngle(angle - offset)
: angle - offset;
}
/**
* Get the angle relative to 0 (right-hand side in Foundry)
* @param {number|float} angle
* @returns {number|float}
*/
static relativeAngle(angle)
{
angle = (angle + 360) % 360;
return angle > 180
? angle -= 360
: angle;
}
/**
* Get the angle from this Point to another
* @param {Point} point
* @param {number|float} offset
* @param {boolean} relative
* @returns {number|float}
*/
angleTo(point, offset = 0, relative = false)
{
return Point.angleBetween(this, point, offset, relative);
}
// Comparisons
/**
* Whether this point is aligned with the grid
* @param {number} gridSize
* @returns {boolean}
*/
isOnGrid(gridSize)
{
return this.x % gridSize === 0
&& this.y % gridSize === 0;
}
/**
* Whether this Point has the same co-ordinates as another Point
* @param {Point} point
* @returns {boolean}
*/
matches(point)
{
return this.x === point.x
&& this.y === point.y;
}
// Distances
/**
* Find the absolute difference between two numbers
* @param {number|float} start
* @param {number|float} end
* @returns {number|float}
*/
static absoluteDifference(start, end)
{
return Math.abs(start - end);
}
/**
* Calculate the distance between two points
* @param {Point} origin
* @param {Point} target
* @returns {number|float}
*/
static distanceBetween(origin, target)
{
return Math.hypot(
Point.absoluteDifference(origin.x, target.x),
Point.absoluteDifference(origin.y, target.y),
);
}
/**
* Find the relative difference between to numbers
* @param {number|float} origin
* @param {number|float} comparator
* @returns {number|float}
*/
static relativeDifference(origin, comparator)
{
const difference = Point.absoluteDifference(origin, comparator);
return comparator < origin
? -difference
: difference;
}
/**
* Get the distance between this Point and another
* @param {Point} point
* @returns {number|float}
*/
distanceTo(point)
{
return Point.distanceBetween(this, point);
}
/**
* Get the distance on the X axis between this Point and another
* @param {Point} point
* @returns {number|float}
*/
distanceX(point)
{
return Point.absoluteDifference(this.x, point.x);
}
/**
* Get the distance on the Y axis between this Point and another
* @param {Point} point
* @returns {number|float}
*/
distanceY(point)
{
return Point.absoluteDifference(this.y, point.y);
}
// Movement
/**
* Move a point at an angle anti-clockwise
* @param {number|float} angle
* @param {number} distance
*/
moveAnticlockwise(angle, distance)
{
switch (true) {
case angle === 0:
case angle === 360:
case angle < 360 && angle > 315:
this.x += distance;
break;
case angle === 315:
case angle < 315 && angle > 270:
this.x += distance;
this.y -= distance;
break;
case angle === 270:
case angle < 270 && angle > 225:
this.y -= distance;
break;
case angle === 225:
case angle < 225 && angle > 180:
this.x -= distance;
this.y -= distance;
break;
case angle === 180:
case angle < 180 && angle > 135:
this.x -= distance;
break;
case angle === 135:
case angle < 135 && angle > 90:
this.x -= distance;
this.y += distance;
break;
case angle === 90:
case angle < 90 && angle > 45:
this.y += distance;
break;
case angle === 45:
case angle < 45 && angle > 0:
this.x += distance;
this.y += distance;
break;
}
}
/**
* Move a point at an angle clockwise
* @param {number|float} angle
* @param {number} distance
*/
moveClockwise(angle, distance)
{
switch (true) {
case angle === 0:
case angle === 360:
case angle > 0 && angle < 45:
this.x += distance;
break;
case angle === 45:
case angle > 45 && angle < 90:
this.x += distance;
this.y += distance;
break;
case angle === 90:
case angle > 90 && angle < 135:
this.y += distance;
break;
case angle === 135:
case angle > 135 && angle < 180:
this.x -= distance;
this.y += distance;
break;
case angle === 180:
case angle > 180 && angle < 225:
this.x -= distance;
break;
case angle === 225:
case angle > 225 && angle < 270:
this.x -= distance;
this.y -= distance;
break;
case angle === 270:
case angle > 270 && angle < 315:
this.y -= distance;
break;
case angle === 315:
case angle > 315 && angle < 360:
this.x += distance;
this.y -= distance;
break;
}
}
/**
* Move a point by an amount
* @param {number} x
* @param {number} y
*/
moveBy(x, y)
{
this.x += x;
this.y += y;
}
/**
* Move a point to a co-ordinate
* @param {number} x
* @param {number} y
*/
moveTo(x, y)
{
this.x = x;
this.y = y;
}
}