@vpmedia/phaser 1.95.0 → 1.97.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +51 -0
- package/package.json +10 -10
- package/src/phaser/core/animation.js +61 -56
- package/src/phaser/core/animation_manager.js +55 -55
- package/src/phaser/core/cache.js +154 -154
- package/src/phaser/core/device.js +2 -1
- package/src/phaser/core/device_util.js +27 -27
- package/src/phaser/core/dom.js +43 -43
- package/src/phaser/core/event_manager.js +63 -63
- package/src/phaser/core/factory.js +47 -47
- package/src/phaser/core/frame.js +30 -30
- package/src/phaser/core/frame_data.js +30 -28
- package/src/phaser/core/frame_util.js +8 -8
- package/src/phaser/core/game.js +34 -18
- package/src/phaser/core/loader.js +170 -170
- package/src/phaser/core/loader_parser.js +22 -22
- package/src/phaser/core/raf.js +1 -1
- package/src/phaser/core/scene.js +16 -10
- package/src/phaser/core/scene_manager.js +51 -43
- package/src/phaser/core/signal.js +68 -52
- package/src/phaser/core/signal.test.js +37 -0
- package/src/phaser/core/sound.js +54 -54
- package/src/phaser/core/sound_manager.js +49 -49
- package/src/phaser/core/stage.js +16 -16
- package/src/phaser/core/time.js +30 -30
- package/src/phaser/core/timer.js +79 -64
- package/src/phaser/core/timer_event.js +9 -9
- package/src/phaser/core/tween.js +90 -90
- package/src/phaser/core/tween_data.js +30 -30
- package/src/phaser/core/tween_manager.js +29 -24
- package/src/phaser/core/world.js +4 -3
- package/src/phaser/display/bitmap_text.js +61 -54
- package/src/phaser/display/button.js +48 -47
- package/src/phaser/display/canvas/buffer.js +8 -8
- package/src/phaser/display/canvas/graphics.js +8 -8
- package/src/phaser/display/canvas/masker.js +5 -5
- package/src/phaser/display/canvas/pool.js +18 -18
- package/src/phaser/display/canvas/renderer.js +16 -16
- package/src/phaser/display/canvas/tinter.js +22 -22
- package/src/phaser/display/canvas/util.js +42 -42
- package/src/phaser/display/display_object.js +119 -108
- package/src/phaser/display/graphics.js +112 -112
- package/src/phaser/display/graphics_data.js +10 -10
- package/src/phaser/display/graphics_data_util.js +3 -3
- package/src/phaser/display/group.js +50 -50
- package/src/phaser/display/image.js +54 -50
- package/src/phaser/display/sprite_batch.js +2 -2
- package/src/phaser/display/sprite_util.js +19 -19
- package/src/phaser/display/text.js +212 -187
- package/src/phaser/display/webgl/abstract_filter.js +4 -4
- package/src/phaser/display/webgl/base_texture.js +9 -9
- package/src/phaser/display/webgl/blend_manager.js +7 -7
- package/src/phaser/display/webgl/earcut.js +95 -95
- package/src/phaser/display/webgl/earcut_node.js +4 -4
- package/src/phaser/display/webgl/fast_sprite_batch.js +15 -15
- package/src/phaser/display/webgl/filter_manager.js +9 -9
- package/src/phaser/display/webgl/filter_texture.js +10 -10
- package/src/phaser/display/webgl/graphics.js +37 -37
- package/src/phaser/display/webgl/graphics_data.js +4 -4
- package/src/phaser/display/webgl/mask_manager.js +6 -6
- package/src/phaser/display/webgl/render_texture.js +16 -16
- package/src/phaser/display/webgl/renderer.js +20 -20
- package/src/phaser/display/webgl/shader/complex.js +4 -4
- package/src/phaser/display/webgl/shader/fast.js +4 -4
- package/src/phaser/display/webgl/shader/normal.js +8 -8
- package/src/phaser/display/webgl/shader/primitive.js +4 -4
- package/src/phaser/display/webgl/shader/strip.js +4 -4
- package/src/phaser/display/webgl/shader_manager.js +9 -9
- package/src/phaser/display/webgl/sprite_batch.js +18 -18
- package/src/phaser/display/webgl/stencil_manager.js +16 -16
- package/src/phaser/display/webgl/texture.js +13 -13
- package/src/phaser/display/webgl/texture_util.js +8 -8
- package/src/phaser/display/webgl/util.js +25 -25
- package/src/phaser/geom/circle.js +69 -69
- package/src/phaser/geom/circle.test.js +1 -0
- package/src/phaser/geom/ellipse.js +28 -28
- package/src/phaser/geom/ellipse.test.js +1 -0
- package/src/phaser/geom/line.js +93 -93
- package/src/phaser/geom/line.test.js +1 -0
- package/src/phaser/geom/matrix.js +54 -54
- package/src/phaser/geom/matrix.test.js +1 -0
- package/src/phaser/geom/point.js +96 -96
- package/src/phaser/geom/point.test.js +1 -0
- package/src/phaser/geom/polygon.js +22 -22
- package/src/phaser/geom/rectangle.js +132 -132
- package/src/phaser/geom/rectangle.test.js +1 -0
- package/src/phaser/geom/rounded_rectangle.js +12 -12
- package/src/phaser/geom/rounded_rectangle.test.js +1 -0
- package/src/phaser/geom/util/circle.js +33 -33
- package/src/phaser/geom/util/circle.test.js +1 -0
- package/src/phaser/geom/util/ellipse.js +5 -5
- package/src/phaser/geom/util/ellipse.test.js +1 -0
- package/src/phaser/geom/util/line.js +26 -26
- package/src/phaser/geom/util/line.test.js +1 -0
- package/src/phaser/geom/util/matrix.js +8 -8
- package/src/phaser/geom/util/matrix.test.js +1 -0
- package/src/phaser/geom/util/point.js +97 -97
- package/src/phaser/geom/util/point.test.js +1 -0
- package/src/phaser/geom/util/polygon.js +4 -4
- package/src/phaser/geom/util/polygon.test.js +1 -0
- package/src/phaser/geom/util/rectangle.js +74 -74
- package/src/phaser/geom/util/rectangle.test.js +1 -0
- package/src/phaser/geom/util/rounded_rectangle.js +4 -4
- package/src/phaser/geom/util/rounded_rectangle.test.js +1 -0
- package/src/phaser/util/math.js +81 -81
- package/src/phaser/util/math.test.js +1 -0
- package/types/phaser/core/animation.d.ts +61 -56
- package/types/phaser/core/animation.d.ts.map +1 -1
- package/types/phaser/core/animation_manager.d.ts +55 -55
- package/types/phaser/core/cache.d.ts +154 -154
- package/types/phaser/core/device.d.ts.map +1 -1
- package/types/phaser/core/dom.d.ts +43 -43
- package/types/phaser/core/event_manager.d.ts +63 -63
- package/types/phaser/core/factory.d.ts +47 -47
- package/types/phaser/core/frame.d.ts +30 -30
- package/types/phaser/core/frame_data.d.ts +28 -27
- package/types/phaser/core/frame_data.d.ts.map +1 -1
- package/types/phaser/core/game.d.ts +52 -19
- package/types/phaser/core/game.d.ts.map +1 -1
- package/types/phaser/core/loader.d.ts +170 -170
- package/types/phaser/core/loader_parser.d.ts.map +1 -1
- package/types/phaser/core/raf.d.ts +1 -1
- package/types/phaser/core/scene.d.ts +15 -9
- package/types/phaser/core/scene.d.ts.map +1 -1
- package/types/phaser/core/scene_manager.d.ts +51 -43
- package/types/phaser/core/scene_manager.d.ts.map +1 -1
- package/types/phaser/core/signal.d.ts +54 -51
- package/types/phaser/core/signal.d.ts.map +1 -1
- package/types/phaser/core/sound.d.ts +54 -54
- package/types/phaser/core/sound_manager.d.ts +49 -49
- package/types/phaser/core/stage.d.ts +10 -10
- package/types/phaser/core/time.d.ts +30 -30
- package/types/phaser/core/timer.d.ts +64 -64
- package/types/phaser/core/timer_event.d.ts +9 -9
- package/types/phaser/core/tween.d.ts +90 -90
- package/types/phaser/core/tween_data.d.ts +30 -30
- package/types/phaser/core/tween_manager.d.ts +29 -24
- package/types/phaser/core/tween_manager.d.ts.map +1 -1
- package/types/phaser/core/world.d.ts +4 -3
- package/types/phaser/core/world.d.ts.map +1 -1
- package/types/phaser/display/bitmap_text.d.ts +59 -52
- package/types/phaser/display/bitmap_text.d.ts.map +1 -1
- package/types/phaser/display/button.d.ts +47 -46
- package/types/phaser/display/button.d.ts.map +1 -1
- package/types/phaser/display/canvas/buffer.d.ts +8 -8
- package/types/phaser/display/canvas/renderer.d.ts +16 -16
- package/types/phaser/display/display_object.d.ts +119 -108
- package/types/phaser/display/display_object.d.ts.map +1 -1
- package/types/phaser/display/graphics.d.ts +101 -101
- package/types/phaser/display/graphics_data.d.ts +10 -10
- package/types/phaser/display/group.d.ts +47 -47
- package/types/phaser/display/image.d.ts +42 -40
- package/types/phaser/display/image.d.ts.map +1 -1
- package/types/phaser/display/text.d.ts +207 -184
- package/types/phaser/display/text.d.ts.map +1 -1
- package/types/phaser/display/webgl/abstract_filter.d.ts +4 -4
- package/types/phaser/display/webgl/base_texture.d.ts +9 -9
- package/types/phaser/display/webgl/blend_manager.d.ts +6 -6
- package/types/phaser/display/webgl/earcut.d.ts +95 -95
- package/types/phaser/display/webgl/earcut_node.d.ts +4 -4
- package/types/phaser/display/webgl/fast_sprite_batch.d.ts +15 -15
- package/types/phaser/display/webgl/filter_manager.d.ts +8 -8
- package/types/phaser/display/webgl/filter_texture.d.ts +10 -10
- package/types/phaser/display/webgl/graphics_data.d.ts +4 -4
- package/types/phaser/display/webgl/render_texture.d.ts +16 -16
- package/types/phaser/display/webgl/renderer.d.ts +20 -20
- package/types/phaser/display/webgl/shader/complex.d.ts +4 -4
- package/types/phaser/display/webgl/shader/fast.d.ts +4 -4
- package/types/phaser/display/webgl/shader/normal.d.ts +8 -8
- package/types/phaser/display/webgl/shader/primitive.d.ts +4 -4
- package/types/phaser/display/webgl/shader/strip.d.ts +4 -4
- package/types/phaser/display/webgl/shader_manager.d.ts +8 -8
- package/types/phaser/display/webgl/sprite_batch.d.ts +17 -17
- package/types/phaser/display/webgl/stencil_manager.d.ts +15 -15
- package/types/phaser/display/webgl/texture.d.ts +12 -12
- package/types/phaser/geom/circle.d.ts +69 -69
- package/types/phaser/geom/ellipse.d.ts +28 -28
- package/types/phaser/geom/line.d.ts +93 -93
- package/types/phaser/geom/matrix.d.ts +54 -54
- package/types/phaser/geom/point.d.ts +96 -96
- package/types/phaser/geom/polygon.d.ts +22 -22
- package/types/phaser/geom/rectangle.d.ts +132 -132
- package/types/phaser/geom/rounded_rectangle.d.ts +12 -12
- package/pnpm-workspace.yaml +0 -4
package/src/phaser/geom/point.js
CHANGED
|
@@ -3,9 +3,9 @@ import { clone, distance, rotate } from './util/point.js';
|
|
|
3
3
|
|
|
4
4
|
export class Point {
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
7
|
-
* @param {number} x -
|
|
8
|
-
* @param {number} y -
|
|
6
|
+
* Creates a new Point instance.
|
|
7
|
+
* @param {number} x - The x coordinate of the point (default: 0).
|
|
8
|
+
* @param {number} y - The y coordinate of the point (default: 0).
|
|
9
9
|
*/
|
|
10
10
|
constructor(x = 0, y = 0) {
|
|
11
11
|
/** @type {number} */
|
|
@@ -17,27 +17,27 @@ export class Point {
|
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
/**
|
|
20
|
-
*
|
|
21
|
-
* @param {Point} source -
|
|
22
|
-
* @returns {Point}
|
|
20
|
+
* Copies the coordinates from another point to this point.
|
|
21
|
+
* @param {Point} source - The point to copy coordinates from.
|
|
22
|
+
* @returns {Point} This point instance for chaining.
|
|
23
23
|
*/
|
|
24
24
|
copyFrom(source) {
|
|
25
25
|
return this.setTo(source.x, source.y);
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
/**
|
|
29
|
-
*
|
|
30
|
-
* @returns {Point}
|
|
29
|
+
* Returns a new point with the x and y coordinates swapped.
|
|
30
|
+
* @returns {Point} A new point instance with swapped coordinates.
|
|
31
31
|
*/
|
|
32
32
|
invert() {
|
|
33
33
|
return this.setTo(this.y, this.x);
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
/**
|
|
37
|
-
*
|
|
38
|
-
* @param {number} x -
|
|
39
|
-
* @param {number} y -
|
|
40
|
-
* @returns {Point}
|
|
37
|
+
* Sets the coordinates of this point to new values.
|
|
38
|
+
* @param {number} x - The new x coordinate for the point.
|
|
39
|
+
* @param {number} y - The new y coordinate for the point.
|
|
40
|
+
* @returns {Point} This point instance for chaining.
|
|
41
41
|
*/
|
|
42
42
|
setTo(x, y) {
|
|
43
43
|
this.x = x || 0;
|
|
@@ -46,11 +46,11 @@ export class Point {
|
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
/**
|
|
49
|
-
*
|
|
50
|
-
* @param {number} x -
|
|
51
|
-
* @param {number} y -
|
|
52
|
-
* @returns {Point}
|
|
53
|
-
* @deprecated
|
|
49
|
+
* Sets the coordinates of this point to new values.
|
|
50
|
+
* @param {number} x - The new x coordinate for the point.
|
|
51
|
+
* @param {number} y - The new y coordinate for the point.
|
|
52
|
+
* @returns {Point} This point instance for chaining.
|
|
53
|
+
* @deprecated Use setTo instead.
|
|
54
54
|
*/
|
|
55
55
|
set(x, y) {
|
|
56
56
|
this.x = x || 0;
|
|
@@ -59,10 +59,10 @@ export class Point {
|
|
|
59
59
|
}
|
|
60
60
|
|
|
61
61
|
/**
|
|
62
|
-
*
|
|
63
|
-
* @param {number} x -
|
|
64
|
-
* @param {number} y -
|
|
65
|
-
* @returns {Point}
|
|
62
|
+
* Adds the given x and y values to the point's coordinates.
|
|
63
|
+
* @param {number} x - The amount to add to the x coordinate.
|
|
64
|
+
* @param {number} y - The amount to add to the y coordinate.
|
|
65
|
+
* @returns {Point} This point instance for chaining.
|
|
66
66
|
*/
|
|
67
67
|
add(x, y) {
|
|
68
68
|
this.x += x;
|
|
@@ -71,10 +71,10 @@ export class Point {
|
|
|
71
71
|
}
|
|
72
72
|
|
|
73
73
|
/**
|
|
74
|
-
*
|
|
75
|
-
* @param {number} x -
|
|
76
|
-
* @param {number} y -
|
|
77
|
-
* @returns {Point}
|
|
74
|
+
* Subtracts the given x and y values from the point's coordinates.
|
|
75
|
+
* @param {number} x - The amount to subtract from the x coordinate.
|
|
76
|
+
* @param {number} y - The amount to subtract from the y coordinate.
|
|
77
|
+
* @returns {Point} This point instance for chaining.
|
|
78
78
|
*/
|
|
79
79
|
subtract(x, y) {
|
|
80
80
|
this.x -= x;
|
|
@@ -83,10 +83,10 @@ export class Point {
|
|
|
83
83
|
}
|
|
84
84
|
|
|
85
85
|
/**
|
|
86
|
-
*
|
|
87
|
-
* @param {number} x -
|
|
88
|
-
* @param {number} y -
|
|
89
|
-
* @returns {Point}
|
|
86
|
+
* Multiplies the point's coordinates by the given x and y values.
|
|
87
|
+
* @param {number} x - The amount to multiply the x coordinate by.
|
|
88
|
+
* @param {number} y - The amount to multiply the y coordinate by.
|
|
89
|
+
* @returns {Point} This point instance for chaining.
|
|
90
90
|
*/
|
|
91
91
|
multiply(x, y) {
|
|
92
92
|
this.x *= x;
|
|
@@ -95,10 +95,10 @@ export class Point {
|
|
|
95
95
|
}
|
|
96
96
|
|
|
97
97
|
/**
|
|
98
|
-
*
|
|
99
|
-
* @param {number} x -
|
|
100
|
-
* @param {number} y -
|
|
101
|
-
* @returns {Point}
|
|
98
|
+
* Divides the point's coordinates by the given x and y values.
|
|
99
|
+
* @param {number} x - The amount to divide the x coordinate by.
|
|
100
|
+
* @param {number} y - The amount to divide the y coordinate by.
|
|
101
|
+
* @returns {Point} This point instance for chaining.
|
|
102
102
|
*/
|
|
103
103
|
divide(x, y) {
|
|
104
104
|
this.x /= x;
|
|
@@ -107,10 +107,10 @@ export class Point {
|
|
|
107
107
|
}
|
|
108
108
|
|
|
109
109
|
/**
|
|
110
|
-
*
|
|
111
|
-
* @param {number} min -
|
|
112
|
-
* @param {number} max -
|
|
113
|
-
* @returns {Point}
|
|
110
|
+
* Constrains the x coordinate of this point to the given range.
|
|
111
|
+
* @param {number} min - The minimum value for the x coordinate.
|
|
112
|
+
* @param {number} max - The maximum value for the x coordinate.
|
|
113
|
+
* @returns {Point} This point instance for chaining.
|
|
114
114
|
*/
|
|
115
115
|
clampX(min, max) {
|
|
116
116
|
this.x = Math.max(min, Math.min(max, this.x));
|
|
@@ -118,10 +118,10 @@ export class Point {
|
|
|
118
118
|
}
|
|
119
119
|
|
|
120
120
|
/**
|
|
121
|
-
*
|
|
122
|
-
* @param {number} min -
|
|
123
|
-
* @param {number} max -
|
|
124
|
-
* @returns {Point}
|
|
121
|
+
* Constrains the y coordinate of this point to the given range.
|
|
122
|
+
* @param {number} min - The minimum value for the y coordinate.
|
|
123
|
+
* @param {number} max - The maximum value for the y coordinate.
|
|
124
|
+
* @returns {Point} This point instance for chaining.
|
|
125
125
|
*/
|
|
126
126
|
clampY(min, max) {
|
|
127
127
|
this.y = Math.max(min, Math.min(max, this.y));
|
|
@@ -129,10 +129,10 @@ export class Point {
|
|
|
129
129
|
}
|
|
130
130
|
|
|
131
131
|
/**
|
|
132
|
-
*
|
|
133
|
-
* @param {number} min -
|
|
134
|
-
* @param {number} max -
|
|
135
|
-
* @returns {Point}
|
|
132
|
+
* Constrains both coordinates of this point to the given range.
|
|
133
|
+
* @param {number} min - The minimum value for both coordinates.
|
|
134
|
+
* @param {number} max - The maximum value for both coordinates.
|
|
135
|
+
* @returns {Point} This point instance for chaining.
|
|
136
136
|
*/
|
|
137
137
|
clamp(min, max) {
|
|
138
138
|
this.x = Math.max(min, Math.min(max, this.x));
|
|
@@ -141,17 +141,17 @@ export class Point {
|
|
|
141
141
|
}
|
|
142
142
|
|
|
143
143
|
/**
|
|
144
|
-
*
|
|
145
|
-
* @returns {Point}
|
|
144
|
+
* Creates a clone of this point.
|
|
145
|
+
* @returns {Point} A new point with the same coordinates as this one.
|
|
146
146
|
*/
|
|
147
147
|
clone() {
|
|
148
148
|
return clone(this);
|
|
149
149
|
}
|
|
150
150
|
|
|
151
151
|
/**
|
|
152
|
-
*
|
|
153
|
-
* @param {Point} dest -
|
|
154
|
-
* @returns {Point}
|
|
152
|
+
* Copies the coordinates of this point to another point.
|
|
153
|
+
* @param {Point} dest - The point to copy coordinates to.
|
|
154
|
+
* @returns {Point} The destination point.
|
|
155
155
|
*/
|
|
156
156
|
copyTo(dest) {
|
|
157
157
|
dest.x = this.x;
|
|
@@ -160,28 +160,28 @@ export class Point {
|
|
|
160
160
|
}
|
|
161
161
|
|
|
162
162
|
/**
|
|
163
|
-
*
|
|
164
|
-
* @param {Point} b -
|
|
165
|
-
* @returns {number}
|
|
163
|
+
* Calculates the distance between this point and another point.
|
|
164
|
+
* @param {Point} b - The other point to calculate the distance to.
|
|
165
|
+
* @returns {number} The distance between the two points.
|
|
166
166
|
*/
|
|
167
167
|
distance(b) {
|
|
168
168
|
return distance(this, b);
|
|
169
169
|
}
|
|
170
170
|
|
|
171
171
|
/**
|
|
172
|
-
*
|
|
173
|
-
* @param {Point} a -
|
|
174
|
-
* @returns {boolean}
|
|
172
|
+
* Checks if this point is equal to another point.
|
|
173
|
+
* @param {Point} a - The other point to compare with.
|
|
174
|
+
* @returns {boolean} True if the points have the same coordinates, false otherwise.
|
|
175
175
|
*/
|
|
176
176
|
equals(a) {
|
|
177
177
|
return a.x === this.x && a.y === this.y;
|
|
178
178
|
}
|
|
179
179
|
|
|
180
180
|
/**
|
|
181
|
-
*
|
|
182
|
-
* @param {Point} a -
|
|
183
|
-
* @param {boolean} asDegrees -
|
|
184
|
-
* @returns {number}
|
|
181
|
+
* Calculates the angle between this point and another point.
|
|
182
|
+
* @param {Point} a - The other point to calculate the angle to.
|
|
183
|
+
* @param {boolean} asDegrees - Whether to return the result in degrees (default: false).
|
|
184
|
+
* @returns {number} The angle between the two points in radians or degrees.
|
|
185
185
|
*/
|
|
186
186
|
angle(a, asDegrees = false) {
|
|
187
187
|
if (asDegrees) {
|
|
@@ -191,46 +191,46 @@ export class Point {
|
|
|
191
191
|
}
|
|
192
192
|
|
|
193
193
|
/**
|
|
194
|
-
*
|
|
195
|
-
* @param {number} x -
|
|
196
|
-
* @param {number} y -
|
|
197
|
-
* @param {number} angle -
|
|
198
|
-
* @param {boolean} asDegrees -
|
|
199
|
-
* @param {number | null | undefined} dist -
|
|
200
|
-
* @returns {Point}
|
|
194
|
+
* Rotates this point around another point by a given angle.
|
|
195
|
+
* @param {number} x - The x coordinate of the center point to rotate around.
|
|
196
|
+
* @param {number} y - The y coordinate of the center point to rotate around.
|
|
197
|
+
* @param {number} angle - The angle in radians to rotate by.
|
|
198
|
+
* @param {boolean} asDegrees - Whether the angle is provided in degrees (default: false).
|
|
199
|
+
* @param {number | null | undefined} dist - The distance to rotate from (default: null).
|
|
200
|
+
* @returns {Point} This point instance for chaining.
|
|
201
201
|
*/
|
|
202
202
|
rotate(x, y, angle, asDegrees, dist = null) {
|
|
203
203
|
return rotate(this, x, y, angle, asDegrees, dist);
|
|
204
204
|
}
|
|
205
205
|
|
|
206
206
|
/**
|
|
207
|
-
*
|
|
208
|
-
* @returns {number}
|
|
207
|
+
* Calculates the magnitude (length) of this point from the origin.
|
|
208
|
+
* @returns {number} The magnitude of the point.
|
|
209
209
|
*/
|
|
210
210
|
getMagnitude() {
|
|
211
211
|
return Math.sqrt(this.x * this.x + this.y * this.y);
|
|
212
212
|
}
|
|
213
213
|
|
|
214
214
|
/**
|
|
215
|
-
*
|
|
216
|
-
* @returns {number}
|
|
215
|
+
* Calculates the square of the magnitude (length) of this point from the origin.
|
|
216
|
+
* @returns {number} The square of the magnitude of the point.
|
|
217
217
|
*/
|
|
218
218
|
getMagnitudeSq() {
|
|
219
219
|
return this.x * this.x + this.y * this.y;
|
|
220
220
|
}
|
|
221
221
|
|
|
222
222
|
/**
|
|
223
|
-
*
|
|
224
|
-
* @param {number} magnitude -
|
|
225
|
-
* @returns {Point}
|
|
223
|
+
* Sets the magnitude (length) of this point while preserving its direction.
|
|
224
|
+
* @param {number} magnitude - The new magnitude for the point.
|
|
225
|
+
* @returns {Point} This point instance for chaining.
|
|
226
226
|
*/
|
|
227
227
|
setMagnitude(magnitude) {
|
|
228
228
|
return this.normalize().multiply(magnitude, magnitude);
|
|
229
229
|
}
|
|
230
230
|
|
|
231
231
|
/**
|
|
232
|
-
*
|
|
233
|
-
* @returns {Point}
|
|
232
|
+
* Normalizes this point to have a magnitude of 1 while preserving its direction.
|
|
233
|
+
* @returns {Point} This point instance for chaining.
|
|
234
234
|
*/
|
|
235
235
|
normalize() {
|
|
236
236
|
if (!this.isZero()) {
|
|
@@ -242,74 +242,74 @@ export class Point {
|
|
|
242
242
|
}
|
|
243
243
|
|
|
244
244
|
/**
|
|
245
|
-
*
|
|
246
|
-
* @returns {boolean}
|
|
245
|
+
* Checks if this point has zero magnitude (is at the origin).
|
|
246
|
+
* @returns {boolean} True if both x and y coordinates are zero, false otherwise.
|
|
247
247
|
*/
|
|
248
248
|
isZero() {
|
|
249
249
|
return this.x === 0 && this.y === 0;
|
|
250
250
|
}
|
|
251
251
|
|
|
252
252
|
/**
|
|
253
|
-
*
|
|
254
|
-
* @param {Point} a -
|
|
255
|
-
* @returns {number}
|
|
253
|
+
* Calculates the dot product of this point and another point.
|
|
254
|
+
* @param {Point} a - The other point to calculate the dot product with.
|
|
255
|
+
* @returns {number} The dot product of the two points.
|
|
256
256
|
*/
|
|
257
257
|
dot(a) {
|
|
258
258
|
return this.x * a.x + this.y * a.y;
|
|
259
259
|
}
|
|
260
260
|
|
|
261
261
|
/**
|
|
262
|
-
*
|
|
263
|
-
* @param {Point} a -
|
|
264
|
-
* @returns {number}
|
|
262
|
+
* Calculates the cross product of this point and another point.
|
|
263
|
+
* @param {Point} a - The other point to calculate the cross product with.
|
|
264
|
+
* @returns {number} The cross product of the two points.
|
|
265
265
|
*/
|
|
266
266
|
cross(a) {
|
|
267
267
|
return this.x * a.y - this.y * a.x;
|
|
268
268
|
}
|
|
269
269
|
|
|
270
270
|
/**
|
|
271
|
-
*
|
|
272
|
-
* @returns {Point}
|
|
271
|
+
* Returns a perpendicular point (rotated 90 degrees counter-clockwise).
|
|
272
|
+
* @returns {Point} A new point that is perpendicular to this one.
|
|
273
273
|
*/
|
|
274
274
|
perp() {
|
|
275
275
|
return this.setTo(-this.y, this.x);
|
|
276
276
|
}
|
|
277
277
|
|
|
278
278
|
/**
|
|
279
|
-
*
|
|
280
|
-
* @returns {Point}
|
|
279
|
+
* Returns a perpendicular point (rotated 90 degrees clockwise).
|
|
280
|
+
* @returns {Point} A new point that is perpendicular to this one (rotated clockwise).
|
|
281
281
|
*/
|
|
282
282
|
rperp() {
|
|
283
283
|
return this.setTo(this.y, -this.x);
|
|
284
284
|
}
|
|
285
285
|
|
|
286
286
|
/**
|
|
287
|
-
*
|
|
288
|
-
* @returns {Point}
|
|
287
|
+
* Returns a point with the same direction as this one but with y coordinate negated.
|
|
288
|
+
* @returns {Point} A new point with the same x coordinate but negated y coordinate.
|
|
289
289
|
*/
|
|
290
290
|
normalRightHand() {
|
|
291
291
|
return this.setTo(this.y * -1, this.x);
|
|
292
292
|
}
|
|
293
293
|
|
|
294
294
|
/**
|
|
295
|
-
*
|
|
296
|
-
* @returns {Point}
|
|
295
|
+
* Returns a new point with the x and y coordinates rounded down to the nearest integer.
|
|
296
|
+
* @returns {Point} A new point with floored coordinates.
|
|
297
297
|
*/
|
|
298
298
|
floor() {
|
|
299
299
|
return this.setTo(Math.floor(this.x), Math.floor(this.y));
|
|
300
300
|
}
|
|
301
301
|
|
|
302
302
|
/**
|
|
303
|
-
*
|
|
304
|
-
* @returns {Point}
|
|
303
|
+
* Returns a new point with the x and y coordinates rounded up to the nearest integer.
|
|
304
|
+
* @returns {Point} A new point with ceiled coordinates.
|
|
305
305
|
*/
|
|
306
306
|
ceil() {
|
|
307
307
|
return this.setTo(Math.ceil(this.x), Math.ceil(this.y));
|
|
308
308
|
}
|
|
309
309
|
|
|
310
310
|
/**
|
|
311
|
-
*
|
|
312
|
-
* @returns {string}
|
|
311
|
+
* Returns a string representation of this point.
|
|
312
|
+
* @returns {string} A string representation of the point in the format "[Point (x=value y=value)]".
|
|
313
313
|
*/
|
|
314
314
|
toString() {
|
|
315
315
|
return `[{Point (x=${this.x} y=${this.y})}]`;
|
|
@@ -4,8 +4,8 @@ import { GEOM_POLYGON } from '../core/const.js';
|
|
|
4
4
|
|
|
5
5
|
export class Polygon {
|
|
6
6
|
/**
|
|
7
|
-
*
|
|
8
|
-
* @param {object[]} points -
|
|
7
|
+
* Creates a new Polygon instance.
|
|
8
|
+
* @param {object[]} points - The array of points to define the polygon (optional).
|
|
9
9
|
*/
|
|
10
10
|
constructor(points = null) {
|
|
11
11
|
/** @type {number} */
|
|
@@ -24,9 +24,9 @@ export class Polygon {
|
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
/**
|
|
27
|
-
*
|
|
28
|
-
* @param {number[]} output -
|
|
29
|
-
* @returns {number[]}
|
|
27
|
+
* Converts the polygon's points to a number array.
|
|
28
|
+
* @param {number[]} output - The array to store the result in (optional).
|
|
29
|
+
* @returns {number[]} An array of numbers representing the polygon's points.
|
|
30
30
|
*/
|
|
31
31
|
toNumberArray(output = []) {
|
|
32
32
|
for (let i = 0; i < this._points.length; i += 1) {
|
|
@@ -43,8 +43,8 @@ export class Polygon {
|
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
/**
|
|
46
|
-
*
|
|
47
|
-
* @returns {Polygon}
|
|
46
|
+
* Flattens the polygon's point array to a simple numeric array.
|
|
47
|
+
* @returns {Polygon} This polygon instance for chaining.
|
|
48
48
|
*/
|
|
49
49
|
flatten() {
|
|
50
50
|
this._points = this.toNumberArray();
|
|
@@ -53,18 +53,18 @@ export class Polygon {
|
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
/**
|
|
56
|
-
*
|
|
57
|
-
* @returns {Polygon}
|
|
56
|
+
* Creates a clone of this polygon.
|
|
57
|
+
* @returns {Polygon} A new polygon with the same values as this one.
|
|
58
58
|
*/
|
|
59
59
|
clone() {
|
|
60
60
|
return clone(this);
|
|
61
61
|
}
|
|
62
62
|
|
|
63
63
|
/**
|
|
64
|
-
*
|
|
65
|
-
* @param {number} x -
|
|
66
|
-
* @param {number} y -
|
|
67
|
-
* @returns {boolean}
|
|
64
|
+
* Checks if the specified point is contained within this polygon.
|
|
65
|
+
* @param {number} x - The x coordinate of the point to check.
|
|
66
|
+
* @param {number} y - The y coordinate of the point to check.
|
|
67
|
+
* @returns {boolean} True if the point is contained within this polygon, false otherwise.
|
|
68
68
|
*/
|
|
69
69
|
contains(x, y) {
|
|
70
70
|
// Adapted from http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html by Jonas Raoni Soares Silva
|
|
@@ -94,9 +94,9 @@ export class Polygon {
|
|
|
94
94
|
}
|
|
95
95
|
|
|
96
96
|
/**
|
|
97
|
-
*
|
|
98
|
-
* @param {object[]} points -
|
|
99
|
-
* @returns {Polygon}
|
|
97
|
+
* Sets the polygon's points to new values.
|
|
98
|
+
* @param {object[]} points - The array of points to define the polygon.
|
|
99
|
+
* @returns {Polygon} This polygon instance for chaining.
|
|
100
100
|
*/
|
|
101
101
|
setTo(points) {
|
|
102
102
|
this.area = 0;
|
|
@@ -133,9 +133,9 @@ export class Polygon {
|
|
|
133
133
|
}
|
|
134
134
|
|
|
135
135
|
/**
|
|
136
|
-
*
|
|
137
|
-
* @param {number} y0 -
|
|
138
|
-
* @returns {number}
|
|
136
|
+
* Calculates the area of this polygon.
|
|
137
|
+
* @param {number} y0 - The y coordinate of the lowest boundary (internal use).
|
|
138
|
+
* @returns {number} The area of this polygon.
|
|
139
139
|
*/
|
|
140
140
|
calculateArea(y0) {
|
|
141
141
|
let p1;
|
|
@@ -157,15 +157,15 @@ export class Polygon {
|
|
|
157
157
|
}
|
|
158
158
|
|
|
159
159
|
/**
|
|
160
|
-
*
|
|
161
|
-
* @returns {object[]}
|
|
160
|
+
* Gets the points of this polygon.
|
|
161
|
+
* @returns {object[]} The array of points that define this polygon.
|
|
162
162
|
*/
|
|
163
163
|
get points() {
|
|
164
164
|
return this._points;
|
|
165
165
|
}
|
|
166
166
|
|
|
167
167
|
/**
|
|
168
|
-
*
|
|
168
|
+
* Sets the points of this polygon.
|
|
169
169
|
*/
|
|
170
170
|
set points(value) {
|
|
171
171
|
if (value !== null) {
|