Anonymous user
Draw a sphere: Difference between revisions
m
→{{header|REXX}}: added/changed comments and whitespace, changed indentations.
m (→{{header|FreeBASIC}}: removed redundant code) |
m (→{{header|REXX}}: added/changed comments and whitespace, changed indentations.) |
||
Line 3,488:
<br>Same with the '''CEIL'''ing and '''FLOOR''' functions.
<br><br>Programming note: the output will appear slightly different when executed on an EBCDIC machine (due to different dithering characters).
<lang rexx>/*REXX program expresses a lighted sphere with simple characters used for shading. */
call drawSphere 19, 4, 2/10 /*draw a sphere with a radius of 19. */
call drawSphere 10, 2, 4/10 /* " " " " " " " ten. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
ceil: procedure; parse arg x;
floor: procedure; parse arg x;
norm: parse arg
/*──────────────────────────────────────────────────────────────────────────────────────*/
drawSphere: procedure; parse arg r, k, ambient /*get the arguments from CL*/
if
else shading= "·:!°oe@░▒▓" /* ASCII " " */
parse value norm(lightSource) with s1 s2 s3 /*normalize light source. */
sLen=length(shading)-1; rr=r*r /*handy─dandy variables. */
do i=floor(-r) to ceil(r) ; x= i+.5; xx=x**2; $=
do j=floor(-2*r) to ceil(r+r); y=j/2+.5; yy=y**2
if xx+yy<=rr then do /*is point within sphere ? */
parse value norm(x y sqrt(rr-xx-yy)) with v1 v2 v3
dot=s1*v1 + s2*v2 + s3*v3 /*the dot product of the Vs*/
if dot>0 then dot=0 /*if positive, make it zero*/
b=abs(dot)**k + ambient /*calculate the brightness.*/
if b<=0 then brite=sLen
else brite=trunc( max( (1-b) * sLen, 0) )
$=($)substr(shading,brite+1,1)
end
else $=$' ' /*append a blank to line. */
end /*j*/
say strip($, '
end /*i*/ /* [↑] display the sphere.*/
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
sqrt: procedure; parse arg x;
numeric digits
numeric digits d;
'''output''' when executed on an ASCII machine:
<pre>
|