mh-two-thousand-and-two
2024-03-25 b8c93990f3fa5e50a8aca16bdc9c2758168aa0fd
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
import { MatrixArray } from './matrix';
 
export interface PointLike {
    x: number
    y: number
}
export default class Point {
 
    x: number
 
    y: number
 
    constructor(x?: number, y?: number) {
        this.x = x || 0;
        this.y = y || 0;
    }
 
    /**
     * Copy from another point
     */
    copy(other: PointLike) {
        this.x = other.x;
        this.y = other.y;
        return this;
    }
 
    /**
     * Clone a point
     */
    clone() {
        return new Point(this.x, this.y);
    }
 
    /**
     * Set x and y
     */
    set(x: number, y: number) {
        this.x = x;
        this.y = y;
        return this;
    }
 
    /**
     * If equal to another point
     */
    equal(other: PointLike) {
        return other.x === this.x && other.y === this.y;
    }
 
    /**
     * Add another point
     */
    add(other: PointLike) {
        this.x += other.x;
        this.y += other.y;
        return this;
    }
 
    scale(scalar: number) {
        this.x *= scalar;
        this.y *= scalar;
    }
 
    scaleAndAdd(other: PointLike, scalar: number) {
        this.x += other.x * scalar;
        this.y += other.y * scalar;
    }
 
    /**
     * Sub another point
     */
    sub(other: PointLike) {
        this.x -= other.x;
        this.y -= other.y;
        return this;
    }
 
    /**
     * Dot product with other point
     */
    dot(other: PointLike) {
        return this.x * other.x + this.y * other.y;
    }
 
    /**
     * Get length of point
     */
    len() {
        return Math.sqrt(this.x * this.x + this.y * this.y);
    }
 
    /**
     * Get squared length
     */
    lenSquare() {
        return this.x * this.x + this.y * this.y;
    }
 
    /**
     * Normalize
     */
    normalize() {
        const len = this.len();
        this.x /= len;
        this.y /= len;
        return this;
    }
 
    /**
     * Distance to another point
     */
    distance(other: PointLike) {
        const dx = this.x - other.x;
        const dy = this.y - other.y;
        return Math.sqrt(dx * dx + dy * dy);
    }
 
    /**
     * Square distance to another point
     */
    distanceSquare(other: Point) {
        const dx = this.x - other.x;
        const dy = this.y - other.y;
        return dx * dx + dy * dy;
    }
 
    /**
     * Negate
     */
    negate() {
        this.x = -this.x;
        this.y = -this.y;
        return this;
    }
 
    /**
     * Apply a transform matrix array.
     */
    transform(m: MatrixArray) {
        if (!m) {
            return;
        }
        const x = this.x;
        const y = this.y;
        this.x = m[0] * x + m[2] * y + m[4];
        this.y = m[1] * x + m[3] * y + m[5];
        return this;
    }
 
    toArray(out: number[]) {
        out[0] = this.x;
        out[1] = this.y;
        return out;
    }
 
    fromArray(input: number[]) {
        this.x = input[0];
        this.y = input[1];
    }
 
    static set(p: PointLike, x: number, y: number) {
        p.x = x;
        p.y = y;
    }
 
    static copy(p: PointLike, p2: PointLike) {
        p.x = p2.x;
        p.y = p2.y;
    }
 
    static len(p: PointLike) {
        return Math.sqrt(p.x * p.x + p.y * p.y);
    }
 
    static lenSquare(p: PointLike) {
        return p.x * p.x + p.y * p.y;
    }
 
    static dot(p0: PointLike, p1: PointLike) {
        return p0.x * p1.x + p0.y * p1.y;
    }
 
    static add(out: PointLike, p0: PointLike, p1: PointLike) {
        out.x = p0.x + p1.x;
        out.y = p0.y + p1.y;
    }
 
    static sub(out: PointLike, p0: PointLike, p1: PointLike) {
        out.x = p0.x - p1.x;
        out.y = p0.y - p1.y;
    }
 
    static scale(out: PointLike, p0: PointLike, scalar: number) {
        out.x = p0.x * scalar;
        out.y = p0.y * scalar;
    }
 
    static scaleAndAdd(out: PointLike, p0: PointLike, p1: PointLike, scalar: number) {
        out.x = p0.x + p1.x * scalar;
        out.y = p0.y + p1.y * scalar;
    }
 
    static lerp(out: PointLike, p0: PointLike, p1: PointLike, t: number) {
        const onet = 1 - t;
        out.x = onet * p0.x + t * p1.x;
        out.y = onet * p0.y + t * p1.y;
    }
}