@vpmedia/phaser 1.94.0 → 1.96.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 +41 -0
- package/package.json +11 -11
- package/src/phaser/core/animation.js +79 -56
- package/src/phaser/core/animation_manager.js +55 -55
- package/src/phaser/core/animation_parser.js +2 -2
- 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 +35 -19
- package/src/phaser/core/input_pointer.js +18 -0
- package/src/phaser/core/loader.js +171 -171
- package/src/phaser/core/loader_parser.js +22 -22
- package/src/phaser/core/raf.js +1 -1
- package/src/phaser/core/scale_manager.js +3 -2
- package/src/phaser/core/scene.js +16 -10
- package/src/phaser/core/scene_manager.js +51 -43
- package/src/phaser/core/signal.js +56 -52
- 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 +54 -30
- package/src/phaser/core/timer.js +82 -64
- package/src/phaser/core/timer_event.js +9 -9
- package/src/phaser/core/tween.js +106 -90
- package/src/phaser/core/tween_data.js +30 -30
- package/src/phaser/core/tween_manager.js +32 -24
- package/src/phaser/core/world.js +4 -3
- package/src/phaser/display/bitmap_text.js +72 -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 +17 -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 +129 -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 +63 -50
- package/src/phaser/display/image.js +68 -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 +217 -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 +21 -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 +74 -69
- package/src/phaser/geom/ellipse.js +33 -28
- package/src/phaser/geom/line.js +96 -93
- package/src/phaser/geom/matrix.js +61 -54
- package/src/phaser/geom/point.js +99 -96
- package/src/phaser/geom/polygon.js +27 -22
- package/src/phaser/geom/rectangle.js +137 -132
- package/src/phaser/geom/rounded_rectangle.js +12 -12
- package/src/phaser/geom/util/circle.js +33 -33
- package/src/phaser/geom/util/ellipse.js +5 -5
- package/src/phaser/geom/util/line.js +26 -26
- package/src/phaser/geom/util/matrix.js +8 -8
- package/src/phaser/geom/util/point.js +97 -97
- package/src/phaser/geom/util/polygon.js +4 -4
- package/src/phaser/geom/util/rectangle.js +74 -74
- package/src/phaser/geom/util/rounded_rectangle.js +4 -4
- package/src/phaser/util/math.js +81 -81
- package/types/phaser/core/animation.d.ts +79 -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/animation_parser.d.ts +3 -2
- package/types/phaser/core/animation_parser.d.ts.map +1 -1
- 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 +51 -44
- package/types/phaser/core/dom.d.ts.map +1 -1
- 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 +54 -21
- package/types/phaser/core/game.d.ts.map +1 -1
- package/types/phaser/core/input_pointer.d.ts +18 -0
- package/types/phaser/core/input_pointer.d.ts.map +1 -1
- package/types/phaser/core/loader.d.ts +175 -172
- package/types/phaser/core/loader.d.ts.map +1 -1
- package/types/phaser/core/loader_parser.d.ts.map +1 -1
- package/types/phaser/core/raf.d.ts +1 -1
- package/types/phaser/core/scale_manager.d.ts +6 -5
- package/types/phaser/core/scale_manager.d.ts.map +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 +54 -30
- package/types/phaser/core/time.d.ts.map +1 -1
- package/types/phaser/core/timer.d.ts +82 -64
- package/types/phaser/core/timer.d.ts.map +1 -1
- package/types/phaser/core/timer_event.d.ts +9 -9
- package/types/phaser/core/tween.d.ts +106 -90
- package/types/phaser/core/tween.d.ts.map +1 -1
- package/types/phaser/core/tween_data.d.ts +30 -30
- package/types/phaser/core/tween_manager.d.ts +35 -67
- 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 +76 -53
- 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/graphics.d.ts +3 -3
- package/types/phaser/display/canvas/graphics.d.ts.map +1 -1
- package/types/phaser/display/canvas/pool.d.ts +6 -3
- package/types/phaser/display/canvas/pool.d.ts.map +1 -1
- package/types/phaser/display/canvas/renderer.d.ts +18 -17
- package/types/phaser/display/canvas/renderer.d.ts.map +1 -1
- package/types/phaser/display/canvas/util.d.ts +6 -6
- package/types/phaser/display/canvas/util.d.ts.map +1 -1
- 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 +118 -102
- package/types/phaser/display/graphics.d.ts.map +1 -1
- package/types/phaser/display/graphics_data.d.ts +10 -10
- package/types/phaser/display/group.d.ts +59 -47
- package/types/phaser/display/group.d.ts.map +1 -1
- package/types/phaser/display/image.d.ts +58 -45
- package/types/phaser/display/image.d.ts.map +1 -1
- package/types/phaser/display/text.d.ts +237 -188
- 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 +10 -10
- package/types/phaser/display/webgl/base_texture.d.ts.map +1 -1
- 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.d.ts +2 -2
- package/types/phaser/display/webgl/graphics.d.ts.map +1 -1
- 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 +21 -20
- package/types/phaser/display/webgl/renderer.d.ts.map +1 -1
- 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/display/webgl/texture_util.d.ts +4 -2
- package/types/phaser/display/webgl/texture_util.d.ts.map +1 -1
- package/types/phaser/geom/circle.d.ts +74 -69
- package/types/phaser/geom/circle.d.ts.map +1 -1
- package/types/phaser/geom/ellipse.d.ts +33 -28
- package/types/phaser/geom/ellipse.d.ts.map +1 -1
- package/types/phaser/geom/line.d.ts +96 -93
- package/types/phaser/geom/line.d.ts.map +1 -1
- package/types/phaser/geom/matrix.d.ts +61 -54
- package/types/phaser/geom/matrix.d.ts.map +1 -1
- package/types/phaser/geom/point.d.ts +99 -96
- package/types/phaser/geom/point.d.ts.map +1 -1
- package/types/phaser/geom/polygon.d.ts +29 -23
- package/types/phaser/geom/polygon.d.ts.map +1 -1
- package/types/phaser/geom/rectangle.d.ts +137 -132
- package/types/phaser/geom/rectangle.d.ts.map +1 -1
- package/types/phaser/geom/rounded_rectangle.d.ts +12 -12
- package/types/phaser/geom/util/point.d.ts +1 -1
- package/types/phaser/geom/util/point.d.ts.map +1 -1
- package/pnpm-workspace.yaml +0 -4
package/src/phaser/geom/point.js
CHANGED
|
@@ -3,38 +3,41 @@ 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
|
+
/** @type {number} */
|
|
11
12
|
this.x = x;
|
|
13
|
+
/** @type {number} */
|
|
12
14
|
this.y = y;
|
|
15
|
+
/** @type {number} */
|
|
13
16
|
this.type = GEOM_POINT;
|
|
14
17
|
}
|
|
15
18
|
|
|
16
19
|
/**
|
|
17
|
-
*
|
|
18
|
-
* @param {Point} source -
|
|
19
|
-
* @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.
|
|
20
23
|
*/
|
|
21
24
|
copyFrom(source) {
|
|
22
25
|
return this.setTo(source.x, source.y);
|
|
23
26
|
}
|
|
24
27
|
|
|
25
28
|
/**
|
|
26
|
-
*
|
|
27
|
-
* @returns {Point}
|
|
29
|
+
* Returns a new point with the x and y coordinates swapped.
|
|
30
|
+
* @returns {Point} A new point instance with swapped coordinates.
|
|
28
31
|
*/
|
|
29
32
|
invert() {
|
|
30
33
|
return this.setTo(this.y, this.x);
|
|
31
34
|
}
|
|
32
35
|
|
|
33
36
|
/**
|
|
34
|
-
*
|
|
35
|
-
* @param {number} x -
|
|
36
|
-
* @param {number} y -
|
|
37
|
-
* @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.
|
|
38
41
|
*/
|
|
39
42
|
setTo(x, y) {
|
|
40
43
|
this.x = x || 0;
|
|
@@ -43,11 +46,11 @@ export class Point {
|
|
|
43
46
|
}
|
|
44
47
|
|
|
45
48
|
/**
|
|
46
|
-
*
|
|
47
|
-
* @param {number} x -
|
|
48
|
-
* @param {number} y -
|
|
49
|
-
* @returns {Point}
|
|
50
|
-
* @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.
|
|
51
54
|
*/
|
|
52
55
|
set(x, y) {
|
|
53
56
|
this.x = x || 0;
|
|
@@ -56,10 +59,10 @@ export class Point {
|
|
|
56
59
|
}
|
|
57
60
|
|
|
58
61
|
/**
|
|
59
|
-
*
|
|
60
|
-
* @param {number} x -
|
|
61
|
-
* @param {number} y -
|
|
62
|
-
* @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.
|
|
63
66
|
*/
|
|
64
67
|
add(x, y) {
|
|
65
68
|
this.x += x;
|
|
@@ -68,10 +71,10 @@ export class Point {
|
|
|
68
71
|
}
|
|
69
72
|
|
|
70
73
|
/**
|
|
71
|
-
*
|
|
72
|
-
* @param {number} x -
|
|
73
|
-
* @param {number} y -
|
|
74
|
-
* @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.
|
|
75
78
|
*/
|
|
76
79
|
subtract(x, y) {
|
|
77
80
|
this.x -= x;
|
|
@@ -80,10 +83,10 @@ export class Point {
|
|
|
80
83
|
}
|
|
81
84
|
|
|
82
85
|
/**
|
|
83
|
-
*
|
|
84
|
-
* @param {number} x -
|
|
85
|
-
* @param {number} y -
|
|
86
|
-
* @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.
|
|
87
90
|
*/
|
|
88
91
|
multiply(x, y) {
|
|
89
92
|
this.x *= x;
|
|
@@ -92,10 +95,10 @@ export class Point {
|
|
|
92
95
|
}
|
|
93
96
|
|
|
94
97
|
/**
|
|
95
|
-
*
|
|
96
|
-
* @param {number} x -
|
|
97
|
-
* @param {number} y -
|
|
98
|
-
* @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.
|
|
99
102
|
*/
|
|
100
103
|
divide(x, y) {
|
|
101
104
|
this.x /= x;
|
|
@@ -104,10 +107,10 @@ export class Point {
|
|
|
104
107
|
}
|
|
105
108
|
|
|
106
109
|
/**
|
|
107
|
-
*
|
|
108
|
-
* @param {number} min -
|
|
109
|
-
* @param {number} max -
|
|
110
|
-
* @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.
|
|
111
114
|
*/
|
|
112
115
|
clampX(min, max) {
|
|
113
116
|
this.x = Math.max(min, Math.min(max, this.x));
|
|
@@ -115,10 +118,10 @@ export class Point {
|
|
|
115
118
|
}
|
|
116
119
|
|
|
117
120
|
/**
|
|
118
|
-
*
|
|
119
|
-
* @param {number} min -
|
|
120
|
-
* @param {number} max -
|
|
121
|
-
* @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.
|
|
122
125
|
*/
|
|
123
126
|
clampY(min, max) {
|
|
124
127
|
this.y = Math.max(min, Math.min(max, this.y));
|
|
@@ -126,10 +129,10 @@ export class Point {
|
|
|
126
129
|
}
|
|
127
130
|
|
|
128
131
|
/**
|
|
129
|
-
*
|
|
130
|
-
* @param {number} min -
|
|
131
|
-
* @param {number} max -
|
|
132
|
-
* @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.
|
|
133
136
|
*/
|
|
134
137
|
clamp(min, max) {
|
|
135
138
|
this.x = Math.max(min, Math.min(max, this.x));
|
|
@@ -138,17 +141,17 @@ export class Point {
|
|
|
138
141
|
}
|
|
139
142
|
|
|
140
143
|
/**
|
|
141
|
-
*
|
|
142
|
-
* @returns {Point}
|
|
144
|
+
* Creates a clone of this point.
|
|
145
|
+
* @returns {Point} A new point with the same coordinates as this one.
|
|
143
146
|
*/
|
|
144
147
|
clone() {
|
|
145
148
|
return clone(this);
|
|
146
149
|
}
|
|
147
150
|
|
|
148
151
|
/**
|
|
149
|
-
*
|
|
150
|
-
* @param {Point} dest -
|
|
151
|
-
* @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.
|
|
152
155
|
*/
|
|
153
156
|
copyTo(dest) {
|
|
154
157
|
dest.x = this.x;
|
|
@@ -157,28 +160,28 @@ export class Point {
|
|
|
157
160
|
}
|
|
158
161
|
|
|
159
162
|
/**
|
|
160
|
-
*
|
|
161
|
-
* @param {Point} b -
|
|
162
|
-
* @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.
|
|
163
166
|
*/
|
|
164
167
|
distance(b) {
|
|
165
168
|
return distance(this, b);
|
|
166
169
|
}
|
|
167
170
|
|
|
168
171
|
/**
|
|
169
|
-
*
|
|
170
|
-
* @param {Point} a -
|
|
171
|
-
* @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.
|
|
172
175
|
*/
|
|
173
176
|
equals(a) {
|
|
174
177
|
return a.x === this.x && a.y === this.y;
|
|
175
178
|
}
|
|
176
179
|
|
|
177
180
|
/**
|
|
178
|
-
*
|
|
179
|
-
* @param {Point} a -
|
|
180
|
-
* @param {boolean} asDegrees -
|
|
181
|
-
* @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.
|
|
182
185
|
*/
|
|
183
186
|
angle(a, asDegrees = false) {
|
|
184
187
|
if (asDegrees) {
|
|
@@ -188,46 +191,46 @@ export class Point {
|
|
|
188
191
|
}
|
|
189
192
|
|
|
190
193
|
/**
|
|
191
|
-
*
|
|
192
|
-
* @param {number} x -
|
|
193
|
-
* @param {number} y -
|
|
194
|
-
* @param {number} angle -
|
|
195
|
-
* @param {boolean} asDegrees -
|
|
196
|
-
* @param {number | null | undefined} dist -
|
|
197
|
-
* @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.
|
|
198
201
|
*/
|
|
199
202
|
rotate(x, y, angle, asDegrees, dist = null) {
|
|
200
203
|
return rotate(this, x, y, angle, asDegrees, dist);
|
|
201
204
|
}
|
|
202
205
|
|
|
203
206
|
/**
|
|
204
|
-
*
|
|
205
|
-
* @returns {number}
|
|
207
|
+
* Calculates the magnitude (length) of this point from the origin.
|
|
208
|
+
* @returns {number} The magnitude of the point.
|
|
206
209
|
*/
|
|
207
210
|
getMagnitude() {
|
|
208
211
|
return Math.sqrt(this.x * this.x + this.y * this.y);
|
|
209
212
|
}
|
|
210
213
|
|
|
211
214
|
/**
|
|
212
|
-
*
|
|
213
|
-
* @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.
|
|
214
217
|
*/
|
|
215
218
|
getMagnitudeSq() {
|
|
216
219
|
return this.x * this.x + this.y * this.y;
|
|
217
220
|
}
|
|
218
221
|
|
|
219
222
|
/**
|
|
220
|
-
*
|
|
221
|
-
* @param {number} magnitude -
|
|
222
|
-
* @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.
|
|
223
226
|
*/
|
|
224
227
|
setMagnitude(magnitude) {
|
|
225
228
|
return this.normalize().multiply(magnitude, magnitude);
|
|
226
229
|
}
|
|
227
230
|
|
|
228
231
|
/**
|
|
229
|
-
*
|
|
230
|
-
* @returns {Point}
|
|
232
|
+
* Normalizes this point to have a magnitude of 1 while preserving its direction.
|
|
233
|
+
* @returns {Point} This point instance for chaining.
|
|
231
234
|
*/
|
|
232
235
|
normalize() {
|
|
233
236
|
if (!this.isZero()) {
|
|
@@ -239,74 +242,74 @@ export class Point {
|
|
|
239
242
|
}
|
|
240
243
|
|
|
241
244
|
/**
|
|
242
|
-
*
|
|
243
|
-
* @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.
|
|
244
247
|
*/
|
|
245
248
|
isZero() {
|
|
246
249
|
return this.x === 0 && this.y === 0;
|
|
247
250
|
}
|
|
248
251
|
|
|
249
252
|
/**
|
|
250
|
-
*
|
|
251
|
-
* @param {Point} a -
|
|
252
|
-
* @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.
|
|
253
256
|
*/
|
|
254
257
|
dot(a) {
|
|
255
258
|
return this.x * a.x + this.y * a.y;
|
|
256
259
|
}
|
|
257
260
|
|
|
258
261
|
/**
|
|
259
|
-
*
|
|
260
|
-
* @param {Point} a -
|
|
261
|
-
* @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.
|
|
262
265
|
*/
|
|
263
266
|
cross(a) {
|
|
264
267
|
return this.x * a.y - this.y * a.x;
|
|
265
268
|
}
|
|
266
269
|
|
|
267
270
|
/**
|
|
268
|
-
*
|
|
269
|
-
* @returns {Point}
|
|
271
|
+
* Returns a perpendicular point (rotated 90 degrees counter-clockwise).
|
|
272
|
+
* @returns {Point} A new point that is perpendicular to this one.
|
|
270
273
|
*/
|
|
271
274
|
perp() {
|
|
272
275
|
return this.setTo(-this.y, this.x);
|
|
273
276
|
}
|
|
274
277
|
|
|
275
278
|
/**
|
|
276
|
-
*
|
|
277
|
-
* @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).
|
|
278
281
|
*/
|
|
279
282
|
rperp() {
|
|
280
283
|
return this.setTo(this.y, -this.x);
|
|
281
284
|
}
|
|
282
285
|
|
|
283
286
|
/**
|
|
284
|
-
*
|
|
285
|
-
* @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.
|
|
286
289
|
*/
|
|
287
290
|
normalRightHand() {
|
|
288
291
|
return this.setTo(this.y * -1, this.x);
|
|
289
292
|
}
|
|
290
293
|
|
|
291
294
|
/**
|
|
292
|
-
*
|
|
293
|
-
* @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.
|
|
294
297
|
*/
|
|
295
298
|
floor() {
|
|
296
299
|
return this.setTo(Math.floor(this.x), Math.floor(this.y));
|
|
297
300
|
}
|
|
298
301
|
|
|
299
302
|
/**
|
|
300
|
-
*
|
|
301
|
-
* @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.
|
|
302
305
|
*/
|
|
303
306
|
ceil() {
|
|
304
307
|
return this.setTo(Math.ceil(this.x), Math.ceil(this.y));
|
|
305
308
|
}
|
|
306
309
|
|
|
307
310
|
/**
|
|
308
|
-
*
|
|
309
|
-
* @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)]".
|
|
310
313
|
*/
|
|
311
314
|
toString() {
|
|
312
315
|
return `[{Point (x=${this.x} y=${this.y})}]`;
|
|
@@ -4,14 +4,19 @@ 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
|
+
/** @type {number} */
|
|
11
12
|
this.area = 0;
|
|
13
|
+
/** @type {Point[]} */
|
|
12
14
|
this._points = [];
|
|
15
|
+
/** @type {boolean} */
|
|
13
16
|
this.closed = true;
|
|
17
|
+
/** @type {boolean} */
|
|
14
18
|
this.flattened = false;
|
|
19
|
+
/** @type {number} */
|
|
15
20
|
this.type = GEOM_POLYGON;
|
|
16
21
|
if (points) {
|
|
17
22
|
this.setTo(points);
|
|
@@ -19,9 +24,9 @@ export class Polygon {
|
|
|
19
24
|
}
|
|
20
25
|
|
|
21
26
|
/**
|
|
22
|
-
*
|
|
23
|
-
* @param {number[]} output -
|
|
24
|
-
* @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.
|
|
25
30
|
*/
|
|
26
31
|
toNumberArray(output = []) {
|
|
27
32
|
for (let i = 0; i < this._points.length; i += 1) {
|
|
@@ -38,8 +43,8 @@ export class Polygon {
|
|
|
38
43
|
}
|
|
39
44
|
|
|
40
45
|
/**
|
|
41
|
-
*
|
|
42
|
-
* @returns {Polygon}
|
|
46
|
+
* Flattens the polygon's point array to a simple numeric array.
|
|
47
|
+
* @returns {Polygon} This polygon instance for chaining.
|
|
43
48
|
*/
|
|
44
49
|
flatten() {
|
|
45
50
|
this._points = this.toNumberArray();
|
|
@@ -48,18 +53,18 @@ export class Polygon {
|
|
|
48
53
|
}
|
|
49
54
|
|
|
50
55
|
/**
|
|
51
|
-
*
|
|
52
|
-
* @returns {Polygon}
|
|
56
|
+
* Creates a clone of this polygon.
|
|
57
|
+
* @returns {Polygon} A new polygon with the same values as this one.
|
|
53
58
|
*/
|
|
54
59
|
clone() {
|
|
55
60
|
return clone(this);
|
|
56
61
|
}
|
|
57
62
|
|
|
58
63
|
/**
|
|
59
|
-
*
|
|
60
|
-
* @param {number} x -
|
|
61
|
-
* @param {number} y -
|
|
62
|
-
* @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.
|
|
63
68
|
*/
|
|
64
69
|
contains(x, y) {
|
|
65
70
|
// Adapted from http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html by Jonas Raoni Soares Silva
|
|
@@ -89,9 +94,9 @@ export class Polygon {
|
|
|
89
94
|
}
|
|
90
95
|
|
|
91
96
|
/**
|
|
92
|
-
*
|
|
93
|
-
* @param {object[]} points -
|
|
94
|
-
* @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.
|
|
95
100
|
*/
|
|
96
101
|
setTo(points) {
|
|
97
102
|
this.area = 0;
|
|
@@ -128,9 +133,9 @@ export class Polygon {
|
|
|
128
133
|
}
|
|
129
134
|
|
|
130
135
|
/**
|
|
131
|
-
*
|
|
132
|
-
* @param {number} y0 -
|
|
133
|
-
* @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.
|
|
134
139
|
*/
|
|
135
140
|
calculateArea(y0) {
|
|
136
141
|
let p1;
|
|
@@ -152,15 +157,15 @@ export class Polygon {
|
|
|
152
157
|
}
|
|
153
158
|
|
|
154
159
|
/**
|
|
155
|
-
*
|
|
156
|
-
* @returns {object[]}
|
|
160
|
+
* Gets the points of this polygon.
|
|
161
|
+
* @returns {object[]} The array of points that define this polygon.
|
|
157
162
|
*/
|
|
158
163
|
get points() {
|
|
159
164
|
return this._points;
|
|
160
165
|
}
|
|
161
166
|
|
|
162
167
|
/**
|
|
163
|
-
*
|
|
168
|
+
* Sets the points of this polygon.
|
|
164
169
|
*/
|
|
165
170
|
set points(value) {
|
|
166
171
|
if (value !== null) {
|