-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsolarsystem.html
306 lines (263 loc) · 15.6 KB
/
solarsystem.html
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
<!doctype html>
<html>
<head>
<title>JS1k, 1k demo submission [id]</title>
<meta charset="utf-8" />
</head>
<body>
<canvas id="c"></canvas>
<script>
(function(window, Math, parseInt) {
//canvas.style.backgroundColor = '#' + bg;
var canvas = document.getElementById('c'),
context = canvas.getContext('2d'),
width = 480,
height = 320,
bg = '000000',
origin = [0, 0, 0],
maxDistance = 1e5,
rads = [0.000016267, 0.000040667, 0.000042667, 0.000022667, 0.00048, 0.0004, 0.000173333, 0.000166667],
AmbientLight = '888888',
colors = ['aaaaaa', 'ddaa88', '67aaff', 'ff0000', '445566', '009900', 'cc9900', '6666ff'],
SIXTEEN = 16, i,
// julian centuries since 2000
T = Math.floor((new Date().getTime()-new Date(2000, 1, 1).getTime())/864e5)/36525,
// astrotable minified with T = 'e'
// [[.38709927+e*37e-8,.20563503+e*1906e-8,.12225995-e*10380e-8,252.25032350+e*149472.67411175,77.45779628+e*0.16047689,48.33076593-e*0.12534081],[.72333566+e*39e-7,677672e-8-e*4107e-8,.05924827-e*1377e-8,181.97909950+e*58517.81538729,131.60246718+e*268329e-8,76.67984255+e*0.27769418],[1.00000261+e*562e-8,.01671123-e*4392e-8,-26720991e-8-e*22596e-8,100.46457166+e*35999.37244981,102.93768193+e*0.32327364,0],[1.52371034+e*1847e-8,.09339410+e*7882e-8,.03228321-e*14192e-8,e*19140-4.55343205,e*0.44441088-23.94362959,49.55953891-e*0.29257343],[5.20288700-e*11607e-8,.04838624-e*13253e-8,.02276602-e*3206e-8,34.39644051+e*3034.74612775,14.72847983+e*0.21252668,100.47390909+e*0.20469106],[9.53667594-e*125060e-8,.05386179-e*50991e-8,.04338874+e*3379e-8,49.95424423+e*1222.49362201,92.59887831-e*0.41897216,113.66242448-e*0.28867794],[19.18916464-e*196176e-8,.04725744+e*4397e-8,.01348507-e*4240e-8,313.23810451+e*428.48202785,170.95427630+e*0.40805281,74.01692503+e*0.04240589],[30.06992276+e*26291e-8,859048e-8+e*5105e-8,.03089309+e*617e-8,e*218.45945325-55.12002969,44.96476227-e*0.32241464,131.78422574-e*508664e-8]]
astrotable = [
// mercury
//a: 0 e: 1 I: 2 L: 3 omega_: 4 Omega: 5
[.38709927+T*37e-8,.20563503+T*1906e-8,.12225995-T*10380e-8,252.25032350+T*149472.67411175,77.45779628+T*0.16047689,48.33076593-T*0.12534081],
// venus
[.72333566+T*39e-7,677672e-8-T*4107e-8,.05924827-T*1377e-8,181.97909950+T*58517.81538729,131.60246718+T*268329e-8,76.67984255+T*0.27769418],
// earth
[1.00000261+T*562e-8,.01671123-T*4392e-8,-26720991e-8-T*22596e-8,100.46457166+T*35999.37244981,102.93768193+T*0.32327364,0],
// mars
[1.52371034+T*1847e-8,.09339410+T*7882e-8,.03228321-T*14192e-8,T*19140-4.55343205,T*0.44441088-23.94362959,49.55953891-T*0.29257343],
// jupiter
[5.20288700-T*11607e-8,.04838624-T*13253e-8,.02276602-T*3206e-8,34.39644051+T*3034.74612775,14.72847983+T*0.21252668,100.47390909+T*0.20469106],
// saturn
[9.53667594-T*125060e-8,.05386179-T*50991e-8,.04338874+T*3379e-8,49.95424423+T*1222.49362201,92.59887831-T*0.41897216,113.66242448-T*0.28867794],
// uranus
[19.18916464-T*196176e-8,.04725744+T*4397e-8,.01348507-T*4240e-8,313.23810451+T*428.48202785,170.95427630+T*0.40805281,74.01692503+T*0.04240589],
// neptune
[30.06992276+T*26291e-8,859048e-8+T*5105e-8,.03089309+T*617e-8,T*218.45945325-55.12002969,44.96476227-T*0.32241464,131.78422574-T*508664e-8]
],
sin = Math.sin,
cos = Math.cos,
PI = Math.PI,
center = function(i) {
var omega = Math.PI/180*(astrotable[i][4]-astrotable[i][5]),
M = Math.PI/180*(astrotable[i][3]-astrotable[i][4]),
e = astrotable[i][1],
r = astrotable[i][0],
I = astrotable[i][2],
fixit = function(x0, eps) {
var x;
do {
x = x0;
x0 = M + e*Math.sin(x);
} while(Math.abs(x-x0) > eps);
return x0;
},
E, u;
for(;M<-PI||M>PI;M+=(M<0?1:-1)*2*PI){}
E = fixit(M, 1/maxDistance);
u = omega + 2*Math.atan(Math.sqrt((1+e)/(1-e))*Math.tan(E/2));
r *= (1-e*cos(E));
return [r*(cos(u)*cos(astrotable[i][5])-sin(u)*cos(I)*sin(astrotable[i][5])),r*(cos(u)*sin(astrotable[i][5])+sin(u)*cos(I)*cos(astrotable[i][5])),r*(sin(u)*sin(I))];
},
// normalize a vector
normalize = function(v, f) {
var d = f ? 1/f : Math.sqrt(sp(v,v));
if( Math.abs(d) < 1e-8) return v;
return [v[0]/d, v[1]/d, v[2]/d];
},
// subtract or add vectors: a - f*b
vs = function(a, b, f) {
f = f ? f : 1;
return [a[0]-f*b[0], a[1]-f*b[1], a[2]-f*b[2]];
},
// scalar product
sp = function(a, b) {
return a[0]*b[0] + a[1]*b[1] + a[2]*b[2];
},
// cross product
cp = function(a, b) {
return [a[1]*b[2]-a[2]*b[1], a[2]*b[0]-a[0]*b[2], a[0]*b[1]-a[1]*b[0]];
},
// scene, this is a singleton
scene = (function() {
// object list
var objectList = [],
lightList = [];
return {
// add object to scene
// object should have a method c which
// calculates the intersection with a
// given ray and a property co
a: function(object) {
objectList.push(object);
},
// add light
l: function(light) {
lightList.push(light);
},
// trace
t: function(start, direction) {
var minDistance = maxDistance, intensity=[],
i, j, k, numberOfObjects = objectList.length, numberOfLights = lightList.length,
objectIndex = numberOfObjects, shadow = false,
distance, normal, ref, nv, intersect, color, phong_size, phong_amount;
// find object nearest to the camera
for (i = 0; i < numberOfObjects; i++) {
distance = objectList[i].i(start, direction);
if (distance > 0 && distance < minDistance) {
minDistance = distance;
objectIndex = i;
}
}
// ray didn't hit any object
if (objectIndex == numberOfObjects)
color = bg;
else {
//if(objectIndex == 0) return 'ff0';
//i = objectList[objectIndex].c;
// Farbe am Schnittpunkt berechnen.
intersect = vs(start, direction, -minDistance);
// Normalenvektor im Schnittpunkt ermitteln
normal = objectList[objectIndex].n(intersect);
// Reflektierten Strahl berechnen (Spiegelung des einfallenden Strahls am Normalenvektor)
ref = vs(start, intersect);
nv = sp(normal, ref);
ref = vs([0,0,0], vs(ref, normal, 2*nv));
// the same only longer:
//for(i=0;i<3;i++)ref[i]=2*distance_normal[i]*nv-ref[i];
// Beleuchtung:
color = AmbientLight;
for(i = 0; i < numberOfLights; i++) {
direction = lightList[i].c;
// Schattenstrahl:
// don't trace to sun: j=1
for(j = 0; j < numberOfObjects && !shadow; j++) {
if(j != objectIndex && objectList[j].i(intersect, normalize(direction)) > 0)
shadow = true;
}
if(!shadow) {
// Diffuse:
j = sp(normal, direction);
for(k=0;k<3;k++) {
intensity[k] = parseInt(lightList[i].co[2*k]+lightList[i].co[2*k+1], SIXTEEN)/256*j;
}
// Specular:
j = sp(ref, direction);
phong_size = 1;
phong_amount = 1;
j = 0;//Math.pow(j, phong_size) * phong_amount;
for(k=0;k<3;k++)
intensity[k] += parseInt(lightList[i].co[2*k]+lightList[i].co[2*k+1], SIXTEEN)/256*j;
}
}
for(k=0;k<3;k++) {
intensity[k] = (parseInt(color[2*k]+color[2*k+1], SIXTEEN)/256+intensity[k])*parseInt(objectList[objectIndex].c[2*k]+objectList[objectIndex].c[2*k+1], SIXTEEN)/256;
intensity[k] = pad(Math.ceil(Math.min(255, 16*intensity[k])).toString(SIXTEEN));
}
color = intensity.join('');
}
return color;
},
// render scene
r: function(canvas) {
var i, j,
color, lastcolor,
zx = vs(look_at, eye),
xx = normalize(vs(cp(rotation, zx), look_at)),
yx = normalize(vs(cp(xx, zx), look_at)),
top_left = vs(vs(look_at, xx), yx, -height/width);
for (j = 0; j < height; j++) {
for (i = 0; i < width; i++) {
color = this.t(eye, normalize(vs(vs(top_left, xx, -i/(width-1)*2), yx, j/(width-1)*2)));
if(color !== lastcolor) canvas.fillStyle = '#'+color;
lastcolor = color;
canvas.fillRect(i, j, 1, 1);
}
}
}
}
})(),
// make sphere
sphere = function(center, radius, color) {
return {
c: color,
// set center
sc: function(newcenter) {
center = newcenter;
},
// calculate the intersection
i: function(start, direction) {
var i, SQ, DV, V;
V = vs(start, center);
DV = sp(V, direction);
SQ = DV * DV - (sp(V,V) - radius * radius);
if (SQ < 0) {
//no intersection
i = -1;
} else {
// intersection
SQ = Math.sqrt(SQ);
i = Math.min(SQ-DV,-DV-SQ);
}
return i;
},
// normal
n: function(p) {
return normalize(vs(p, center), 1/radius);
}
};
},
light = function(center, color) {
return {
co: color,
c: center
};
},
pad = function(s) {
if(s.length==1)
s = '0'+s;
return s;
},
// UPS: universe positioning system
phi = PI,
theta = PI/2,
eyer = 6,
eye,
dir = 1,
look_at,
rotation;
canvas.width = width;
canvas.height = height;
// sun
scene.a(sphere(origin, 4.64e-3*50, 'ffff00'));
scene.l(light([30,0,5], 'aaaaaa'));
scene.l(light([-30,0,-5], 'aaaaaa'));
//var cen;
for(i=0;i<astrotable.length;i++)
scene.a(sphere(center(i), rads[i]*1000, colors[i]));
/* move forward and back angle = 0.5;*/
window.setTimeout(function() {
eye = [eyer*sin(theta)*cos(phi), eyer*sin(theta)*sin(phi), eyer*cos(theta)];
rotation = [sin(theta+1)*cos(phi), sin(theta+1)*sin(phi), cos(theta+1)];
look_at = vs(origin, normalize(vs(origin, eye)), -5);
scene.r(context);
phi += PI/16;
if(phi>2*PI)
phi-=2*PI;
eyer += dir*2;
if(eyer <= 6 || eyer >= 35)
dir*=-1;
window.setTimeout(arguments.callee, 1);
}, 1);
})(window, Math, parseInt);
</script>
</body>
</html>