-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjects.test.js
303 lines (220 loc) · 7.99 KB
/
objects.test.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
import { describe, it, expect } from "vitest";
import { Point, Line } from "./objects";
import { PGA_MATCHERS } from "./pga_matchers";
import { PI } from "../sketchlib/math_consts";
expect.extend(PGA_MATCHERS);
describe("Point", () => {
describe("euclidean", () => {
it("converts to direction", () => {
const a = Point.point(2, -5);
const result = a.to_direction();
const expected = Point.direction(2, -5);
expect(result).toBePoint(expected);
});
it("gets the underlying x and y components", () => {
const a = Point.point(2, -5);
expect(a.x).toBe(2);
expect(a.y).toBe(-5);
});
it("adding two points returns the midpoint", () => {
const a = Point.point(1, 2);
const b = Point.point(3, 4);
const result = a.add(b);
const expected = Point.point(2, 3);
expect(result).toEqual(expected);
});
it("adding a direction returns the correct point", () => {
const a = Point.point(1, 2);
const dir = Point.direction(3, 4);
const result = a.add(dir);
const expected = Point.point(4, 6);
expect(result).toBePoint(expected);
});
it("subtracting points produces the correct direction", () => {
const a = Point.point(1, 4);
const b = Point.point(3, 2);
const result = a.sub(b);
const expected = Point.direction(-2, 2);
expect(result).toBePoint(expected);
});
it("joining two points gives the line through them", () => {
const a = Point.point(0, 1);
const b = Point.point(1, 0);
const result = a.join(b);
const expected = new Line(1, 1, 1);
expect(result).toBeLine(expected);
});
it("swapping join arguments reverses the line's orientation", () => {
const a = Point.point(0, 1);
const b = Point.point(1, 0);
const result_forward = a.join(b);
const result_backward = b.join(a);
const expected_forward = new Line(1, 1, 1);
const expected_backward = new Line(-1, -1, -1);
expect(result_forward).toBeLine(expected_forward);
expect(result_backward).toBeLine(expected_backward);
});
it("lerp interpolates two points", () => {
const a = Point.point(1, 2);
const b = Point.point(-2, -8);
const result = Point.lerp(a, b, 0.25);
// 3/4 * 1 + 1/4 * -2 = 1/4(3 -2) = 1/4
// 3/4 * 2 + 1/4 * -8 = 1/4(6 - 8) = -2/4 = -1/2
const expected = Point.point(0.25, -0.5);
expect(result).toEqual(expected);
});
it("toString formats as point", () => {
const a = Point.point(0.00012345, 2.98763);
const result = a.toString();
const expected = "Point(0.000123, 2.99)";
expect(result).toBe(expected);
});
});
describe("direction", () => {
it("converts to point", () => {
const a = Point.direction(2, -5);
const result = a.to_point();
const expected = Point.point(2, -5);
expect(result).toBePoint(expected);
});
it("dir_from_angle computes cosine and sine", () => {
const angle = (2 * PI) / 3;
const result = Point.dir_from_angle(angle);
const expected = Point.direction(-0.5, Math.sqrt(3) / 2);
expect(result).toBePoint(expected);
});
it("gets the underlying x and y components", () => {
const a = Point.direction(-3, 5);
expect(a.x).toBe(-3);
expect(a.y).toBe(5);
});
it("dual returns the orthogonal line", () => {
const a = Point.direction(2, 1);
const result = a.dual();
const expected = new Line(2, 1, 0);
expect(result).toBeLine(expected);
});
it("neg negates the components", () => {
const a = Point.direction(1, -3);
const result = a.neg();
const expected = Point.direction(-1, 3);
expect(result).toBePoint(expected);
});
it("ideal norm returns the magnitude of x and y components", () => {
const a = Point.direction(3, 4);
const result = a.ideal_norm_sqr();
// 3^2 + 4^2
expect(result).toBe(25);
});
it("ideal magnitude returns the magnitude of x and y components", () => {
const a = Point.direction(3, 4);
const result = a.ideal_norm();
// sqrt(3^2 + 4^2) = sqrt(25) = 5
expect(result).toBeCloseTo(5);
});
it("limit_length with short direction does not change vector", () => {
const a = Point.direction(1, 2);
const max_length = 100;
const result = a.limit_length(max_length);
expect(result).toEqual(a);
});
it("limit_length with long direction snaps to max length", () => {
const a = Point.direction(300, 400);
const max_length = 100;
const result = a.limit_length(max_length);
// original vector has magnitude 500 (3-4-5 triangle scaled by 100)
// the new magnitude is 100, which is 1/5 exactly.
// 300 / 5 = 60
// 400 / 5 = 80
const expected = Point.direction(60, 80);
expect(result).toEqual(expected);
});
it("set_length with zero length direction throws error", () => {
const a = Point.ZERO;
const length = 100;
expect(() => {
a.set_length(length);
}).toThrowError("null vector");
});
it("set_length with short direction snaps to length", () => {
const a = Point.direction(3, 4);
const length = 100;
const result = a.set_length(length);
// original vector has magnitude 5 (3-4-5 triangle)
// the new magnitude is 100, which is 1/5 exactly.
// 300 / 5 = 60
// 400 / 5 = 80
const expected = Point.direction(60, 80);
expect(result).toBePoint(expected);
});
it("set_length with long direction snaps to max length", () => {
const a = Point.direction(300, 400);
const max_length = 100;
const result = a.set_length(max_length);
// original vector has magnitude 500 (3-4-5 triangle scaled by 100)
// the new magnitude is 100, which is 1/5 exactly.
// 300 / 5 = 60
// 400 / 5 = 80
const expected = Point.direction(60, 80);
expect(result).toBePoint(expected);
});
it("scale performs scalar multiplication", () => {
const dir = Point.direction(4, -3);
const result = dir.scale(2);
const expected = Point.direction(8, -6);
expect(result).toBePoint(expected);
});
it("dot of two directions computes the dot product of components", () => {
const a = Point.direction(1, 2);
const b = Point.direction(3, 4);
const result = a.dot(b);
// 1 * 3 + 2 * 4 = 3 + 8 = 11
const expected = 11;
expect(result).toBe(expected);
});
});
it("lerp interpolates two directions", () => {
const a = Point.direction(1, 2);
const b = Point.direction(-2, -8);
const result = Point.lerp(a, b, 0.25);
// 3/4 * 1 + 1/4 * -2 = 1/4(3 -2) = 1/4
// 3/4 * 2 + 1/4 * -8 = 1/4(6 - 8) = -2/4 = -1/2
const expected = Point.direction(0.25, -0.5);
expect(result).toBePoint(expected);
});
});
describe("Line", () => {
it("constructor normalizes Euclidean line", () => {
const line = new Line(3, 4, 5);
expect(line.is_infinite).toBe(false);
expect(line.nx).toBe(3 / 5);
expect(line.ny).toBe(4 / 5);
expect(line.d).toBe(1);
});
it("constructor doesn't modify line at infinity", () => {
const line = new Line(0, 0, 42);
expect(line.is_infinite).toBe(true);
expect(line.nx).toBe(0);
expect(line.ny).toBe(0);
expect(line.d).toBe(42);
});
it("meet of axes returns origin", () => {
const a = Line.X_AXIS;
const b = Line.Y_AXIS;
const result = a.meet(b);
expect(result).toBePoint(Point.ORIGIN);
});
it("meet of two lines returns their intersection", () => {
const a = new Line(1, 1, 1);
const b = new Line(1, -1, 2);
const result = a.meet(b);
const expected = Point.point(1.5, -0.5);
expect(result).toBePoint(expected);
});
it("toString formats as direction", () => {
const a = Point.direction(0.00012345, 2.98763);
const result = a.toString();
const expected = "Direction(0.000123, 2.99)";
expect(result).toBe(expected);
});
});