-
Notifications
You must be signed in to change notification settings - Fork 4
/
rect.js
276 lines (248 loc) · 6.13 KB
/
rect.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
/** @module rect */
import { vec2 } from "pex-math";
/**
* Creates a new rectangle.
* @returns {import("./types.js").rect}
*/
export function create() {
return [
[Infinity, Infinity],
[-Infinity, -Infinity],
];
}
/**
* Reset a rectangle.
* @param {import("./types.js").rect} a
* @returns {import("./types.js").rect}
*/
export function empty(a) {
a[0][0] = a[0][1] = Infinity;
a[1][0] = a[1][1] = -Infinity;
return a;
}
/**
* Copies a rectangle.
* @param {import("./types.js").rect} a
* @returns {import("./types.js").rect}
*/
export function copy(a) {
return [a[0].slice(), a[1].slice()];
}
/**
* Sets a rectangle to another.
* @param {import("./types.js").rect} a
* @param {import("./types.js").rect} b
* @returns {import("./types.js").rect}
*/
export function set(a, b) {
a[0][0] = b[0][0];
a[0][1] = b[0][1];
a[1][0] = b[1][0];
a[1][1] = b[1][1];
return a;
}
/**
* Checks if a rectangle is empty.
* @param {import("./types.js").rect} a
* @returns {boolean}
*/
export function isEmpty(a) {
return a[0][0] > a[1][0] || a[0][1] > a[1][1];
}
/**
* Updates a rectangle from a list of points.
* @param {import("./types.js").rect} a
* @param {import("./types.js").vec2[] | import("./types.js").TypedArray} points
* @returns {import("./types.js").rect}
*/
export function fromPoints(a, points) {
const isTypedArray = !Array.isArray(points);
for (let i = 0; i < points.length / (isTypedArray ? 2 : 1); i++) {
includePoint(a, isTypedArray ? points.slice(i * 2) : points[i]);
}
return a;
}
/**
* Returns a list of 4 points from a rectangle.
* @param {import("./types.js").rect} a
* @param {import("./types.js").vec2[]} points
* @returns {import("./types.js").vec2[]}
*/
export function getCorners(a, points = []) {
points[0] = a[0].slice();
points[1] = [a[0][1], a[1][0]];
points[2] = a[1].slice();
points[3] = [a[1][0], a[0][1]];
return points;
}
/**
* Scales a rectangle.
* @param {import("./types.js").rect} a
* @param {number} n
* @returns {import("./types.js").rect}
*/
export function scale(a, n) {
a[0][0] *= n;
a[0][1] *= n;
a[1][0] *= n;
a[1][1] *= n;
return a;
}
/**
* Sets the size of a rectangle using width and height.
* @param {import("./types.js").rect} a
* @param {import("./types.js").vec2} size
* @returns {import("./types.js").rect}
*/
export function setSize(a, size) {
a[1][0] = a[0][0] + size[0];
a[1][1] = a[0][1] + size[1];
return a;
}
/**
* Returns the size of a rectangle.
* @param {import("./types.js").rect} a
* @param {import("./types.js").vec2} out
* @returns {import("./types.js").vec2}
*/
export function size(a, out = []) {
out[0] = width(a);
out[1] = height(a);
return out;
}
/**
* Returns the width of a rectangle.
* @param {import("./types.js").rect} a
* @returns {number}
*/
export function width(a) {
return a[1][0] - a[0][0];
}
/**
* Returns the height of a rectangle.
* @param {import("./types.js").rect} a
* @returns {number}
*/
export function height(a) {
return a[1][1] - a[0][1];
}
/**
* Returns the aspect ratio of a rectangle.
* @param {import("./types.js").rect} a
* @returns {number}
*/
export function aspectRatio(a) {
return width(a) / height(a);
}
/**
* Sets the position of a rectangle.
* @param {import("./types.js").rect} a
* @param {import("./types.js").vec2} p
* @returns {import("./types.js").rect}
*/
export function setPosition(a, [x, y]) {
const w = width(a);
const h = height(a);
a[0][0] = x;
a[0][1] = y;
a[1][0] = x + w;
a[1][1] = y + h;
return a;
}
/**
* Returns the center of a rectangle.
* @param {import("./types.js").rect} a
* @param {import("./types.js").vec2} out
* @returns {import("./types.js").rect}
*/
export function center(a, out = []) {
out[0] = a[0][0] + width(a) * 0.5;
out[1] = a[0][1] + height(a) * 0.5;
return out;
}
/**
* Checks if a point is inside a rectangle.
* @param {import("./types.js").rect} a
* @param {import("./types.js").vec2} p
* @returns {boolean}
*/
export function containsPoint(a, [x, y]) {
return x >= a[0][0] && x <= a[1][0] && y >= a[0][1] && y <= a[1][1];
}
/**
* Checks if a rectangle is inside another rectangle.
* @param {import("./types.js").rect} a
* @param {import("./types.js").rect} b
* @returns {boolean}
*/
export function containsRect(a, b) {
return containsPoint(a, b[0]) && containsPoint(a, b[1]);
}
/**
* Includes a point in a rectangle.
* @param {import("./types.js").rect} a
* @param {import("./types.js").vec2} p
* @returns {import("./types.js").rect}
*/
export function includePoint(a, [x, y]) {
const minx = a[0][0];
const miny = a[0][1];
const maxx = a[1][0];
const maxy = a[1][1];
a[0][0] = minx > x ? x : minx;
a[0][1] = miny > y ? y : miny;
a[1][0] = maxx < x ? x : maxx;
a[1][1] = maxy < y ? y : maxy;
return a;
}
/**
* Includes a rectangle in another rectangle.
* @param {import("./types.js").rect} a
* @param {import("./types.js").rect} b
* @returns {import("./types.js").rect}
*/
export function includeRect(a, b) {
includePoint(a, b[0]);
includePoint(a, b[1]);
return a;
}
/**
* Maps a point into the dimensions of a rectangle.
* @param {import("./types.js").rect} a
* @param {import("./types.js").vec2} p
* @returns {import("./types.js").vec2}
*/
export function mapPoint(a, p) {
const minx = a[0][0];
const miny = a[0][1];
const maxx = a[1][0];
const maxy = a[1][1];
p[0] = Math.max(minx, Math.min(p[0], maxx)) - minx;
p[1] = Math.max(miny, Math.min(p[1], maxy)) - miny;
return p;
}
/**
* Clamps a point into the dimensions of a rectangle.
* @param {import("./types.js").rect} a
* @param {import("./types.js").vec2} p
* @returns {import("./types.js").vec2}
*/
export function clampPoint(a, p) {
const minx = a[0][0];
const miny = a[0][1];
const maxx = a[1][0];
const maxy = a[1][1];
p[0] = Math.max(minx, Math.min(p[0], maxx));
p[1] = Math.max(miny, Math.min(p[1], maxy));
return p;
}
/**
* Prints a rect to a string.
* @param {import("./types.js").rect} a
* @param {number} [precision=4]
* @returns {string}
*/
export function toString(a, precision = 4) {
// prettier-ignore
return `[${vec2.toString(a[0], precision)}, ${vec2.toString(a[1], precision)}]`;
}