-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.js
More file actions
120 lines (111 loc) · 3.46 KB
/
utils.js
File metadata and controls
120 lines (111 loc) · 3.46 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
"use strict";
function randomColor() {
const r = Math.round(Math.random() * 255);
const g = Math.round(Math.random() * 255);
const b = Math.round(Math.random() * 255);
return `rgb(${r},${g},${b},1)`;
}
// https://stackoverflow.com/questions/849211/shortest-distance-between-a-point-and-a-line-segment
function dist2(v, w) {
return (v.x - w.x) ** 2 + (v.y - w.y) ** 2;
}
function distToSegmentSquared(p, v, w) {
var l2 = dist2(v, w);
if (l2 == 0) return dist2(p, v);
var t = ((p.x - v.x) * (w.x - v.x) + (p.y - v.y) * (w.y - v.y)) / l2;
t = Math.max(0, Math.min(1, t));
return dist2(p, { x: v.x + t * (w.x - v.x), y: v.y + t * (w.y - v.y) });
}
function clamp(n, min, max) {
return Math.max(Math.min(n, max), min);
}
function selectAllPoints() {
deselectAll();
points.map((p) => selectedPoints.push(p));
}
function selectAllLines() {
deselectAll();
lines.map((l) => selectedLines.push(l));
}
function deselectAll() {
selectedPoints.length = 0;
selectedLines.length = 0;
}
function createNewPoint(connected = false) {
let newPos = Input.downPos.copy();
if (activePoint) {
if (Input.ctrl || Input.gridSnapping) {
let diff = Input.downPos.sub(activePoint.pos).scale(1 / cellSize);
diff.set(Math.round(diff.x), Math.round(diff.y));
if (diff.length() < 1) {
return;
}
newPos = activePoint.pos.add(diff.scale(cellSize));
}
}
const newPoint = new Point(newPos);
points.push(newPoint);
if (activePoint && (connected || Input.createConnections))
lines.push(new Line(newPoint, activePoint));
selectedPoints.push(newPoint);
activePoint = newPoint;
}
function connectSelectedPoints() {
for (let i = 0; i < selectedPoints.length; i++) {
for (let j = i + 1; j < selectedPoints.length; j++) {
const p1 = selectedPoints[i];
const p2 = selectedPoints[j];
let found = false;
lines.map((l) => {
if (
(l.p1 === p1 && l.p2 === p2) ||
(l.p1 === p2 && l.p2 === p1)
) {
found = true;
}
});
if (found === false) {
lines.push(new Line(p1, p2));
}
}
}
}
function relaxLines() {
if (selectedPoints.length) {
selectedPoints.map((p) => {
lines.map((l) => {
if (l.p1 === p || l.p2 === p) {
l.length = l.p1.pos.dist(l.p2.pos);
}
});
});
} else if (selectedLines.length) {
selectedLines.map((l) => {
l.length = l.p1.pos.dist(l.p2.pos);
});
} else {
lines.map((l) => {
l.length = l.p1.pos.dist(l.p2.pos);
});
}
}
function hide() {
selectedPoints.map((p) => (p.hidden = !p.hidden));
selectedLines.map((l) => (l.hidden = !l.hidden));
UnhideElem.classList.add("enabled");
}
function unhide() {
points.map((p) => (p.hidden = false));
lines.map((l) => (l.hidden = false));
UnhideElem.classList.remove("enabled");
}
function deleteSelected() {
activePoint = undefined;
lines = lines.filter(
(l) =>
selectedPoints.indexOf(l.p1) < 0 && selectedPoints.indexOf(l.p2) < 0
);
points = points.filter((p) => selectedPoints.indexOf(p) < 0);
lines = lines.filter((l) => selectedLines.indexOf(l) < 0);
deselectAll();
}