How to resolve the algorithm Draw a sphere step by step in the ERRE programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Draw a sphere step by step in the ERRE programming language

Table of Contents

Problem Statement

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.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Draw a sphere step by step in the ERRE programming language

Source code in the erre programming language

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

  

You may also check:How to resolve the algorithm Collections step by step in the Forth programming language
You may also check:How to resolve the algorithm Define a primitive data type step by step in the J programming language
You may also check:How to resolve the algorithm Day of the week step by step in the Simula programming language
You may also check:How to resolve the algorithm Möbius function step by step in the Nim programming language
You may also check:How to resolve the algorithm Accumulator factory step by step in the C# programming language