-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
72 lines (56 loc) · 1.44 KB
/
sketch.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
let lines;
function setup() {
createCanvas(windowWidth, windowHeight);
lines = Line.getRandomLines(5);
}
function draw() {
background(20);
let intersectionPoints = [];
let angles = [];
for (let line of lines) {
angles = angles.concat(getAngles(line));
}
for (let ang of angles) {
intersectionPoints = intersectionPoints.concat(getIntersections(ang));
}
intersectionPoints = intersectionPoints.sort(function (a, b) {
return a.angle - b.angle;
});
drawPoly(intersectionPoints);
for (let line of lines) {
line.draw();
}
stroke(0);
ellipse(mouseX, mouseY, 10);
}
function getAngles(line) {
angles = []
for (let i = 0; i < 2; i++) {
let p = i == 0 ? line.start : line.end;
let ang = atan2(p.y - mouseY, p.x - mouseX);
angles.push(ang);
angles.push(ang - 0.001);
angles.push(ang + 0.001);
}
return angles;
}
function getIntersections(ang) {
let intersections = []
let x1 = cos(ang);
let y1 = sin(ang);
let ray = Line.castRay(createVector(mouseX, mouseY), createVector(mouseX + x1, mouseY + y1));
let i = ray.getIntersection(lines);
if (i) {
intersections.push({ pt: i, angle: ang });
}
return intersections;
}
function drawPoly(points) {
for (i = 0; i < points.length; i++) {
let p1 = points[i];
let p2 = points[(i + 1) % points.length];
noStroke();
fill(255, 255, 255, 240);
triangle(mouseX, mouseY, p1.pt.x, p1.pt.y, p2.pt.x, p2.pt.y);
}
}