-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils2D.js
346 lines (318 loc) · 9.95 KB
/
utils2D.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
/* eslint no-param-reassign:0, no-nested-ternary:0 */
const Utils2D = {
/** returns coordinates normalized according to the
element's dimensions */
getNormalizedCoords: (coords, elem) => ({
x: coords.x / elem.offsetWidth,
y: coords.y / elem.offsetHeight,
}),
/** returns coordinates clipped by the boundaries of the element */
getClippedCoords: (coords, elem) => {
const w = elem.offsetWidth;
const h = elem.offsetHeight;
const { x, y } = coords;
return {
x: x < 0 ? 0 : (x > w) ? w : x,
y: y < 0 ? 0 : (y > h) ? h : y,
};
},
/** a combination of clipping and normalization */
getClippedAndNormalizedCoords: (coords, elem) => {
const w = elem.offsetWidth;
const h = elem.offsetHeight;
const { x, y } = coords;
return {
x: (x < 0 ? 0 : (x > w) ? w : x) / w,
y: (y < 0 ? 0 : (y > h) ? h : y) / h,
};
},
/** check if (x,y) is within circle (cx,cy,r) */
coordsInCircle: (x, y, cx, cy, r) => ((x - cx) * (x - cx) + (y - cy) * (y - cy) <= r * r),
/** check if (x,y) is within rectangle */
coordsInRectangle: (x, y, rx, ry, rw, rh) => (
rx <= x && x <= rx + rw && ry <= y && y <= ry + rh
),
/** check if (x,y) is within the element, assuming that both
have their coordinates calculated from the same reference point */
coordsInElement: (x, y, el) => {
const ex = parseFloat(el.style.left);
const ey = parseFloat(el.style.top);
const ew = el.offsetWidth;
const eh = el.offsetHeight;
return Utils2D.coordsInRectangle(x, y, ex, ey, ew, eh);
},
/** check if point is inside a convex polygon */
coordsInConvexPolygon: (x, y, points) => {
let sign = null;
let j = points.length - 1;
for (let i = 0; i < points.length; ++i) {
if (x === points[i].x && y === points[i].y) return true;
const cprod = (points[j].x - x) * (points[i].y - y)
- (points[i].x - x) * (points[j].y - y);
if (sign) {
if ((sign > 0 && cprod < 0) || (sign < 0 && cprod > 0)) return false;
} else if (cprod > 0) {
sign = 1;
} else if (cprod < 0) {
sign = -1;
}
j = i;
}
return true;
},
/** check if point is inside an ellipse */
coordsInEllipse: (x, y, cx, cy, rx, ry) => {
const xx = x - cx;
const yy = y - cy;
return (xx * xx) / (rx * rx) + (yy * yy) / (ry * ry) <= 1;
},
/** check if circles intersect */
circlesIntersect: (cx1, cy1, r1, cx2, cy2, r2) => {
const dx12 = cx1 - cx2;
const dy12 = cy1 - cy2;
const r12 = r1 + r2;
return dx12 * dx12 + dy12 * dy12 <= r12 * r12;
},
/** check if 2 rectangles intersect */
rectanglesIntersect: (x1, y1, w1, h1, x2, y2, w2, h2) => (
(x1 <= x2 + w2) && (x2 <= x1 + w1) && (y1 <= y2 + h2) && (y2 <= y1 + h1)
),
/** check if 2 dom elements intersect */
domElementsIntersect: (elem1, elem2) => {
const x1 = parseFloat(elem1.style.left);
const y1 = parseFloat(elem1.style.top);
const w1 = elem1.offsetWidth;
const h1 = elem1.offsetHeight;
const x2 = parseFloat(elem2.style.left);
const y2 = parseFloat(elem2.style.top);
const w2 = elem2.offsetWidth;
const h2 = elem2.offsetHeight;
return Utils2D.rectanglesIntersect(x1, y1, w1, h1, x2, y2, h2, w2);
},
/** creates a new line */
createLine: (x1, y1, x2, y2) => ({ x1, y1, x2, y2 }),
/** draw a line */
drawLine: (ctx, x1, y1, x2, y2) => {
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.stroke();
},
/** draw a circle */
drawCircle: (ctx, cx, cy, r, styleParams) => {
const fillColor = styleParams.fillColor || '#00ff00';
const strokeColor = styleParams.strokeColor || '#000000';
const lineWidth = styleParams.lineWidth || 2;
ctx.beginPath();
ctx.arc(cx, cy, r, 0, 2 * Math.PI, false);
ctx.fillStyle = fillColor;
ctx.fill();
ctx.lineWidth = lineWidth;
ctx.strokeStyle = strokeColor;
ctx.stroke();
},
/** draw an ellipse */
drawEllipse: (ctx, cx, cy, rx, ry, nofill) => {
// ctx.save(); // save state
// ctx.beginPath();
//
// ctx.translate(cx - rx, cy - ry);
// ctx.scale(rx, ry);
// ctx.arc(1, 1, 1, 0, 2 * Math.PI, false);
//
// ctx.restore(); // restore to original state
// if (!nofill) ctx.fill();
// ctx.stroke();
ctx.beginPath();
ctx.ellipse(cx, cy, rx, ry, 0, 2 * Math.PI, false);
if (!nofill) ctx.fill();
ctx.stroke();
},
/** draw a polygon */
drawPolygon: (ctx, points, scaleX, scaleY, nofill) => {
ctx.beginPath();
ctx.moveTo(points[0].x * scaleX, points[0].y * scaleY);
for (let i = 1; i < points.length; ++i) {
ctx.lineTo(points[i].x * scaleX, points[i].y * scaleY);
}
ctx.closePath();
if (!nofill) ctx.fill('evenodd');
ctx.stroke();
},
/** draw a polygon */
drawDeleteIcon: (ctx, x, y, width, height) => {
// draw ellipse
const blw = (width + height) * 0.02;
ctx.fillStyle = 'red';
ctx.lineWidth = blw;
ctx.strokeStyle = 'black';
Utils2D.drawEllipse(ctx, x, y, 0.5 * (width - blw), 0.5 * (height - blw));
// draw an X symbol
ctx.lineWidth = (width + height) * 0.075;
ctx.lineCap = 'round';
ctx.strokeStyle = 'white';
ctx.beginPath();
ctx.moveTo(x - 0.2 * width, y - 0.2 * height);
ctx.lineTo(x + 0.2 * width, y + 0.2 * height);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(x + 0.2 * width, y - 0.2 * height);
ctx.lineTo(x - 0.2 * width, y + 0.2 * height);
ctx.stroke();
},
/** clip the canvas with an ellipse shape */
clipEllipse: (ctx, cx, cy, rx, ry) => {
ctx.save(); // save state
ctx.beginPath();
ctx.translate(cx - rx, cy - ry);
ctx.scale(rx, ry);
ctx.arc(1, 1, 1, 0, 2 * Math.PI, false);
ctx.restore(); // restore to original state
ctx.clip();
},
/** get the convex hull of the given points */
getConvexHull: (pts) => {
// sort by x and y
const points = pts.slice(0);
points.sort((a, b) => (a.x === b.x ? a.y - b.y : a.x - b.x));
// compute lower hull
const lower = [];
for (let i = 0; i < points.length; ++i) {
while (lower.length >= 2 && cross(lower[lower.length - 2], lower[lower.length - 1], points[i]) <= 0) {
lower.pop();
}
lower.push(points[i]);
}
// compute upper hull
const upper = [];
for (let i = points.length - 1; i >= 0; --i) {
while (upper.length >= 2 && cross(upper[upper.length - 2], upper[upper.length - 1], points[i]) <= 0) {
upper.pop();
}
upper.push(points[i]);
}
// join lower and upper hulls
upper.pop();
lower.pop();
return lower.concat(upper);
},
/**
* get the area of a polygon assuming that:
* 1) it's a simple polygon
* 2) the points are sorted as they appear in the polygon's perimeter
* either clockwise or counterclockwise
*/
getAreaOfPolygon: (points) => {
let area = 0.0;
let j = points.length - 1;
for (let i = 0; i < points.length; ++i) {
area += (points[j].x + points[i].x) * (points[j].y - points[i].y);
j = i;
}
return Math.abs(area * 0.5);
},
/** get centroid of a polygon */
getCentroidOfPolygon: (points) => {
if (points.length === 0) {
return null;
} else if (points.length < 3) {
let x = 0;
let y = 0;
for (let i = 0; i < points.length; ++i) {
x += points[i].x;
y += points[i].y;
}
x /= points.length;
y /= points.length;
return { x, y };
} else {
let area = 0.0;
let x = 0.0;
let y = 0.0;
let j = points.length - 1;
for (let i = 0; i < points.length; ++i) {
const delta = points[j].x * points[i].y - points[i].x * points[j].y;
x += (points[j].x + points[i].x) * delta;
y += (points[j].y + points[i].y) * delta;
area += delta;
j = i;
}
area *= 3;
x /= area;
y /= area;
return { x, y };
}
},
getPointWithinComplexPolygon(points, refX, refY) {
if (Utils2D.isPointInPolygon(refX, refY, points)) {
return { x: refX, y: refY };
}
const coefs = [0.5, 1.5, 0.75, 1.25, 0.25];
for (const p of points) {
const dx = refX - p.x;
const dy = refY - p.y;
for (const c of coefs) {
const x = p.x + dx * c;
const y = p.y + dy * c;
if (Utils2D.isPointInPolygon(x, y, points)) {
return { x, y };
}
}
}
const m = points.length >> 1;
return {
x: (points[m].x + points[m + 1].x) * 0.5,
y: (points[m].y + points[m + 1].y) * 0.5 };
},
isPointInPolygon(x, y, points) {
if (points.length < 3) return false;
let j = points.length - 1;
let count = 0;
for (let i = 0; i < points.length; ++i) {
let p1;
let p2;
if (points[i].y < points[j].y) {
p1 = points[i];
p2 = points[j];
} else {
p1 = points[j];
p2 = points[i];
}
if (p1.y < y && y <= p2.y) {
if (p1.x === p2.x) {
if (p1.x >= x) count++;
} else {
const m = (p2.y - p1.y) / (p2.x - p1.x);
const xx = p1.x + (y - p1.y) / m;
if (xx >= x) count++;
}
}
j = i;
}
return count % 2 === 1;
},
isPointInPolygonBoundingBox(x, y, points) {
if (points.length === 0) return false;
const bb = Utils2D.getBoundingBox(points);
return x >= bb.minX && x <= bb.maxX && y >= bb.minY && y <= bb.maxY;
},
getBoundingBox(points) {
let minX = points[0].x;
let maxX = points[0].x;
let minY = points[0].y;
let maxY = points[0].y;
for (let i = 1; i < points.length; ++i) {
if (minX > points[i].x) minX = points[i].x;
if (maxX < points[i].x) maxX = points[i].x;
if (minY > points[i].y) minY = points[i].y;
if (maxY < points[i].y) maxY = points[i].y;
}
return { minX, minY, maxX, maxY };
},
};
export default Utils2D;
/** get the value of the 3rd component of the cross product between
the vectors (o -> a) and (o -> b) */
function cross(o, a, b) {
return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x);
}