-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShapes.js
542 lines (501 loc) · 16.2 KB
/
Shapes.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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
/** GENERAL DISCLAIMER -
* This file is not designed to be reproduced or distributed, nor is it useable in content of any kind.
* It should be for the personal use of the owner, as it breaks any and all legislation regarding copyrighted code.
* - Credits : p5.js, Processing Foundation, @shiffman, @RobinLefebvre, @ */
class Dot
{
/** Constructor for Dot class.
* @param {Vector2D} args.position : the Dot's position on the map
* @param {Hex} args.circumferenceColor : the Dot's color on display */
constructor(args)
{
if(!args){args = {};}
/**@property position {Vector2D} : the current position of the Shape. */
this.position = args.position || new Vector2D(0,0);
/**@property pivot {Vector2D} : the pivot point of the Shape. */
this.pivot = args.pivot || this.position.copy();
/**@property rotation {Number} : the current rotation of the Shape in radians. */
this.rotation = args.rotation || 0;
/**@property circumferenceColor {Hex} : the RGBA color value used to display the shape - circumferenceColor */
this.circumferenceColor = args.circumferenceColor || '#000000FF';
/**@property areaColor {Hex} : the RGBA color value used to display the shape - area */
this.areaColor = args.areaColor || '#00000000';
/**@property radius {Number} : the current radius of the Shape. */
this.radius = 1;
}
getArea()
{
return this.radius;
}
getCircumference()
{
return this.radius;
}
getCenter()
{
return this.position.copy();
}
getPoints()
{
return [this.position.copy()];
}
/** Returns wether this shape contains the given Vector2D */
contains(vector)
{
return this.equals(vector);
}
/** Returns wether this shape intersect with the given shape */
intersects(shape)
{
let thisPoly = new Polygon(this.getPoints())
if(shape instanceof Polygon)
{
return thisPoly.intersects(shape)
}
else
{
let rangePoly = new Polygon(this.getPoints())
return thisPoly.intersects(rangePoly)
}
}
/** Displays the shape to the given canvas, using the given camera's parameter. See p5Renderer and Camera.js */
display(render, camera)
{
let pos = camera.mapToScreen(this.position);
render._pInst.stroke(this.circumferenceColor);
render._pInst.fill(this.areaColor);
render._pInst.point(pos.x, pos.y);
}
}
class Triangle extends Dot
{
/** Rectangle is defined as centered around a position. Base and Height define it. */
constructor(args)
{
super(args);
this.base = args.base || 100;
this.height = args.height || 100;
delete this.radius;
}
getArea()
{
return this.base * this.height / 2;
}
getCircumference()
{
let pts = this.getPoints();
let s = pts[0].dist(pts[1]);
s += pts[1].dist(pts[2]);
s += pts[2].dist(pts[0]);
return s;
}
getCenter()
{
return this.position.copy();
}
/** Rotates the current Triangle by a given angle in radians */
rotate(radiansAngle)
{
this.rotation += radiansAngle;
}
/** Returns an array with the position of the points of the current Rectangle */
getPoints()
{
let h = this.height / 2;
let b = this.base / 2;
let center = Vector2D.rotateAround(this.position, this.pivot, this.rotation);
let top = center.copy().add(0, h);
let right = center.copy().add(b,-h);
let left = center.copy().add(-b, -h);
let ret =
[
Vector2D.rotateAround(this.pivot, top, this.rotation).floor(),//Top
Vector2D.rotateAround(this.pivot, right, this.rotation).floor(),//Left
Vector2D.rotateAround(this.pivot, left, this.rotation).floor(),//Right
];
return ret;
}
/** Returns an array of arrays with edge start and end */
getEdges()
{
let pts = this.getPoints();
let ret =
[
[pts[0], pts[1]],
[pts[1], pts[2]],
[pts[2], pts[0]],
]
return ret;
}
/** Returns wether this shape contains the given Vector2D. Inaccurate for Triangle. Resolves as Rectangle */
contains(vector)
{
return (vector.x >= this.position.x - (this.base) &&
vector.x <= this.position.x + (this.base)&&
vector.y >= this.position.y - (this.height) &&
vector.y <= this.position.y + (this.height));
}
/** Displays the shape on the given canvas, using given camera */
display(render, camera)
{
let cameraShape = camera.getShape();
if(this.intersects(cameraShape))
{
// Setup render to use proper coloring
render._pInst.stroke(this.circumferenceColor);
render._pInst.fill(this.areaColor);
// Render a single pixel on the screen when need be.
let dim = camera.mapDimensionsToScreen(this.base, this.height);
if(dim.x < 1 && dim.y < 1)
{
let pos = camera.mapToScreen(this.position)
render._pInst.point(pos.x, pos.y);
}
// Otherwise, render all the points.
let pts = this.getPoints();
render._pInst.beginShape();
for(let i = 0; i < pts.length; i++)
{
let p = pts[i];
let pPos = camera.mapToScreen(p.x, p.y);
render._pInst.vertex(pPos.x, pPos.y);
}
render._pInst.endShape(CLOSE);
}
}
}
class Rectangle extends Dot
{
/** Rectangle is defined as centered around a position. Width and Height define it. */
constructor(args)
{
super(args);
this.width = args.width || 100;
this.height = args.height || 100;
delete this.radius;
}
getArea()
{
return this.height * this.width
}
getCircumference()
{
return (2 * this.height) + (2 * this.width)
}
getCenter()
{
return this.position.copy();
}
/** Rotates the current Rectangle by a given angle in radians */
rotate(radiansAngle)
{
this.rotation += radiansAngle;
}
/** Returns an array with the position of the points of the current Rectangle */
getPoints()
{
let h = this.height;
let w = this.width;
let center = Vector2D.rotateAround(this.pivot, this.position, this.rotation);
let tL = center.copy().add(-w, h);
let tR = center.copy().add(w, h);
let bR = center.copy().add(w, -h);
let bL = center.copy().add(-w, -h);
let ret =
[
Vector2D.rotateAround(this.pivot, tL, this.rotation).floor(),//Top Left
Vector2D.rotateAround(this.pivot, tR, this.rotation).floor(),//Top Right
Vector2D.rotateAround(this.pivot, bR, this.rotation).floor(),//Bottom Right
Vector2D.rotateAround(this.pivot, bL, this.rotation).floor(),//Bottom Left
];
return ret;
}
/** Returns an array of arrays with edge start and end */
getEdges()
{
let pts = this.getPoints();
let ret =
[
[pts[0], pts[1]],
[pts[1], pts[2]],
[pts[2], pts[3]],
[pts[3], pts[0]],
]
return ret;
}
/** Returns wether this shape contains the given Vector2D */
contains(vector)
{
return (vector.x >= this.position.x - (this.width) &&
vector.x <= this.position.x + (this.width)&&
vector.y >= this.position.y - (this.height) &&
vector.y <= this.position.y + (this.height));
}
/** Displays the shape on the given canvas, using given camera */
display(render, camera)
{
let cameraShape = camera.getShape();
if(this.intersects(cameraShape))
{
// Setup render to use proper coloring
render._pInst.stroke(this.circumferenceColor);
render._pInst.fill(this.areaColor);
// Render a single pixel on the screen when need be.
let dim = camera.mapDimensionsToScreen(this.width, this.height);
if(dim.x < 1 && dim.y < 1)
{
let pos = camera.mapToScreen(this.position)
render._pInst.point(pos.x, pos.y);
}
// Otherwise, render all the points.
let pts = this.getPoints();
render._pInst.beginShape();
for(let i = 0; i < pts.length; i++)
{
let p = pts[i];
let pPos = camera.mapToScreen(p.x, p.y);
render._pInst.vertex(pPos.x, pPos.y);
}
render._pInst.endShape(CLOSE);
}
}
}
/** Circle is defined as centered around a position. Radius defines it. */
class Circle extends Dot
{
constructor(args)
{
if(args === undefined){ args = {}}
super(args);
this.radius = args.radius || 100;
}
getArea()
{
return (Math.PI * this.radius * this.radius);
}
getCircumference()
{
return (2 * Math.PI * this.radius);
}
getCenter()
{
return this.position.copy();
}
getPoints()
{
// I'm sorry, this is just for giggles. Gotta relax, sometimes.
console.warn('-Circle.getPoints \nWTF man, why U trying to compute an Infinite thing ?');
return undefined;
}
contains(vector)
{
return (this.position.dist(vector) < this.radius)
}
intersects(shape)
{
let flag = false;
if(shape instanceof Polygon)
{
if(shape.contains(this.position))
{
flag = true;
}
shape.points.forEach(p => {
if(this.contains(p)){flag = true;}
})
}
else if(shape.getPoints)
{
let oP = new Polygon(shape.getPoints())
if(oP.contains(this.position))
{
flag = true;
}
oP.points.forEach(p =>
{
if(this.contains(p))
{
flag = true;
}
})
}
else
{
//ERROR
flag = undefined;
}
return flag;
}
display(render, camera)
{
let cameraShape = camera.getShape();
if (this.intersects(cameraShape))
{
render._pInst.stroke(this.circumferenceColor);
render._pInst.fill(this.areaColor);
let pos = camera.mapToScreen(this.position);
let dim = camera.mapDimensionsToScreen(this.radius, this.radius);
if(dim.x < 1 || dim.y < 1)
render._pInst.point(pos.x, pos.y);
else
render._pInst.ellipse(pos.x, pos.y, dim.x, dim.y);
}
}
}
class Line extends Dot
{
constructor(...args)
{
this.startPosition = args[0] || new Vector2D(0,0);
this.endPosition = args[1] || new Vector2D(100,100);
}
}
class Polygon
{
constructor(...args)
{
if(args[0] instanceof Array)
this.points = args[0];
}
getPoints()
{
return this.points;
}
isClosed()
{
let a = this.points[0];
let b = this.points[this.points.length-1]
return (a.equals(b))
}
intersects(shape)
{
if(shape instanceof Polygon)
{
let flag = false;
shape.points.forEach(oP => {
if(this.contains(oP))
{
flag = true;
}
})
this.points.forEach(tP => {
if(shape.contains(tP))
{
flag = true;
}
})
return flag;
}
else if(shape instanceof Dot)
{
return this.contains(shape.position);
}
}
contains(vector)
{
let magicFunction = (P0, P1, P2) => { return ((P1[0] - P0[0]) * (P2[1] - P0[1]) - (P2[0] - P0[0]) * (P1[1] - P0[1])); }
let arr = this.points;
if(!this.isClosed)
arr[arr.length] = arr[0];
let wn = 0;
for (let i = 0; i < arr.length - 1; i++)
{
let vec1 = new Vector2D(arr[i].x, arr[i].y);
let vec2 = new Vector2D(arr[i + 1].x, arr[i + 1].y);
if (vec2.y <= vector.y)
{
if (vec1.y > vector.y)
{
if (magicFunction([vec2.x, vec2.y], [vec1.x, vec1.y], [vector.x, vector.y]) > 0)
{
wn++;
}
}
}
else
{
if (vec1.y <= vector.y)
{
if (magicFunction([vec2.x, vec2.y], [vec1.x, vec1.y], [vector.x, vector.y]) < 0)
{
wn--;
}
}
}
}
return wn != 0;
}
display(render, camera)
{
let flag = false;
this.points.forEach(p=>{ if(camera.isOnScreen(p)) flag = true; })
if(flag)
{
if(this.areaColor)
render._pInst.fill(this.areaColor);
if(this.circumferenceColor)
render._pInst.stroke(this.circumferenceColor);
render._pInst.beginShape();
this.points.forEach(p => { let sPoint = camera.mapToScreen(p); vertex(sPoint.x, sPoint.y) } );
render._pInst.endShape(CLOSE);
}
}
/** Centroid is taking in an set of points and returning the x-y coordinates of the centroid.*/
centroid()
{
if(!this.centre)
{
let vertices = this.points
let centroid = createVector(0, 0);
let signedArea = 0.0;
let x0 = 0.0; // Current vertex X
let y0 = 0.0; // Current vertex Y
let x1 = 0.0; // Next vertex X
let y1 = 0.0; // Next vertex Y
let a = 0.0; // Partial signed area
// For all vertices except last
let i = 0
for (i = 0; i < vertices.length-1; i++)
{
x0 = vertices[i].x;
y0 = vertices[i].y;
x1 = vertices[i+1].x;
y1 = vertices[i+1].y;
a = x0*y1 - x1*y0;
signedArea += a;
centroid.x += (x0 + x1)*a;
centroid.y += (y0 + y1)*a;
}
// Do last vertex separately to avoid performing an expensive
// modulus operation in each iteration.
x0 = vertices[i].x;
y0 = vertices[i].y;
x1 = vertices[0].x;
y1 = vertices[0].y;
a = x0*y1 - x1*y0;
signedArea += a;
centroid.x += (x0 + x1)*a;
centroid.y += (y0 + y1)*a;
signedArea *= 0.5;
centroid.x /= (6.0*signedArea);
centroid.y /= (6.0*signedArea);
this.centre = {x: round(centroid.x), y: round(centroid.y)}
}
return this.centre;
}
static getRegular(points, radius, center, startRot)
{
if(!startRot)
startRot = 0;
if(!center)
center = new Vector2D(0,0)
let ret = [];
for(let i = startRot; i < Math.PI * 2 + startRot; i += (Math.PI * 2) / points)
{
let x = (radius) * cos(i) + center.x;
let y = (radius) * sin(i) + center.y;
ret.push(new Vector2D(x, y).floor());
}
return (new Polygon(ret));
}
}