-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
199 lines (175 loc) · 5.23 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
// The amount of symbol we want to place;
var count = 7;
var done = false;
var debug = false; //shows helper lines for mouseMove
var polyBase = new Poly();
var additional = [];
var wip = [];
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){
var mousePoint = new Point(mouseX(e), mouseY(e), wip.length);
additional = [];
dragPoint = PointCollision(mousePoint);
if(done){
var polyEdges = poly2.getEdges();
var startEdge = EdgeCollectionCollision(mousePoint, polyEdges);
if(startEdge && startEdge.polygon) addToDrawing('face', startEdge, 'rgba(0,0,0,0.2)');
}
if(dragPoint){
dragPoint.moveTo(mousePoint);
document.addEventListener('mouseup', mouseUp);
redraw();
return;
}
if(!done){
wip.push(mousePoint);
}
redraw();
}
function mouseUp(){
dragPoint = null;
additional = [];
document.removeEventListener('mouseup', mouseUp);
}
function mouseMove(e){
//console.log(e);
var mousePoint = new Point(mouseX(e), mouseY(e), 'mouse');
additional = [];
tooltip.setXY(mousePoint);
tooltip.setDragPoint('null');
if(dragPoint!=null){
dragPoint.moveTo(mousePoint);
tooltip.setDragPoint(dragPoint.name);
tooltip.showPoint(dragPoint, mousePoint);
// redraw();
// return;
}
var pointByMouse = PointCollision(mousePoint);
var lineByMouse = LineCollision(mousePoint);
if (!dragPoint && pointByMouse){
tooltip.showPoint(pointByMouse, mousePoint);
}else if(!dragPoint && lineByMouse){
tooltip.showEdge(lineByMouse, mousePoint);
}else{
tooltip.clearHover();
}
redraw();
}
function keyPressEvent(e){
if(e.key != 'Enter') return;
if(done){
console.log('making triangulation');
var tri = new Triangulation();
tri.poly = poly2;
tri.makeMonotone();
poly2 = tri.getPoly();
}else {
done = true;
console.log('making poly');
poly2 = new Poly();
poly2.makeFromPointArray(wip);
wip = [];
}
redraw();
}
// -------======= Drawing Fucntions
function redraw(){
stage.clear();
if(!done){
connectTheDots(wip);
}
if(poly2){
drawPoly(poly2, 4, 'red');
}
if(additional.length){
drawAdditional(additional);
}
}
function connectTheDots(points){
points.forEach(function(item, i, arr){
stage.drawPoint(item, 6, "black");
if(i+1 in arr){
stage.drawLine(item, arr[i+1]);
}
});
}
function drawPoly(polygon, width=4, color="black"){
var points = polygon.getPoints();
points.forEach((item) => stage.drawPoint(item, width+2, color));
var edges = polygon.getEdges();
edges.forEach(item => stage.drawEdge(item, width, color));
}
function assureCCW(points){
var sum = 0;
for(var i=0; i<points.length-1; i++){
sum += (points[i+1].x - points.x)*(points[i+1].y + points.y);
}
if(sum >= 0) return wip; //polygon is already CCW
console.log('reversing');
return wip.reverse();
}
function addToDrawing(type, el, color, width = undefined){
additional.push({type:type, element:el, color:color, width:width});
}
function drawAdditional(items){
items.forEach(function(item){
switch(item.type){
case 'face':
stage.drawEdgeLoop(item.element, item.color)
break;
case 'line':
stage.drawLine(item.element[0], item.element[1], item.width, item.color);
break;
case 'point':
stage.drawPoint(item.element, item.width, item.color);
break;
}
});
}
// -----==== Collision Detection
function PointCollision(mouse){
var points = done ? poly2.getPoints() : wip;
return points.find( point => distanceAB(point, mouse) < 150 );
}
function LineCollision(mouse){
var edges = done ? poly2.getEdges() : [];
if(edges.length < 4) return null;
return EdgeCollectionCollision(mouse, edges);
}
function EdgeCollectionCollision(p, edges){
for (var i=0; i<edges.length; i++){
if(edges[i].next == null) continue;
var v = edges[i].origin;
var w = edges[i].next.origin;
var t = Math.max(0, Math.min(1, (dot(p,v,w) / v.distance2(w)))); //normalized dot product of VP, WP
var l = lerp(v,w,t);
if (debug){
addToDrawing('line', [p,v], 'grey', 1);
addToDrawing('line', [p,w], 'grey', 1);
if(0 < t && t< 1){
addToDrawing('line', [l,p], 'purple', 4);
addToDrawing('point', l, 'green', 5);
}
addToDrawing('point', p, 'blue', 5);
}
if ( p.distance(l) < 15 && t < 0.5){ //distance to lerp along VW using t
return edges[i];
}
}
return null;
}
function distanceAB(a, b){
return(sqr(a.x-b.x) + sqr(a.y-b.y));
};
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');
}