-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreflection.html
More file actions
215 lines (176 loc) · 8.48 KB
/
reflection.html
File metadata and controls
215 lines (176 loc) · 8.48 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
<HTML>
<BODY>
<canvas width='500' height='500' id='lab03'>
</canvas>
<script>
var canvas = document.getElementById('lab03');
var ctx = canvas.getContext('2d');
let x0 = 10;
let y0 = 10;
let x1 = 157;
let y1 = 127;
function line(ctx, x0, y0, x1, y1) {
let delta = 2*(y1 - y0);
let eps = 0;
let y = y0;
let scale = 1;
for (let x = x0; x<x1; x++) {
if (eps >= (x1 - x0)) {
y += 1;
eps -= 2*(x1-x0);
}
eps += delta;
ctx.fillRect(x*scale, y*scale, scale*1, scale*1);
}
}
class Point{
constructor(x, y) {
this.x = x;
this.y = y;
}
}
function fillTriangle(ctx, points, color) {
ctx.fillStyle = color;
let miny = points[0].y, maxy = points[0].y;
for (let i=1; i<points.length; i++) {
if (points[i].y > maxy) maxy = points[i].y;
if (points[i].y < miny) miny = points[i].y;
}
let yarray = [];
for (let i=0; i<points.length; i++) {
let p = 0;
if (i!=points.length-1) p = i+1;
let hi, lo;
if (points[i].y>points[p].y) {hi = i; lo = p;}
else if (points[i].y<points[p].y) {hi = p; lo = i;}
else continue;
if (points[hi].x == points[lo].x) {
for (let j=points[lo].y; j<points[hi].y; j++) {
if (typeof yarray[j] == 'undefined') yarray[j] = [];
yarray[j].push(points[lo].x);
}
} else {
let k = (points[hi].y - points[lo].y)/
(points[hi].x - points[lo].x);
for (let j=points[lo].y; j<points[hi].y; j++) {
if (typeof yarray[j] == 'undefined') yarray[j] = [];
yarray[j].push((j - points[lo].y)/k + points[lo].x);
}
}
}
let image_data = ctx.createImageData(500, 500);
for (let y=miny; y<maxy; y++) {
if (typeof(yarray[y]) == "undefined") {yarray[y] = []; }
let xarray = yarray[y].sort( function(a, b){ return a - b; } );
for (let j = 0; j<xarray.length/2; j++) {
for (let x = xarray[j*2]; x < xarray[j*2+1]; x++) {
ctx.fillRect(Math.floor(x), y, 1, 1);
image_data.data[(y*500+Math.floor(x))*4 + 0] = 56;
image_data.data[(y*500+Math.floor(x))*4 + 1] = 78;
image_data.data[(y*500+Math.floor(x))*4 + 2] = 23;
image_data.data[(y*500+Math.floor(x))*4 + 3] = 255;
}
}
}
}
let light = {x: 0, y: 0, z: -78};
let step = 5;
let alpha = 0;
let radius = 100;
let diamond_v = [
{x: 0.000000E+00 , y: 0.000000E+00, z: 78.0000},
{x: 45.0000 , y: 45.0000, z: 0.000000E+00 },
{x: 45.0000 , y: -45.0000, z: 0.000000E+00 },
{x: -45.0000 , y: -45.0000, z: 0.000000E+00 },
{x: -45.0000 , y: 45.0000, z: 0.000000E+00 },
{x: 0.000000E+00 , y: 0.000000E+00, z: -78.0000 },
];
let diamond_f = [
[ 1 , 2 , 3 ],
[ 1 , 3 , 4 ],
[ 1 , 4 , 5 ],
[ 1 , 5 , 2 ],
];
/*
[ 6 , 5 , 4 ],
[ 6 , 4 , 3 ],
[ 6 , 3 , 2 ],
[ 6 , 2 , 1 ],
[ 6 , 1 , 5 ],
];
*/
let fill_color_percent = 0.25;
let x_offset = 200, y_offset = 200;
let scale = 3;
let color_r = Math.random()*255;
let color_g = Math.random()*255;
let color_b = Math.random()*255;
let color = "rgb("+color_r+","+color_g+
","+color_b+")";
setInterval(function(){
ctx.clearRect(0,0,canvas.width, canvas.height);
alpha+=step;
if(alpha == 360)
alpha = 0;
let new_light = {x: light.x + radius*Math.cos(alpha/180*Math.PI), y: light.y + radius*Math.sin(alpha/180*Math.PI), z: light.z};
for (let f = 0; f < diamond_f.length; f++)
{
let points = [];
let p0_idx = diamond_f[f][0]-1;
let p1_idx = diamond_f[f][1]-1;
let p2_idx = diamond_f[f][2]-1;
//console.log(p0_idx+":p0 = "+diamond_v[p0_idx].x+" "+diamond_v[p0_idx].y+" "+diamond_v[p0_idx].z);
//console.log(p1_idx+":p1 = "+diamond_v[p1_idx].x+" "+diamond_v[p1_idx].y+" "+diamond_v[p1_idx].z);
//console.log(p2_idx+":p2 = "+diamond_v[p2_idx].x+" "+diamond_v[p2_idx].y+" "+diamond_v[p2_idx].z);
let vA = {x: diamond_v[p0_idx].x-diamond_v[p1_idx].x,
y: diamond_v[p0_idx].y-diamond_v[p1_idx].y,
z: diamond_v[p0_idx].z-diamond_v[p1_idx].z};
let vB = {x: diamond_v[p2_idx].x-diamond_v[p1_idx].x,
y: diamond_v[p2_idx].y-diamond_v[p1_idx].y,
z: diamond_v[p2_idx].z-diamond_v[p1_idx].z};
for (let i=0; i<diamond_f[f].length; i++) {
let index = diamond_f[f][i]-1;
points.push(new Point(scale*diamond_v[index].x+x_offset,
scale*diamond_v[index].y+y_offset));
}
let vN = {x:0, y:0, z:1};
//console.log("A = "+vA.x+" "+vA.y+" "+vA.z);
//console.log("B = "+vB.x+" "+vB.y+" "+vB.z);
vN.y = (vB.x*vA.z-vB.z*vA.x)/(vB.y*vA.x-vB.x*vA.y);
vN.x = -1*(vA.z + vN.y*vA.y)/vA.x;
//console.log("N = "+vN.x+" "+vN.y+" "+vN.z);
let vL = {x: new_light.x-diamond_v[p1_idx].x,
y: new_light.y - diamond_v[p1_idx].y,
z: new_light.z - diamond_v[p1_idx].z};
//console.log(p1_idx+":p1 = "+diamond_v[p1_idx].x+" "+diamond_v[p1_idx].y+" "+diamond_v[p1_idx].z);
//console.log("light = "+light.x+" "+light.y+" "+light.z);
//console.log("L = "+vL.x+" "+vL.y+" "+vL.z);
let vEye = {x:0, y:0, z:1};
let cosa = (vN.x*vL.x+vN.y*vL.y+vN.z*vL.z)/
Math.sqrt(vN.x*vN.x+vN.y*vN.y+vN.z*vN.z)/
Math.sqrt(vL.x*vL.x+vL.y*vL.y+vL.z*vL.z);
if (cosa < 0) cosa = 0;
//console.log("cos a = "+ cosa);
let v_reflected = {x:(2*vN.x*(vN.x*vL.x+vN.y*vL.y+vN.z*vL.z)/(vN.x*vN.x+vN.y*vN.y+vN.z*vN.z)) - vL.x,
y:(2*vN.y*(vN.x*vL.x+vN.y*vL.y+vN.z*vL.z)/(vN.x*vN.x+vN.y*vN.y+vN.z*vN.z)) - vL.y,
z:(2*vN.z*(vN.x*vL.x+vN.y*vL.y+vN.z*vL.z)/(vN.x*vN.x+vN.y*vN.y+vN.z*vN.z)) - vL.z}
//console.log("v_reflected ", v_reflected);
let cosb = (v_reflected.x*vEye.x+v_reflected.y*vEye.y+v_reflected.z*vEye.z)/
Math.sqrt(v_reflected.x*v_reflected.x+v_reflected.y*v_reflected.y+v_reflected.z*v_reflected.z)/
Math.sqrt(vEye.x*vEye.x+vEye.y*vEye.y+vEye.z*vEye.z);
if (cosb < 0) cosb = 0;
//console.log("cos b = "+ cosb);
c_r = (1-fill_color_percent)*color_r*cosb+fill_color_percent*color_r;
c_g = (1-fill_color_percent)*color_g*cosb+fill_color_percent*color_g;
c_b = (1-fill_color_percent)*color_b*cosb+fill_color_percent*color_b;
color = "rgb("
+c_r+","
+c_g+","
+c_b+")";
//console.log("color = "+color);
fillTriangle(ctx, points, color);
}
},17);
</script>
</BODY>
</HTML>