forked from techwebb/canvas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
294 lines (244 loc) · 8.28 KB
/
main.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// The amount of symbol we want to place;
var count = 7;
var done = false;
var polyBase = new Poly();
var poly2 = null;
var dragPoint = null;
var dragEdge = null;
function mouseX(e) {
return e.clientX - e.target.offsetLeft;
}
function mouseY(e) {
return e.clientY - e.target.offsetTop;
}
function mouseDown(e) {
const x = mouseX(e);
const y = mouseY(e);
dragPoint = PointCollision(x, y);
var startEdge = false;
if (done) {
const polyEdges = poly2.getEdges();
startEdge = EdgeCollectionCollision(x, y, polyEdges);
console.log(startEdge);
}
if (dragPoint) {
dragPoint.x = x;
dragPoint.y = y;
document.addEventListener(__event.mouseUp, mouseUp);
redraw();
}
var addedPoints;
var lastEdge;
if (!done && polyBase.getPoints().length < count) {
console.log(x, y, polyBase.getPoints().length);
if (polyBase.getPoints().length === 0) {
polyBase.firstPoint(x, y);
} else if (polyBase.getPoints().length === 1) {
addedPoints = polyBase.getPoints();
lastEdge = addedPoints[addedPoints.length - 1].edge;
polyBase.secondPoint(x, y, lastEdge);
} else {
addedPoints = polyBase.getPoints();
lastEdge = addedPoints[addedPoints.length - 1].edge.twin;
polyBase.thirdPoint(x, y, lastEdge);
}
}
if (!done && polyBase.getPoints().length === count) {
done = true;
addedPoints = polyBase.getPoints();
var firstEdge = addedPoints[0].edge;
lastEdge = addedPoints[addedPoints.length - 1].edge.twin;
polyBase.close(firstEdge, lastEdge);
redraw();
return;
}
if (done && startEdge && startEdge.polygon !== null) {
//clearCanvas();
drawEdgeLoop(startEdge);
}
}
function mouseUp() {
dragPoint = null;
document.removeEventListener(__event.mouseUp, mouseUp);
};
function mouseMove(e) {
var x = mouseX(e);
var y = mouseY(e);
document.getElementById('xDisplay').innerHTML = x;
document.getElementById('yDisplay').innerHTML = y;
if (dragPoint)
document.getElementById('dragDisplay').innerHTML = dragPoint.name;
else document.getElementById('dragDisplay').innerHTML = 'null';
if (dragPoint != null) {
dragPoint.x = x;
dragPoint.y = y;
redraw();
}
redraw();
var pointByMouse = PointCollision(x, y);
var lineByMouse = LineCollision(x, y, true);
var div = document.getElementById('mouseHoverDisplay');
if (!dragPoint && pointByMouse) {
div.innerHTML = pointByMouse.toHTML();
div.style.visibility = "visible";
div.style.top = Math.min(pointByMouse.y, y) - 70 + "px";
div.style.left = pointByMouse.x - (div.offsetWidth / 2) + "px";
} else if (!dragPoint && lineByMouse) {
div.innerHTML = lineByMouse.toHTML();
div.style.visibility = "visible";
div.style.top = Math.min((lineByMouse.origin.y + lineByMouse.next.origin.y) / 2, y - 30) - 50 + "px";
div.style.left = (lineByMouse.origin.x + lineByMouse.next.origin.x) / 2 - (div.offsetWidth / 2) + "px";
//drawNgon(lineByMouse);
} else {
div.innerHTML = "";
div.style.visibility = "hidden";
}
}
function clearCanvas() {
window.window.c.fillStyle = "#ffffff";
window.window.c.fillRect(0, 0, window.w, window.h);
}
function redraw() {
clearCanvas();
if (done) {
polyBase = assureCCW(polyBase);
//console.log('running triangulation');
const tri = new Triangulation(polyBase);
tri.makeMonotone();
poly2 = tri.getPoly();
drawPoly(poly2, 3, '#f00');
//drawPoly(polyBase, 1, '#666');
if (dragEdge) fillPoly(dragEdge);
} else {
drawPoly(polyBase, 2, '#666');
}
//drawPoly(polyBase, 4, '#ccc');
}
function PointCollision(mouseX, mouseY) {
const points = polyBase.getPoints();
for (let i = 0; i < points.length; i++) {
if (points[i].distance2(mouseX, mouseY) < 150) { return points[i]; }
}
return null;
}
function LineCollision(mouseX, mouseY, debug = false) {
const edges = done ? poly2.getEdges() : polyBase.getEdges();
if (edges.length < 4) { return null; }
return EdgeCollectionCollision(mouseX, mouseY, edges, debug);
}
function EdgeCollectionCollision(mouseX, mouseY, edges, debug = false) {
const p = new Point(mouseX, mouseY, 'mouse');
if (debug) { drawPoint(p, __color.blue); }
// TODO: Where is the definition for edges? It isn't in local scope and can't find it globally.
for (let i = 0; i < edges.length; i++) {
if (edges[i].next == null) { continue; }
const v = edges[i].origin;
const w = edges[i].next.origin;
const t = Math.max(0, Math.min(1, (dot(p, v, w) / v.distance2(w.x, w.y)))); //normalized dot product of VP, WP
const l = lerp(v, w, t);
if (debug) {
drawLinePoints(p, v, 2, __color.gray);
drawLinePoints(p, w, 2, __color.gray);
drawPoint(l, __color.green);
drawLinePoints(l, p, 2, __color.purple);
}
if (p.distance(l.x, l.y) < 15 && t < 0.5) //distance to lerp along VW using t
return edges[i];
}
return null;
}
function drawPoly(polygon, width=3, color = __color.black) {
const points = polygon.getPoints();
var i;
for (i = 0; i < points.length; i++) {
drawPoint(points[i], 2 * width, color);
}
if (points.length < 2) { return; }
const edges = polygon.getEdges();
for (i = 0; i < edges.length; i++) {
drawLine(edges[i], width, color);
}
}
function drawPoint(p, width = 5, color = __color.red) {
if (window.c == null) { return; }
window.c.fillStyle = color;
window.c.beginPath();
window.c.arc(p.x, p.y, width, 0, Math.PI * 2, true);
window.c.closePath();
window.c.fill();
}
function drawLine(edge, width=4, color = __color.black) {
if (edge.next == null || edge.next.origin == null || edge.origin == null) return false;
drawLinePoints(edge.origin, edge.next.origin, width, color);
return false;
}
function drawLinePoints(a, b, width = 2, color = __color.gray) {
if (window.c === null) { return; }
window.c.lineWidth = width;
window.c.strokeStyle = color;
window.c.beginPath();
window.c.moveTo(a.x, a.y);
window.c.lineTo(b.x, b.y);
window.c.closePath();
window.c.stroke();
}
/** Starts an edge
*
* @param {any} start
*/
function drawEdgeLoop(start) {
window.c.fillStyle = 'rgba(100,100,255,0.2)';
window.c.beginPath();
window.c.moveTo(start.origin.x, start.origin.y);
//console.log(start);
var next = start.next;
//console.log("start: "+start.name);
while (next.name !== start.name) {
//console.log(count, next.name);
window.c.lineTo(next.origin.x, next.origin.y);
next = next.next;
}
window.c.lineTo(start.origin.x, start.origin.y);
window.c.closePath();
window.c.fill();
}
/** Starts an edge for polygon
*
* @param {any} start
*/
function fillPoly(start) {
// TODO: Implement this function
}
/** Used to find the median distance between polygons
*
* @param {any} a
* @param {any} o
* @param {any} b
*/
function dot(a, o, b) {
//vector 1 is OA
//vector 2 is OB
return (a.x - o.x) * (b.x - o.x) + (a.y - o.y) * (b.y - o.y);
}
function lerp(a, b, t) {
return new Point(a.x + t * (b.x - a.x), a.y + t * (b.y - a.y), 'lerp');
}
function assureCCW(polygon) {
const edges = polygon.getEdges();
var sum = 0;
var i;
for (i = 0; i < edges.length; i++) {
sum += (edges[i].next.origin.x - edges[i].origin.x) * (edges[i].next.origin.y + edges[i].origin.y);
}
// Polygon is already CCW
if (sum >= 0) return polygon;
// Points are CW, need to switch.
console.log("reversing");
const points = polygon.getPoints();
polygon.wipe();
for (i = points.length; i--;) {
polygon.addPoint(points[i].x, points[i].y);
}
polygon.close();
return polygon;
}