Draw a sphere: Difference between revisions

Content added Content deleted
m ({{out}})
(→‎{{header|XPL0}}: Added zkl)
Line 3,247: Line 3,247:
SetVid($03); \restore normal text mode
SetVid($03); \restore normal text mode
]</lang>
]</lang>

=={{header|zkl}}==
Uses the PPM class from http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#zkl
{{trans|XPL0}}
<lang zkl>img:=PPM(640,480);
R:=100; R2:=R*R; //radius, in pixels; radius squared
X0:=640/2; Y0:=480/2; //coordinates of center of screen
foreach Y in ([-R..R]){ //for all the coordinates near the circle
foreach X in ([-R..R]){ // which is under the sphere
D2:=X*X + Y*Y;
C:=0; //default color is black
if(D2<=R2){ //coordinate is inside circle under sphere
Z:=(R2-D2).toFloat().sqrt();//height of point on surface of sphere above X,Y
C=0x82+Z-(X+Y)/2; //color is proportional; offset X and Y, and
} // shift color to upper limit of its range
img[X+X0,Y+Y0]=C.shiftLeft(8)+C; //green + blue = cyan
}
}
img.write(File("foo.ppm","wb"));</lang>
{{out}}See the XPL0 image.
The radius of 100 is the max before the color calculation overflows 24 bits so for a radius (R) of, say 200, use
<lang zkl>img[X+X0,Y+Y0]=C*140+C;</lang>