Death Star: Difference between revisions
→{{header|Sidef}}: simplified the code to use the Vector class and added link to the output image
(Maple Version) |
(→{{header|Sidef}}: simplified the code to use the Vector class and added link to the output image) |
||
Line 1,600:
{{trans|Perl}}
Writes a PGM to stdout.
<lang ruby>func
▲ y -= sph[1];
z.sqrt!
[sph[2] - z, sph[2] + z]
}
func normalize(v) {
}
func dot(x, y) {
}
var pos = [120, 120, 0, 120]
var neg = [-77, -33, -100, 190]
var light = normalize(
func draw(k, amb) {
STDOUT.binmode(':raw')
print ("P5\n", pos[0]*2 + 3, " ", pos[1]*2 + 3, "\n255\n")
for y in ((pos[1] - pos[3] - 1) .. (pos[1] + pos[3] + 1)) {
var row = []
for x in ((pos[0] - pos[3] - 1) .. (pos[0] + pos[3] + 1)) {
var hit = 0;▼
var
var
if (!h) { hit = 0; h = [0, 0] }
Line 1,644 ⟶ 1,640:
elsif (hs[0] > h[0]) { hit = 1 }
elsif (hs[1] > h[0]) { hit = (hs[1] > h[1] ? 0 : 2) }
else { hit = 1 }
▲ var (val, v);
given(hit) {
when (0) { val = 0}
when (1) { v =
default { v =
}
if (defined(v)) {
v = normalize(v)
val = int((dot(v, light)**k + amb) * 255)
val = (val > 255 ? 255 : (val < 0 ? 0 : val))
}
row.append(val)
}
print 'C*'.pack(row...)
}
}
draw(2, 0.2)
Output image: [https://github.com/trizen/rc/blob/master/img/death_star_sidef.png here].
=={{header|Tcl}}==
|