-
Notifications
You must be signed in to change notification settings - Fork 0
/
shapeUtils.js
446 lines (401 loc) · 13.9 KB
/
shapeUtils.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
const tolerance = 5;
let idCounter = 0;
const createLinesFromPoints = (points) => {
const lines = [];
for (let i = 0; i < points.length; i++) {
let line;
if (i + 1 === points.length) {
line = [points[i], points[0]];
} else {
line = [points[i], points[i + 1]];
}
lines.push(line);
}
lines.forEach((line) => {
line.sort((p1, p2) => p1[0] < p2[0] || (p1[0] === p2[0] && p1[1] < p2[1]) ? -1 : 1);
});
return orderLines(lines);
}
const createShape = (x, y, size, def, rot) => {
const points = def.map((point) => {
return {
0: point[0],
1: point[1],
}
});
const lines = createLinesFromPoints(points);
const shape = {
highlighted: false,
subShapes: [{
points: [...points],
lines: [...lines],
id: idCounter++,
}],
}
recalculatePointsAndLines(shape);
const center = getCenterOfShapeGroup([shape]);
updatePoints(shape, center, x, y, size, rot);
return shape;
}
const createPath = (shape) => {
const lines = shape.lines;
const path = new Path2D();
let chainBeginning;
let previous;
for (let i = 0; i < lines.length; i++) {
const line = lines[i].length === 1 ? lines[i][0] : lines[i];
if (chainBeginning === undefined) {
chainBeginning = line;
path.moveTo(line[0][0], line[0][1]);
path.lineTo(line[1][0], line[1][1]);
} else {
if (line[0] !== previous[1]) {
path.lineTo(chainBeginning[0][0], chainBeginning[0][1]);
chainBeginning = line;
path.moveTo(line[0][0], line[0][1]);
path.lineTo(line[1][0], line[1][1]);
} else {
path.lineTo(line[1][0], line[1][1]);
}
}
previous = line;
}
path.closePath();
return path;
}
const updatePoints = (shape, center, dx, dy, size, rot) => {
shape.points.forEach((p) => {
const [x, y] = rotatePoint(p[0] - center[0], p[1] - center[1], rot);
p[0] = (x * size) + center[0] + dx;
p[1] = (y * size) + center[1] + dy;
});
shape.path = createPath(shape);
}
const toggleHighlight = (shape) => {
shape.highlighted = !shape.highlighted;
}
const distanceBetweenPoints = (p1, p2) => {
return Math.sqrt(Math.pow(p1[0] - p2[0], 2) + Math.pow(p1[1] - p2[1], 2));
}
const getDifferenceBetweenLines = (l1, l2) => {
return distanceBetweenPoints(l1[0], l2[0]) + distanceBetweenPoints(l1[1], l2[1]);
}
// Given 2 shapes, find a matching line between them. That is, a line where the sum of distances between the 2 points is below the given tolerance.
const findNearestLinesWithinTolerance = (shape1, shape2, tolerance) => {
let minDifference;
let minLines;
shape1.lines.forEach((l1) => {
shape2.lines.forEach((l2) => {
// Check it both ways. Have to make sure lines are in the right order so they're drawn correctly.
const difference = getDifferenceBetweenLines(l1, l2);
if (minDifference === undefined || difference < minDifference) {
minDifference = difference;
minLines = [l1, l2];
}
const difference2 = getDifferenceBetweenLines(l1, [l2[1], l2[0]]);
if (minDifference === undefined || difference2 < minDifference) {
minDifference = difference2;
minLines = [l1, [l2[1], l2[0]]];
}
});
});
if (minDifference !== undefined && minDifference < tolerance) {
return {
shapes: [shape1, shape2],
lines: minLines,
}
}
return undefined;
}
const moveToFront = (shape, shapes) => {
const i = shapes.findIndex((s) => s === shape);
shapes.splice(i, 1);
shapes.push(shape);
}
// Given a line and a number of points, return that many evenly spaced points along the line (not including ends).
const getPointsOnLine = (line, pointCount) => {
const start = line[0];
const end = line[1];
const rise = end[1] - start[1];
const run = end[0] - start[0];
const points = [];
for (let i = 0; i < pointCount; i++) {
const newPoint = {};
newPoint[0] = start[0] + ((i + 1) * (run / (pointCount + 1)));
newPoint[1] = start[1] + ((i + 1) * (rise / (pointCount + 1)));
points.push(newPoint);
}
return points;
}
// Tests to make sure combining 2 shapes would have a valid result. Specifically, tries to avoid a self-intersecting shape. The
// math to check that rigorously seems overkill here. Instead, it takes a few sample points on the newly added lines, and checks
// if they're inside the existing shape.
const newShapeIsValid = (shape1, shape2, context) => {
const perimeter1 = shape1.lines;
const perimeter2 = shape2.lines;
const newLines = perimeter1.filter((l) => !perimeter2.find((line) => getDifferenceBetweenLines(l, line) < .1 || getDifferenceBetweenLines(l, line.toReversed()) < .1));
for (let i = 0; i < newLines.length; i++) {
const line = newLines[i];
const testPoints = getPointsOnLine(line, 3);
for (let j = 0; j < testPoints.length; j++) {
if (shapeContainsPoint(context, shape2, testPoints[j][0], testPoints[j][1])) {
return false;
}
}
}
return true;
}
const snapLine = (lineMatch, context) => {
const l1 = lineMatch.lines[1];
const l2 = lineMatch.lines[0];
const shape = lineMatch.shapes[0];
const sizeMult = distanceBetweenPoints(l1[0], l1[1]) / distanceBetweenPoints(l2[0], l2[1]);
const center = getCenterOfShapeGroup([shape]);
updatePoints(shape, center, 0, 0, sizeMult, 0);
const rot = getRotationBetweenLines(l1, l2);
updatePoints(shape, center, 0, 0, 1, rot);
const dx = l1[0][0] - l2[0][0];
const dy = l1[0][1] - l2[0][1];
updatePoints(shape, center, dx, dy, 1, 0);
const outerShape = lineMatch.shapes[1];
const val = newShapeIsValid(shape, outerShape, context);
if (!val) {
// updatePoints does transformation in different order, so has to call it separately.
updatePoints(shape, center, -dx, -dy, 1, 0);
updatePoints(shape, center, 0, 0, 1, -rot);
updatePoints(shape, center, 0, 0, 1 / sizeMult, 0);
return false;
}
shape.subShapes.forEach((subShape) => {
subShape.points.forEach((point, i) => {
const index = outerShape.points.findIndex((p) => distanceBetweenPoints(point, p) < tolerance);
if (index !== -1) {
subShape.points[i] = outerShape.points[index];
}
});
subShape.lines = createLinesFromPoints(subShape.points);
});
outerShape.subShapes.push(...shape.subShapes);
recalculatePointsAndLines(outerShape);
return true;
}
const recalculatePointsAndLines = (shape) => {
const points = [];
const lines = [];
shape.subShapes.forEach((s) => {
s.points.forEach((p) => {
if (!points.includes(p)) {
points.push(p);
}
});
s.lines.forEach((l) => {
lines.push(l);
});
});
shape.points = points;
shape.lines = lines;
reduceLinesToPerimeter(shape);
shape.lines = orderLines(shape.lines);
shape.path = createPath(shape);
}
const removeSubShape = (shape, subShape) => {
if (shape === undefined || subShape === undefined) {
return;
}
const index = shape.subShapes.findIndex((s) => s === subShape);
if (index !== -1) {
shape.subShapes.splice(index, 1);
const points = subShape.points.map((p) => {
return { ...p };
});
const lines = createLinesFromPoints(points);
subShape.subShapes = [{ points, lines }];
recalculatePointsAndLines(shape);
recalculatePointsAndLines(subShape);
}
}
const duplicateShape = (shape) => {
const points = [];
shape.subShapes.forEach((subShape) => {
subShape.points.forEach((point) => {
if (!points.find((p) => p === point)) {
points.push(point);
}
});
});
const newPoints = points.map((point) => ({ ...point }));
const newSubShapes = [];
shape.subShapes.forEach((subShape) => {
const newSubShape = {
points: [],
id: idCounter++,
}
for (let i = 0; i < subShape.points.length; i++) {
newSubShape.points.push(newPoints.find((point) => point[0] === subShape.points[i][0] && point[1] === subShape.points[i][1]));
}
newSubShape.lines = createLinesFromPoints(newSubShape.points);
newSubShapes.push(newSubShape);
});
const newShape = {
highlighted: false,
subShapes: newSubShapes,
}
recalculatePointsAndLines(newShape);
return newShape;
}
const getSubShape = (context, shape, x, y) => {
return shape.subShapes.find((s) => {
return context.isPointInPath(createPath(s), x, y);
});
}
const getRotationBetweenLines = (l1, l2) => {
const dx = l1[1][0] - l1[0][0];
const dy = l1[1][1] - l1[0][1];
const dx2 = l2[1][0] - l2[0][0];
const dy2 = l2[1][1] - l2[0][1];
let rotationAngle = (Math.atan2(dy, dx) - Math.atan2(dy2, dx2)) % (Math.PI / 2);
if (rotationAngle < -1 * Math.PI / 4) {
rotationAngle += Math.PI / 2;
} else if (rotationAngle > Math.PI / 4) {
rotationAngle -= Math.PI / 2;
}
return rotationAngle;
}
const regularNGon = (n) => {
const def = [];
for (let i = 0; i < n; i++) {
def.push([
Math.cos((2 * Math.PI * i) / n),
Math.sin((2 * Math.PI * i) / n),
]);
}
return def;
}
const rotatePoint = (x, y, rot) => {
return [
(x * Math.cos(rot)) - (y * Math.sin(rot)),
(y * Math.cos(rot)) + (x * Math.sin(rot)),
]
}
const shapeContainsPoint = (context, shape, x, y) => {
return context.isPointInPath(shape.path, x, y);
}
const updateShapeGroupLocation = (shapeGroup, dx, dy) => {
const center = getCenterOfShapeGroup(shapeGroup);
shapeGroup.forEach((shape) => {
updatePoints(shape, center, dx, dy, 1, 0);
});
}
const updateShapeGroupRotation = (shapeGroup, rotation) => {
const center = getCenterOfShapeGroup(shapeGroup);
shapeGroup.forEach((shape) => {
updatePoints(shape, center, 0, 0, 1, rotation);
});
}
const updateShapeGroupSize = (shapeGroup, sizeMultiplier) => {
const center = getCenterOfShapeGroup(shapeGroup);
shapeGroup.forEach((shape) => {
updatePoints(shape, center, 0, 0, sizeMultiplier, 0);
});
}
const getCenterOfShapeGroup = (shapeGroup) => {
let top, right, bottom, left;
shapeGroup.forEach((shape) => {
shape.points.forEach((point) => {
if (top === undefined || point[1] < top) {
top = point[1];
}
if (right === undefined || point[0] > right) {
right = point[0];
}
if (bottom === undefined || point[1] > bottom) {
bottom = point[1];
}
if (left === undefined || point[0] < left) {
left = point[0];
}
});
});
return [(right + left) / 2, (bottom + top) / 2];
}
const getUniqueLines = (lines) => {
const uniqueLines = [];
const repeatedLines = [];
lines.forEach((line) => {
if (!repeatedLines.find((l) => (l[0] === line[0] && l[1] === line[1]) || (l[1] === line[0] && l[0] === line[1]))) {
const index = uniqueLines.findIndex((l) => (l[0] === line[0] && l[1] === line[1]) || (l[1] === line[0] && l[0] === line[1]));
if (index === -1) {
uniqueLines.push(line);
} else {
uniqueLines.splice(index, 1);
repeatedLines.push(line);
}
}
});
return uniqueLines;
}
const reduceLinesToPerimeter = (shape) => {
const uniqueLines = getUniqueLines(shape.lines);
shape.lines = orderLines(uniqueLines);
shape.path = createPath(shape);
}
const orderLines = (linesInput) => {
const lines = [...linesInput];
const orderedLines = lines.splice(0, 1);
let previousPoint = orderedLines[0][1];
while (lines.length > 0) {
let index = lines.findIndex((line) => line[0] === previousPoint || line[1] === previousPoint);
if (index === -1) {
orderedLines.push(lines.splice(0, 1)[0]);
previousPoint = orderedLines[orderedLines.length - 1][1];
} else {
const newLine = lines.splice(index, 1)[0];
if (newLine[0] === previousPoint) {
previousPoint = newLine[1];
orderedLines.push([newLine[0], newLine[1]]);
} else {
previousPoint = newLine[0];
orderedLines.push([newLine[1], newLine[0]]);
}
}
}
return orderedLines;
}
const fillShapesFromInputData = (shapes) => {
const points = [];
shapes.forEach((shape) => {
shape.subShapes.forEach((subShape) => {
for (let i = 0; i < subShape.points.length; i++) {
const point = points.find((p) => p[0] === subShape.points[i][0] && p[1] === subShape.points[i][1]);
if (!point) {
points.push(subShape.points[i]);
} else {
subShape.points[i] = point;
}
}
subShape.lines = createLinesFromPoints(subShape.points);
subShape.id = idCounter++;
});
shape.highlighted = false;
recalculatePointsAndLines(shape);
});
}
const shapeUtils = {
updateShapeGroupLocation,
updateShapeGroupRotation,
updateShapeGroupSize,
createShape,
regularNGon,
snapLine,
toggleHighlight,
moveToFront,
shapeContainsPoint,
removeSubShape,
getSubShape,
duplicateShape,
fillShapesFromInputData,
getCenterOfShapeGroup,
createPath,
findNearestLinesWithinTolerance,
}
export default shapeUtils;