-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCubes.dart
188 lines (125 loc) · 5.83 KB
/
Cubes.dart
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
#import('dart:dom');
class Cubes {
CanvasRenderingContext2D ctx;
var hfov = 100 * Math.PI / 180;
var vfov = 80 * Math.PI / 180;
List<Point> vertices;
var Xangle = 0;
var Yangle = 0;
var Zangle = 0;
Cubes() {
var doc = window.document;
HTMLCanvasElement canvas = doc.getElementById("canvas");
ctx = canvas.getContext("2d");
vertices = new List<Point>(8);
vertices[0] = new Point(-10,10,20);
vertices[1]=new Point(10,10,20);
vertices[2]=new Point(10,10,30);
vertices[3]=new Point(-10,10,30);
vertices[4]=new Point(-10,-10,20);
vertices[5]=new Point(10,-10,20);
vertices[6]=new Point(10,-10,30);
vertices[7]=new Point(-10,-10,30);
drawCube(vertices);
window.document.onkeydown = (KeyboardEvent e){
if(e.keyCode == 37){
Xangle = Xangle + 0.005;
for(var i = 0; i < vertices.length; i++){
// vertices[i].rotateX(Xangle);
// vertices[i].rotateY(Yangle);
// vertices[i].rotateZ(Zangle);
vertices[i] = vertices[i].rotateX(Xangle);
vertices[i] = vertices[i].rotateY(Yangle);
vertices[i] = vertices[i].rotateZ(Zangle);
}
drawCube(vertices);
}else if(e.keyCode == 38){
Yangle = Yangle + 0.005;
for(var i = 0; i < vertices.length; i++){
vertices[i] = vertices[i].rotateX(Xangle);
vertices[i] = vertices[i].rotateY(Yangle);
vertices[i] = vertices[i].rotateZ(Zangle);
}
drawCube(vertices);
}else if(e.keyCode == 39){
Xangle = Xangle - 0.005;
for(var i = 0; i < vertices.length; i++){
vertices[i] = vertices[i].rotateX(Xangle);
vertices[i] = vertices[i].rotateY(Yangle);
vertices[i] = vertices[i].rotateZ(Zangle);
}
drawCube(vertices);
}else if(e.keyCode == 40){
Yangle = Yangle + 0.005;
for(var i = 0; i < vertices.length; i++){
vertices[i] = vertices[i].rotateX(Xangle);
vertices[i] = vertices[i].rotateY(Yangle);
vertices[i] = vertices[i].rotateZ(Zangle);
}
drawCube(vertices);
}
};
}
drawCube(List<Point> vertices){
var hViewDistance = ( 300 / 2 ) / Math.tan( hfov / 2 );
var vViewDistance = ( 300 / 2 ) / Math.tan( vfov / 2 );
ctx.clearRect(0,0,300,300);
ctx.setFillColor("black");
ctx.setFillStyle("black");
ctx.fillRect(0,0,300,300);
ctx.setStrokeStyle("#444");
ctx.setLineWidth(2);
ctx.beginPath();
ctx.moveTo(vertices[0].projectedX(hViewDistance), vertices[0].projectedY(vViewDistance));
ctx.lineTo(vertices[1].projectedX(hViewDistance), vertices[1].projectedY(vViewDistance));
ctx.lineTo(vertices[2].projectedX(hViewDistance), vertices[2].projectedY(vViewDistance));
ctx.lineTo(vertices[3].projectedX(hViewDistance), vertices[3].projectedY(vViewDistance));
ctx.lineTo(vertices[0].projectedX(hViewDistance), vertices[0].projectedY(vViewDistance));
// Bottom polygon
ctx.lineTo(vertices[4].projectedX(hViewDistance), vertices[4].projectedY(vViewDistance));
ctx.lineTo(vertices[5].projectedX(hViewDistance), vertices[5].projectedY(vViewDistance));
ctx.lineTo(vertices[6].projectedX(hViewDistance), vertices[6].projectedY(vViewDistance));
ctx.lineTo(vertices[7].projectedX(hViewDistance), vertices[7].projectedY(vViewDistance));
ctx.lineTo(vertices[4].projectedX(hViewDistance), vertices[4].projectedY(vViewDistance));
// Joining middle lines
ctx.moveTo(vertices[1].projectedX(hViewDistance), vertices[1].projectedY(vViewDistance));
ctx.lineTo(vertices[5].projectedX(hViewDistance), vertices[5].projectedY(vViewDistance));
ctx.moveTo(vertices[2].projectedX(hViewDistance), vertices[2].projectedY(vViewDistance));
ctx.lineTo(vertices[6].projectedX(hViewDistance), vertices[6].projectedY(vViewDistance));
ctx.moveTo(vertices[3].projectedX(hViewDistance), vertices[3].projectedY(vViewDistance));
ctx.lineTo(vertices[7].projectedX(hViewDistance), vertices[7].projectedY(vViewDistance));
ctx.fill();
ctx.closePath();
ctx.stroke();
}
}
class Point{
Point(this.x,this.y,this.z);
var x,y,z;
projectedX(hViewDistance) => (this.x * hViewDistance) / this.z + 300/2 ;
projectedY(vViewDistance) => (300/2) - ( this.y * vViewDistance ) / this.z;
rotateX(angle){
//this.y = this.y * Math.cos(angle*Math.PI/180) - this.z * Math.sin(angle*Math.PI/180);
//this.z = this.y * Math.sin(angle*Math.PI/180) + this.z + Math.cos(angle*Math.PI/180);
var newy = this.y * Math.cos(angle) - this.z * Math.sin(angle);
var newz = this.y * Math.sin(angle) + this.z + Math.cos(angle);
return new Point(this.x, newy, newz);
}
rotateZ(angle){
//this.x = this.x * Math.cos(angle*Math.PI/180) - this.y * Math.sin(angle*Math.PI/180);
//this.y = this.x * Math.sin(angle*Math.PI/180) + this.y * Math.cos(angle*Math.PI/180);
var newx = this.x * Math.cos(angle) - this.y * Math.sin(angle);
var newy = this.x * Math.sin(angle) + this.y * Math.cos(angle);
return new Point(newx,newy,this.z);
}
rotateY(angle){
//this.x = this.x * Math.cos(angle*Math.PI/180) - this.z * Math.sin(angle*Math.PI/180);
//this.z = this.x * Math.sin(angle*Math.PI/180) + this.z * Math.cos(angle*Math.PI/180);
var newx = this.x * Math.cos(angle) - this.z * Math.sin(angle);
var newz = this.x * Math.sin(angle) + this.z * Math.cos(angle);
return new Point(newx,this.y, newz);
}
}
main(){
new Cubes();
}