Task

Draw a sphere.

The sphere can be represented graphically, or in ASCII art, depending on the language capabilities.

Either static or rotational projection is acceptable for this task.



Related tasks

 draw a cuboid
 draw a rotating cube
 write language name in 3D ASCII
 draw a Deathstar


PROGRAM SPHERE
 
CONST SHADES$=".:!*oe&#%@"
 
DIM LIGHT[2],X[2],Y[2],V[2],VEC[2]
 
PROCEDURE DOT(X[],Y[]->D)
        D=X[0]*Y[0]+X[1]*Y[1]+X[2]*Y[2]
        IF D<0 THEN D=-D ELSE D=0 END IF
END PROCEDURE
 
PROCEDURE NORMALIZE(V[]->V[])
        LUNG=SQR(V[0]*V[0]+V[1]*V[1]+V[2]*V[2])
        V[0]=V[0]/LUNG
        V[1]=V[1]/LUNG
        V[2]=V[2]/LUNG
END PROCEDURE
 
PROCEDURE PDRAW(R,K,AMBIENT)
        FOR I=INT(-R) TO INT(R) DO
                X=I+0.5
                FOR J=INT(-2*R) TO INT(2*R) DO
                        Y=J/2+0.5
                        IF (X*X+Y*Y<=R*R) THEN
                                VEC[0]=X
                                VEC[1]=Y
                                VEC[2]=SQR(R*R-X*X-Y*Y)
                                NORMALIZE(VEC[]->VEC[])
                                DOT(LIGHT[],VEC[]->D)
                                B=D^K+AMBIENT
                                INTENSITY%=(1-B)*(LEN(SHADES$)-1)
                                IF (INTENSITY%<0) THEN INTENSITY%=0 END IF
                                IF (INTENSITY%>=LEN(SHADES$)-1) THEN
                                        INTENSITY%=LEN(SHADES$)-2
                                END IF
                                PRINT(#1,MID$(SHADES$,INTENSITY%+1,1);)
                           ELSE
                                PRINT(#1,(" ");)
                        END IF
                END FOR
                PRINT(#1,)
        END FOR
END PROCEDURE
 
BEGIN
    LIGHT[]=(30,30,-50)
    OPEN("O",1,"SPHERE.PRN")
       NORMALIZE(LIGHT[]->LIGHT[])
       PDRAW(10,2,0.4)
 
       PRINT(#1,STRING$(79,"="))
       PDRAW(20,4,0.1)
    CLOSE(1)
END PROGRAM
 