Repository navigation
Expand file tree
/
Copy pathengine.js
More file actions
329 lines (230 loc) · 9.35 KB
/
Copy pathengine.js
File metadata and controls
329 lines (230 loc) · 9.35 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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
//3D vertex
var Vertex = function(x,y,z){
this.x = parseFloat(x);
this.y = parseFloat(y);
this.z = parseFloat(z);
}
//2D vertex
var Vertex2D = function(x,y){
this.x = parseFloat(x);
this.y = parseFloat(y);
}
//Cube Contructor
var Cube = function(center, size){
var d = size/2;
//Create vertex
this.vertices = [
new Vertex(center.x - d, center.y - d, center.z + d),
new Vertex(center.x - d, center.y - d, center.z - d),
new Vertex(center.x + d, center.y - d, center.z - d),
new Vertex(center.x + d, center.y - d, center.z + d),
new Vertex(center.x + d, center.y + d, center.z + d),
new Vertex(center.x + d, center.y + d, center.z - d),
new Vertex(center.x - d, center.y + d, center.z - d),
new Vertex(center.x - d, center.y + d, center.z + d)
]
//could make this more dynamic with loops
//Create Faces
this.faces = [
[this.vertices[0], this.vertices[1], this.vertices[2], this.vertices[3]],
[this.vertices[3], this.vertices[2], this.vertices[5], this.vertices[4]],
[this.vertices[4], this.vertices[5], this.vertices[6], this.vertices[7]],
[this.vertices[7], this.vertices[6], this.vertices[1], this.vertices[0]],
[this.vertices[7], this.vertices[0], this.vertices[3], this.vertices[4]],
[this.vertices[1], this.vertices[6], this.vertices[5], this.vertices[2]]
]
}
//Pyramid Contructor
var Pyramid = function(center, r, h){
var dz = h/2
//Create vertex
this.vertices = [
new Vertex(center.x, center.y, center.z + dz),
new Vertex(center.x + r, center.y + r, center.z - dz),
new Vertex(center.x - r, center.y + r, center.z - dz),
new Vertex(center.x + r, center.y - r, center.z - dz),
new Vertex(center.x - r, center.y - r, center.z - dz),
]
//could make this more dynamic with loops
//Create Faces
this.faces = [
[this.vertices[0], this.vertices[1], this.vertices[2]],
[this.vertices[0], this.vertices[1], this.vertices[3]],
[this.vertices[0], this.vertices[4], this.vertices[2]],
[this.vertices[0], this.vertices[4], this.vertices[3]],
[this.vertices[1], this.vertices[2], this.vertices[3], this.vertices[4]]
]
}
var Icosahedron = function(center, r){
var c1 = Math.cos(2*(Math.PI)/5);
var c2 = Math.cos((Math.PI)/5);
var s1 = Math.sin(2*(Math.PI)/5);
var s2 = Math.sin(4*(Math.PI)/5);
var dL = 2*r/(Math.sqrt(6)+Math.sqrt(2));
this.vertices = [
//Pentagon Plane #1
new Vertex(center.x, center.y - r, center.z+r/2),//0N
new Vertex(center.x + s1*r, center.y - c1*r, center.z+r/2),//1O
new Vertex(center.x + s2*r, center.y + c2*r, center.z+r/2),//2P
new Vertex(center.x - s2*r, center.y + c2*r, center.z+r/2),//3L
new Vertex(center.x - s1*r, center.y - c1*r, center.z+r/2),//4M
//Pentagon Plane #3
new Vertex(center.x, center.y + r, center.z-r/2),//5Q
new Vertex(center.x + s1*r, center.y + c1*r, center.z-r/2),//6U
new Vertex(center.x + s2*r, center.y - c2*r, center.z-r/2),//7T
new Vertex(center.x - s2*r, center.y - c2*r, center.z-r/2),//8S
new Vertex(center.x - s1*r, center.y + c1*r, center.z-r/2),//9R
//Top tip
new Vertex(center.x, center.y, center.z + r/2 + dL),//10X
//Bottom tip
new Vertex(center.x, center.y, center.z - r/2 - dL),//11Z
]
this.faces = [
//Top cone
[this.vertices[10], this.vertices[0], this.vertices[4]], //XMN
[this.vertices[10], this.vertices[0], this.vertices[1]], //XNO
[this.vertices[10], this.vertices[1], this.vertices[2]], //XPO
[this.vertices[10], this.vertices[2], this.vertices[3]], //XPL
[this.vertices[10], this.vertices[3], this.vertices[4]], //XLM
//Sides
[this.vertices[4], this.vertices[0], this.vertices[8]], //MNS -
[this.vertices[8], this.vertices[7], this.vertices[0]], //STN +
[this.vertices[0], this.vertices[1], this.vertices[7]], //NOT -
[this.vertices[7], this.vertices[6], this.vertices[1]], //TUO +
[this.vertices[2], this.vertices[1], this.vertices[6]], //POU -
[this.vertices[5], this.vertices[6], this.vertices[2]], //QUP +
[this.vertices[3], this.vertices[2], this.vertices[5]], //LPQ -
[this.vertices[5], this.vertices[9], this.vertices[3]], //QRL +
[this.vertices[3], this.vertices[4], this.vertices[9]], //LMR -
[this.vertices[9], this.vertices[8], this.vertices[4]], //RSM +
//Bottom Cone
[this.vertices[11], this.vertices[8], this.vertices[7]], //ZST
[this.vertices[11], this.vertices[7], this.vertices[6]], //ZTU
[this.vertices[11], this.vertices[5], this.vertices[6]], //ZQU
[this.vertices[11], this.vertices[5], this.vertices[9]], //ZQR
[this.vertices[11], this.vertices[9], this.vertices[8]] //ZRS
]
}
//Render Function
function render(objects, ctx, dx, dy){
//Clears last render
ctx.clearRect(0, 0, 2*dx, 2*dy);
for(var i =0, numbObjects = objects.length; i < numbObjects; ++i){
for(var j=0, numbFaces = objects[i].faces.length; j < numbFaces; ++j){
var face = objects[i].faces[j];//gets current face
var P = project(face[0]); //P is projection of first vertex
ctx.beginPath();
ctx.moveTo(P.x + dx, -P.y + dy);//moves pen to projection of first vertex
for (var k=0, numbVertices = face.length; k < numbVertices; ++k){//draws line to each 2d projection of each vertex to draw shape
P = project(face[k]);
ctx.lineTo(P.x + dx, -P.y + dy);
}
ctx.closePath();
ctx.stroke();
ctx.fill();
}//Loops through each face in object
}//Loops through all objects in objects array
}
//2D projection of 3D point
function project(M){
return new Vertex2D(M.x, M.z);
}
//Rotates a vertex using a rotation matrix
function rotate(M, center, theta, pi){
//Rotation matrix
var ct = Math.cos(theta);
var st = Math.sin(theta);
var cp = Math.cos(pi);
var sp = Math.sin(pi);
//vector from center to vertex
var x = M.x - center.x;
var y = M.y - center.y;
var z = M.z - center.z;
//rotate
M.x = ct * x - st * cp * y + st * sp * z + center.x;
M.y = st * x + ct * cp * y - ct * sp * z + center.y;
M.z = sp * y + cp * z + center.z;
}
//Autorates if theres no input
function autoRotate(){
if(mousedown == false){
for (var z = 0; z < objects.length; ++z){
for (var i = 0; i < objects[z].vertices.length; i++){
rotate(objects[z].vertices[i], cube_center, -Math.PI/720, Math.PI/720);
}
}
render(objects, ctx, dx, dy);
setTimeout(autoRotate, 5);
}
}
//Event when mousedown, sets reference mouse position
function initiateMove(evt){
mousedown = true;
cx = evt.clientX;
cy = evt.clientY;
}
//Moves Shapes, event on mouse move
function move(evt){
if(mousedown == true){//Check that mouse is down
var theta = 0.6*(cx - evt.clientX)*Math.PI/360;//difference between reference position and current postion, converted into angle, x axis
var pi = 0.6*(cy - evt.clientY)*Math.PI/180;//difference between reference position and current postion, converted into angle, y axis
for (var z = 0; z < objects.length; ++z){//runs through each object
for (var i = 0; i < objects[z].vertices.length; i++){//Thoguh each vertex
rotate(objects[z].vertices[i], cube_center, theta, pi);//Rotates each vertex
}
}
render(objects, ctx, dx, dy);//rerenders
cx = evt.clientX;//update reference mosue position
cy = evt.clientY;
}
}
function stopMove(){//evt mosue up
mousedown = false;
setTimeout(autoRotate(objects), 1000);
}
function clearObjects(){
objects = [];
}
function newShape(shape){
clearObjects();
switch(shape){
case 'cube' :
objects[0] = new Cube(cube_center, dy);
break;
case 'pyramid' :
objects[0] = new Pyramid(cube_center, dy/2, dy);
break;
case 'icosahedron' :
objects[0] = new Icosahedron(cube_center, dy/2);
break;
default:
console.log("Invalid Shape");
}
stopMove();
}
//canvas setup
var canvas = document.getElementById('myChart');
var ctx = canvas.getContext('2d');
ctx.canvas.width = window.innerWidth -200;
ctx.canvas.height = window.innerHeight - 200;
ctx.strokeStyle = 'rgba(0, 0, 0, 0.6)';
ctx.fillStyle = 'rgba(72, 209, 204, 0.3)';
//reference center
var dx = canvas.width / 2;
var dy = canvas.height / 2;
var cube_center = new Vertex(0,11*dy/10,0);
var cube1 = new Cube(cube_center, dy);
var cube2 = new Cube(cube_center, dy/2);
var pyramid1 = new Pyramid(cube_center, dy/2, dy);
var pyramid2 = new Pyramid(cube_center, dy/4, dy/2);
var icosahedron1 = new Icosahedron(cube_center, dy/2);
var objects = [cube1, pyramid1, icosahedron1];
//event setup
canvas.addEventListener('mousedown', initiateMove);
document.addEventListener('mousemove', move);
document.addEventListener('mouseup', stopMove);
var cx = 0;
var cy = 0;
var mousedown = false;
//first render
render(objects, ctx, dx, dy);