-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
106 lines (98 loc) · 2.9 KB
/
script.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
let pairNum = 3;
let points = [];
let cnv;
let currentPos = [];
function getGradient(p1, p2){
if(p1.y == p2.y){
return false;
}
gradient = (p2.x-p1.x)/(p2.y-p1.y);
return(gradient);
}
function centerCanvas(){
var x = (windowWidth - width) / 2;
var y = (windowHeight - height) / 2;
cnv.position(x, y);
}
function setup(){
cnv = createCanvas(Math.min(windowHeight, windowWidth), Math.min(windowHeight, windowWidth));
centerCanvas();
strokeWeight(5);
for(let i=0; i<pairNum; i++) {
let pointset = [];
for(let j=0; j<2; j++) {
pointset.push({
x: (Math.floor(Math.random()*(width-40))+20),
y: (Math.floor(Math.random()*(height-40))+20),
});
}
pointset.gradient = getGradient(pointset[0], pointset[1]);
points.push(pointset);
}
points.forEach((pair) => {
currentPos.push({x: pair[0].x,
y: pair[0].y});
});
console.log(currentPos);
}
function windowResized(){
centerCanvas();
}
function draw(){
background(0);
strokeWeight(5);
stroke(255);
points.forEach((pair) => {
point(pair[0].x, pair[0].y);
})
stroke(70);
points.forEach((pair) => {
point(pair[1].x, pair[1].y);
})
stroke(0, 200, 0);
strokeWeight(1);
for(let i=0;i<points.length; i++){
line(points[i][0].x, points[i][0].y, points[i][1].x, points[i][1].y);
}
stroke(200, 0, 0);
strokeWeight(5);
for(let i=0; i<currentPos.length; i++){
updatePosition(i);
}
}
function updatePosition(i) {
let atEndOfLine;
if(points[i].gradient == false){ /* vertical line */
atEndOfLine = (points[i][1].y)>=(points[i][0].y);
} else if((points[i][1].y>=points[i][0].y) && (points[i][1].x>=points[i][0].x)) {
atEndOfLine = (currentPos[i].y>=points[i][1].y) && (currentPos[i].x>=points[i][1].x);
} else if((points[i][1].y<=points[i][0].y) && (points[i][1].x<=points[i][0].x)) {
atEndOfLine = (currentPos[i].y<=points[i][1].y) && (currentPos[i].x<=points[i][1].x);
} else if((points[i][1].y>=points[i][0].y) && (points[i][1].x<=points[i][0].x)) {
atEndOfLine = (currentPos[i].y>=points[i][1].y) && (currentPos[i].x<=points[i][1].x);
} else if((points[i][1].y<=points[i][0].y) && (points[i][1].x>=points[i][0].x)) {
atEndOfLine = (currentPos[i].y<=points[i][1].y) && (currentPos[i].x>=points[i][1].x);
};
if(!atEndOfLine){ /* if not reached end of line */
if(points[i].gradient == false){
currentPos[i].x+=1/1.5;
} else if (points[i].gradient>=0) {
if (points[i][1].x>=points[i][0].x) {/* point going towards is higher than current point */
currentPos[i].y+=1/1.5;
currentPos[i].x+=points[i].gradient/1.5;
} else {
currentPos[i].y-=1/1.5;
currentPos[i].x-=points[i].gradient/1.5;
}
} else {
if (points[i][1].x>=points[i][0].x) {/* point going towards is higher than current point */
currentPos[i].y-=1/1.5;
currentPos[i].x-=points[i].gradient/1.5;
} else {
currentPos[i].y+=1/1.5;
currentPos[i].x+=points[i].gradient/1.5;
}
}
}
point(currentPos[i].x, currentPos[i].y);
}