-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
90 lines (70 loc) · 2.3 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
let canvas;
let intersection_points = [];
let lines = [];
let last_click = null;
let q = null;
let q_turn = -1;
let h = null;
function setup() {
canvas = createCanvas(document.getElementById("canvasContainer").offsetWidth, windowHeight)
canvas.parent("canvasContainer");
canvas.mouseClicked(click_line);
background(200)
}
function draw() {
background(200);
q?.display();
h?.display();
lines.forEach(line => line.display());
intersection_points.forEach(point => point.display());
preview_line?.display(); // does nothing if preview_line is null
preview_intersection_points?.forEach(point => point.display());
}
function reset_stroke() {
stroke("black")
strokeWeight(1)
}
function add_line(line) {
let intersections = line.get_all_intersections();
line.recolor(intersections);
lines.push(line);
intersection_points.push(...intersections);
}
function refresh_all_colors() {
intersection_points.forEach(point => point.recolor());
lines.forEach(line => line.recolor()) ;
}
function find_best_h() {
intersection_points[0].color = "blue";
let bestCounter = Infinity; let bestH = null;
for (let i = intersection_points.length-1; i >= 0; i--) {
let counters = [0,0];
let h_lines = [new Line(q, intersection_points[i], 2), new Line(q, intersection_points[i], 2)];
// We create a h line shifted every so slightly in the two direction
h_lines[0].a.x += EPSILON_ZERO;
h_lines[0].b.x += EPSILON_ZERO;
h_lines[1].a.x -= EPSILON_ZERO;
h_lines[1].b.x -= EPSILON_ZERO;
for (let j = lines.length-1; j >= 0; j--) {
const line_to_test = lines[j];
for (const h_lines_key in h_lines) {
if (are_in_region(q, h_lines[h_lines_key], line_to_test.points)) {
counters[h_lines_key] += 1;
}
}
}
for (const counters_key in counters) {
if (counters[counters_key] < bestCounter) {
bestCounter = counters[counters_key];
bestH = h_lines[counters_key];
}
}
}
h = bestH;
refresh_all_colors();
}
function count_nb_green_lines() {
let counter = 0;
lines.forEach(line => {if (line.color === "green") { counter += 1;}});
return counter
}