-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutilities.js
More file actions
122 lines (107 loc) · 3.15 KB
/
Copy pathutilities.js
File metadata and controls
122 lines (107 loc) · 3.15 KB
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
// enumerator for colors
const colors = {
RED : [255, 0, 0],
GREEN : [0, 255, 0],
BLUE : [0, 0, 255],
YELLOW : [255, 255, 0],
MAGENTA : [255, 0, 255],
CYAN : [0, 255, 255],
WHITE : [255, 255, 255],
GREY : [128, 128, 128],
}
// enumerator of keys
const keys = {
LEFT : 37,
RIGHT : 39,
SPACE : 32,
M : 77,
S : 83,
ZERO : 48,
NINE : 57,
}
// enumerator for side direction
const side = {
LEFT : 1,
COLLINEAR : 0,
RIGHT : -1,
}
// Function to generate an array of white points
function generateSetOfPoints(n) {
let points = [];
for (let i = 0; i < n; i++) {
let x = random(50, width - 50);
let y = random(50, height - 50);
newPoint = createVector(x, y);
newPoint.color = colors.WHITE;
newPoint.circle = true;
points.push(newPoint);
}
return points;
}
// Function to draw a point with its color
function drawPoint(_point, scrSh=false) {
stroke(_point.color);
strokeWeight(4);
point(_point);
drawCircle(_point);
if (scrSh) screenShot();
}
// Function to draw an array of points
function drawPoints(points, scrSh = false, numbers = false) {
for (let i = 0; i < points.length; i++) {
if (numbers) {
// Draw the label
fill(255);
noStroke();
textSize(12);
textAlign(CENTER, BOTTOM);
text(i + 1, points[i].x, points[i].y - 8); // Adjust the value -8 to position the label relative to the point
}
drawPoint(points[i], scrSh);
}
}
function drawCircle(_point) {
if (_point.circle && _point.color !== colors.WHITE) {
_point.circle = false;
noFill();
stroke(_point.color);
strokeWeight(2);
ellipse(_point.x, _point.y, 15, 15); // Draw a circle around the first point
}
}
function drawLine(_point1, _point2, color = colors.GREEN, scrSh = false) {
stroke(color);
strokeWeight(2);
line(_point1.x, _point1.y, _point2.x, _point2.y);
if (scrSh) screenShot();
}
// Function to sort points by x-coordinate
function sortPoints(points) {
points.sort(function(a, b) {
return a.x - b.x;
});
return points;
}
function orient(_point1, _point2, _point3, scrSh = false) {
// Calculate the determinant
let determinant = (_point2.x - _point1.x) * (_point3.y - _point1.y) - (_point3.x - _point1.x) * (_point2.y - _point1.y);
// Return the sign of the side direction
return determinant > 0 ? side.LEFT : determinant < 0 ? side.RIGHT : side.COLLINEAR;
}
function distance(_point1, _point2) {
return Math.sqrt(Math.pow(_point2.x - _point1.x, 2) + Math.pow(_point2.y - _point1.y, 2));
}
function lineDist(_point1, _point2, _point3) {
return Math.abs((_point2.y - _point1.y) * _point3.x - (_point2.x - _point1.x) * _point3.y + _point2.x * _point1.y - _point2.y * _point1.x) / distance(_point1, _point2);
}
function changeColor(_point, color) {
_point.color = color;
_point.circle = true;
drawPoint(_point);
if (microSteps) screenShot();
}
function changeColors(points, color) {
for (let i = 0; i < points.length; i++) {
changeColor(points[i], color);
}
}