Draw a sphere: Difference between revisions

Content added Content deleted
(Added Julia language)
m (→‎{{header|REXX}}: added/changed comments and whitespace, simplified and optimized a function, used a template for the output.)
Line 3,936: Line 3,936:
=={{header|REXX}}==
=={{header|REXX}}==
This program is modeled after the   '''C'''   version.
This program is modeled after the   '''C'''   version.

<br>The REXX language doesn't have a &nbsp; '''SQRT''' &nbsp; function, so a version is included here.
The REXX language doesn't have a &nbsp; '''SQRT''' &nbsp; function, so a version is included here.
<br>Same with the &nbsp; '''CEIL'''ing &nbsp; and &nbsp; '''FLOOR''' &nbsp; functions.
<br>Same with the &nbsp; '''CEIL'''ing &nbsp; and &nbsp; '''FLOOR''' &nbsp; functions.

<br><br>Programming note: &nbsp; the output will appear slightly different when executed on an EBCDIC machine &nbsp; (due to different dithering characters).
Programming note: &nbsp; the output will appear slightly different when executed on an EBCDIC machine &nbsp; (due to different dithering characters).
<lang rexx>/*REXX program expresses a lighted sphere with simple characters used for shading. */
<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 19, 4, 2/10 /*draw a sphere with a radius of 19. */
Line 3,953: Line 3,955:
lightSource= '30 30 -50' /*position of light source.*/
lightSource= '30 30 -50' /*position of light source.*/
parse value norm(lightSource) with s1 s2 s3 /*normalize light source. */
parse value norm(lightSource) with s1 s2 s3 /*normalize light source. */
sLen=length(shading)-1; rr=r*r /*handy─dandy variables. */
shadeLen=length(shading) - 1; rr=r**2; r2=r+r /*handy─dandy variables. */


do i=floor(-r) to ceil(r) ; x= i+.5; xx=x**2; $=
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
do j=floor(-r2) to ceil(r2); y=j * .5 + .5; yy=y**2
if xx+yy<=rr then do /*is point within sphere ? */
if xx+yy<=rr then do /*is point within sphere ? */
parse value norm(x y sqrt(rr-xx-yy)) with v1 v2 v3
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*/
dot=s1*v1 + s2*v2 + s3*v3 /*the dot product of the Vs*/
if dot>0 then dot=0 /*if positive, make it zero*/
if dot>0 then dot=0 /*if positive, make it zero*/
b=abs(dot)**k + ambient /*calculate the brightness.*/
b=-dot**k + ambient /*calculate the brightness.*/
if b<=0 then brite=sLen
if b<=0 then brite=shadeLen
else brite=trunc( max( (1-b) * sLen, 0) )
else brite=max( (1-b) * shadeLen, 0) % 1
$=($)substr(shading,brite+1,1) /*construct a display line.*/
$=($)substr(shading, brite + 1, 1)
end
end /* [↑] build display line.*/
else $=$' ' /*append a blank to line. */
else $=$' ' /*append a blank to line. */
end /*j*/
end /*j*/ /*[↓] strip trailing blanks*/
say strip($, 'T') /*show a line of the sphere*/
say strip($, 'T') /*show a line of the sphere*/
end /*i*/ /* [↑] display the sphere.*/
end /*i*/ /* [↑] display the sphere.*/
Line 3,977: Line 3,979:
do k=j+5 to 0 by -1; numeric digits m.k; g=(g+x/g)*.5; end /*k*/
do k=j+5 to 0 by -1; numeric digits m.k; g=(g+x/g)*.5; end /*k*/
numeric digits d; return g/1</lang>
numeric digits d; return g/1</lang>
'''output''' &nbsp; when executed on an ASCII machine:
{{out|output|text=&nbsp; when using the default input and executed on an '''ASCII''' machine:}}
<pre>
<pre>
eeeeeeeeee@@@@@@@
eeeeeeeeee@@@@@@@