From: Svjatoslav Agejenko Date: Tue, 15 Oct 2024 18:11:53 +0000 (+0300) Subject: Refactoring code for better readability X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=commitdiff_plain;h=6fcf1b3456715011f6c72e7d98fca651158decf9;p=qbasicapps.git Refactoring code for better readability --- diff --git a/Graphics/3D/04vann2.bas b/Graphics/3D/04vann2.bas index 8c30f0a..ae73f1b 100755 --- a/Graphics/3D/04vann2.bas +++ b/Graphics/3D/04vann2.bas @@ -1,9 +1,8 @@ DECLARE SUB ruut2 (x!, y!, z!, s!) -DECLARE SUB ruut (x!, y!, z!, s!) -DECLARE SUB kuus (x, y, z, s) +DECLARE SUB ruut (x%, y%, z%, s%) +DECLARE SUB kuus (x!, y!, z!, s!) DECLARE SUB porand () -DECLARE SUB addp (x, y, z) -' kursor keys and to z, w - rotate +' Cursor keys and to z, w - rotate ' - speed down ' q - quit @@ -26,168 +25,167 @@ myx = 0 myy = 0 myz = -1000 - - start nait3d -SUB addp (x, y, z) -c = 1 +SUB addp (x%, y%, z%) + c = 1 -xn(nump + 1) = -100 + x -yn(nump + 1) = y -zn(nump + 1) = -100 + z + ' Define the vertices of a square in 3D space + xn(nump + 1) = -100 + x + yn(nump + 1) = y + zn(nump + 1) = -100 + z -xn(nump + 2) = 100 + x -yn(nump + 2) = y -zn(nump + 2) = -100 + z + xn(nump + 2) = 100 + x + yn(nump + 2) = y + zn(nump + 2) = -100 + z -xn(nump + 3) = 100 + x -yn(nump + 3) = y -zn(nump + 3) = 100 + z + xn(nump + 3) = 100 + x + yn(nump + 3) = y + zn(nump + 3) = 100 + z -xn(nump + 4) = -100 + x -yn(nump + 4) = y -zn(nump + 4) = 100 + z + xn(nump + 4) = -100 + x + yn(nump + 4) = y + zn(nump + 4) = 100 + z -point1(numl + 1) = nump + 1 -point2(numl + 1) = nump + 2 -col(numl + 1) = c + ' Define the edges of the square + point1(numl + 1) = nump + 1 + point2(numl + 1) = nump + 2 + col(numl + 1) = c -point1(numl + 2) = nump + 2 -point2(numl + 2) = nump + 3 -col(numl + 2) = c + point1(numl + 2) = nump + 2 + point2(numl + 2) = nump + 3 + col(numl + 2) = c -point1(numl + 3) = nump + 3 -point2(numl + 3) = nump + 4 -col(numl + 3) = c + point1(numl + 3) = nump + 3 + point2(numl + 3) = nump + 4 + col(numl + 3) = c -point1(numl + 4) = nump + 4 -point2(numl + 4) = nump + 1 -col(numl + 4) = c + point1(numl + 4) = nump + 4 + point2(numl + 4) = nump + 1 + col(numl + 4) = c -nump = nump + 4 -numl = numl + 4 + ' Update the counters for the next square + nump = nump + 4 + numl = numl + 4 END SUB SUB getcor -xn(nump + 1) = -150 -yn(nump + 1) = -125 -zn(nump + 1) = -200 + ' Define the vertices of a square in 3D space + xn(nump + 1) = -150 + yn(nump + 1) = -125 + zn(nump + 1) = -200 -xn(nump + 2) = 150 -yn(nump + 2) = -125 -zn(nump + 2) = -200 + xn(nump + 2) = 150 + yn(nump + 2) = -125 + zn(nump + 2) = -200 -xn(nump + 3) = 150 -yn(nump + 3) = 125 -zn(nump + 3) = -200 + xn(nump + 3) = 150 + yn(nump + 3) = 125 + zn(nump + 3) = -200 -xn(nump + 4) = -150 -yn(nump + 4) = 125 -zn(nump + 4) = -200 + xn(nump + 4) = -150 + yn(nump + 4) = 125 + zn(nump + 4) = -200 + ' Define the edges of the square + point1(numl + 1) = nump + 1 + point2(numl + 1) = nump + 2 -xn(nump + 5) = -150 -yn(nump + 5) = -125 -zn(nump + 5) = 200 + point1(numl + 2) = nump + 2 + point2(numl + 2) = nump + 3 -xn(nump + 6) = 150 -yn(nump + 6) = -125 -zn(nump + 6) = 200 + point1(numl + 3) = nump + 3 + point2(numl + 3) = nump + 4 -xn(nump + 7) = 150 -yn(nump + 7) = 125 -zn(nump + 7) = 200 + point1(numl + 4) = nump + 4 + point2(numl + 4) = nump + 1 -xn(nump + 8) = -150 -yn(nump + 8) = 125 -zn(nump + 8) = 200 + ' Define the vertices of another square in 3D space + xn(nump + 5) = -150 + yn(nump + 5) = -125 + zn(nump + 5) = 200 + xn(nump + 6) = 150 + yn(nump + 6) = -125 + zn(nump + 6) = 200 -point1(numl + 1) = nump + 1 -point2(numl + 1) = nump + 2 + xn(nump + 7) = 150 + yn(nump + 7) = 125 + zn(nump + 7) = 200 -point1(numl + 2) = nump + 2 -point2(numl + 2) = nump + 3 + xn(nump + 8) = -150 + yn(nump + 8) = 125 + zn(nump + 8) = 200 -point1(numl + 3) = nump + 3 -point2(numl + 3) = nump + 4 + ' Define the edges of the second square + point1(numl + 5) = nump + 5 + point2(numl + 5) = nump + 6 -point1(numl + 4) = nump + 4 -point2(numl + 4) = nump + 1 + point1(numl + 6) = nump + 6 + point2(numl + 6) = nump + 7 -point1(numl + 5) = nump + 5 -point2(numl + 5) = nump + 6 + point1(numl + 7) = nump + 7 + point2(numl + 7) = nump + 8 -point1(numl + 6) = nump + 6 -point2(numl + 6) = nump + 7 - -point1(numl + 7) = nump + 7 -point2(numl + 7) = nump + 8 + point1(numl + 8) = nump + 8 + point2(numl + 8) = nump + 5 -point1(numl + 8) = nump + 8 -point2(numl + 8) = nump + 5 + ' Define the edges connecting the two squares into cube + point1(numl + 9) = nump + 5 + point2(numl + 9) = nump + 1 -point1(numl + 9) = nump + 5 -point2(numl + 9) = nump + 1 + point1(numl + 10) = nump + 6 + point2(numl + 10) = nump + 2 -point1(numl + 10) = nump + 6 -point2(numl + 10) = nump + 2 + point1(numl + 11) = nump + 7 + point2(numl + 11) = nump + 3 -point1(numl + 11) = nump + 7 -point2(numl + 11) = nump + 3 + point1(numl + 12) = nump + 8 + point2(numl + 12) = nump + 4 -point1(numl + 12) = nump + 8 -point2(numl + 12) = nump + 4 + ' Update the counters for the next set of vertices and edges + nump = nump + 8 + numl = numl + 12 -nump = nump + 8 -numl = numl + 12 + ' Define a pyramid in 3D space + xn(nump + 1) = -150 + yn(nump + 1) = -125 + 201 + zn(nump + 1) = 0 + xn(nump + 2) = -150 + yn(nump + 2) = -125 + 201 + zn(nump + 2) = 89 + xn(nump + 3) = -150 + yn(nump + 3) = -125 + zn(nump + 3) = 89 + xn(nump + 4) = -150 + yn(nump + 4) = -125 + zn(nump + 4) = 0 -xn(nump + 1) = -150 -yn(nump + 1) = -125 + 201 -zn(nump + 1) = 0 + ' Define the edges of the pyramid + point1(numl + 1) = nump + 1 + point2(numl + 1) = nump + 2 -xn(nump + 2) = -150 -yn(nump + 2) = -125 + 201 -zn(nump + 2) = 89 + point1(numl + 2) = nump + 2 + point2(numl + 2) = nump + 3 -xn(nump + 3) = -150 -yn(nump + 3) = -125 -zn(nump + 3) = 89 + point1(numl + 3) = nump + 3 + point2(numl + 3) = nump + 4 -xn(nump + 4) = -150 -yn(nump + 4) = -125 -zn(nump + 4) = 0 + point1(numl + 4) = nump + 4 + point2(numl + 4) = nump + 1 -point1(numl + 1) = nump + 1 -point2(numl + 1) = nump + 2 + ' Update the counters for the next set of vertices and edges + nump = nump + 4 + numl = numl + 4 -point1(numl + 2) = nump + 2 -point2(numl + 2) = nump + 3 -point1(numl + 3) = nump + 3 -point2(numl + 3) = nump + 4 - -point1(numl + 4) = nump + 4 -point2(numl + 4) = nump + 1 - -nump = nump + 4 -numl = numl + 4 - - -FOR x = -1000 TO 1000 STEP 250 -FOR z = -1000 TO 1000 STEP 250 -'addp x, -200, z -'addp x, 200, z -NEXT z -NEXT x porand END SUB @@ -196,17 +194,19 @@ SUB kuus (x, y, z, s) b = 0 f = .3925 +' Calculate the vertices of a hexagon in 3D space FOR a = 0 + f TO 6 + f STEP 6.28 / 8 -x1 = SIN(a) * s -y1 = COS(a) * s -b = b + 1 + x1 = SIN(a) * s + y1 = COS(a) * s + b = b + 1 -xn(nump + b) = x1 + x -yn(nump + b) = y -zn(nump + b) = y1 + z + xn(nump + b) = x + x1 + yn(nump + b) = y + zn(nump + b) = z + y1 NEXT a +' Define the edges of the hexagon point1(numl + 1) = nump + 1 point2(numl + 1) = nump + 2 col(numl + 1) = 12 @@ -227,7 +227,6 @@ point1(numl + 5) = nump + 5 point2(numl + 5) = nump + 6 col(numl + 5) = 12 - point1(numl + 6) = nump + 6 point2(numl + 6) = nump + 7 col(numl + 6) = 12 @@ -240,224 +239,249 @@ point1(numl + 8) = nump + 8 point2(numl + 8) = nump + 1 col(numl + 8) = 12 +' Update the counters for the next set of vertices and edges nump = nump + b numl = numl + 8 -'LOCATE 1, 1 -'PRINT b - - - END SUB SUB nait3d + ' Main loop to render the 3D scene 1 -myx = myx + SIN(deg1) * mye -myz = myz + COS(deg1) * mye - -myx = myx + COS(deg1) * myk -myz = myz - SIN(deg1) * myk - -deg1 = deg1 + d1 -Deg2 = Deg2 + d2 - -C1 = COS(deg1): S1 = SIN(deg1) -C2 = COS(Deg2): S2 = SIN(Deg2) - -FOR a = 1 TO nump - -xo = xn(a) - myx -yo = -yn(a) - myy -zo = zn(a) - myz - -x1 = (xo * C1 - zo * S1) -z1 = (xo * S1 + zo * C1) - -y1 = (yo * C2 - z1 * S2) -z2 = (yo * S2 + z1 * C2) - - -xo(a) = x(a) -yo(a) = y(a) -IF z2 < 20 THEN -x(a) = -1 -ELSE -x(a) = 320 + (x1 / z2 * 500) -y(a) = 240 + (y1 / z2 * 500) -END IF -NEXT - - -FOR a = 1 TO numl -p1 = point1(a) -p2 = point2(a) -IF xo(p1) = -1 OR xo(p2) = -1 THEN ELSE LINE (xo(p1), yo(p1))-(xo(p2), yo(p2)), 0 -IF x(p1) = -1 OR x(p2) = -1 THEN ELSE LINE (x(p1), y(p1))-(x(p2), y(p2)), col(a) -NEXT - - -K$ = INKEY$ -IF K$ <> "" THEN - -SELECT CASE K$ - -CASE CHR$(0) + "P" -mye = mye - 3 - -CASE CHR$(0) + "H" -mye = mye + 3 - -CASE CHR$(0) + "M" -myk = myk + 3 - -CASE CHR$(0) + "K" -myk = myk - 3 - -CASE "+" -myy = myy + 3 - -CASE "-" -myy = myy - 3 - -CASE "6" -d1 = d1 + .01 - -CASE "4" -d1 = d1 - .01 - -CASE "8" -d2 = d2 - .01 - -CASE "2" -d2 = d2 + .01 - - -CASE " " -d1 = d1 / 2 -d2 = d2 / 2 -d3 = d3 / 2 -mye = mye / 2 -myk = myk / 2 - -CASE "q" -SYSTEM - -CASE CHR$(27) -SYSTEM -END SELECT -END IF - -GOTO 1 + ' Update the position based on rotation + myx = myx + SIN(deg1) * mye + myz = myz + COS(deg1) * mye + + myx = myx + COS(deg1) * myk + myz = myz - SIN(deg1) * myk + + ' Update the rotation angles + deg1 = deg1 + d1 + Deg2 = Deg2 + d2 + + ' Calculate the rotation matrices + C1 = COS(deg1): S1 = SIN(deg1) + C2 = COS(Deg2): S2 = SIN(Deg2) + + ' Apply the rotation to each vertex + FOR a = 1 TO nump + xo = xn(a) - myx + yo = -yn(a) - myy + zo = zn(a) - myz + + x1 = (xo * C1 - zo * S1) + z1 = (xo * S1 + zo * C1) + + y1 = (yo * C2 - z1 * S2) + z2 = (yo * S2 + z1 * C2) + + ' Project the vertex onto the 2D screen + xo(a) = x(a) + yo(a) = y(a) + IF z2 < 20 THEN + x(a) = -1 + ELSE + x(a) = 320 + (x1 / z2 * 500) + y(a) = 240 + (y1 / z2 * 500) + END IF + NEXT + + ' Draw the edges of each shape + FOR a = 1 TO numl + p1 = point1(a) + p2 = point2(a) + IF xo(p1) = -1 OR xo(p2) = -1 THEN + ' Skip drawing if the vertex is off-screen + ELSE + ' erase edge on old coordinates + LINE (xo(p1), yo(p1))-(xo(p2), yo(p2)), 0 + END IF + + IF x(p1) = -1 OR x(p2) = -1 THEN + ' Skip drawing if the vertex is off-screen + ELSE + ' draw edge on new coordinates + LINE (x(p1), y(p1))-(x(p2), y(p2)), col(a) + END IF + NEXT + + ' Handle user input + K$ = INKEY$ + IF K$ <> "" THEN + + SELECT CASE K$ + + CASE CHR$(0) + "P" + mye = mye - 3 + + CASE CHR$(0) + "H" + mye = mye + 3 + + CASE CHR$(0) + "M" + myk = myk + 3 + + CASE CHR$(0) + "K" + myk = myk - 3 + + CASE "+" + myy = myy + 3 + + CASE "-" + myy = myy - 3 + + CASE "6" + d1 = d1 + .01 + + CASE "4" + d1 = d1 - .01 + + CASE "8" + d2 = d2 - .01 + + CASE "2" + d2 = d2 + .01 + + CASE " " + d1 = d1 / 2 + d2 = d2 / 2 + d3 = d3 / 2 + mye = mye / 2 + myk = myk / 2 + + CASE "q" + SYSTEM + + CASE CHR$(27) + SYSTEM + END SELECT + END IF + + ' Continue the main loop + GOTO 1 END SUB SUB porand -FOR x = -100 TO 0 STEP 12.067 + .3 -FOR z = -100 TO 0 STEP 12.067 + .3 -kuus x, -125, z, 6.53 -ruut x + 6.033 + .15, -125, z + 6.033 + .15, 3.111 + .3 -NEXT z -NEXT x + ' Generate a grid of shapes in 3D space + FOR x = -100 TO 0 STEP 12.067 + .3 + FOR z = -100 TO 0 STEP 12.067 + .3 + kuus x, -125, z, 6.53 + ruut x + 6.033 + .15, -125, z + 6.033 + .15, 3.111 + .3 + NEXT z + NEXT x + + ' Generate another grid of shapes in 3D space + FOR y = -100 TO 0 STEP 20.3 + FOR x = -100 TO 0 STEP 20.3 + ruut2 x, y, 200, 10 + NEXT x + NEXT y -FOR y = -100 TO 0 STEP 20.3 -FOR x = -100 TO 0 STEP 20.3 -ruut2 x, y, 200, 10 -NEXT x -NEXT y +END SUB +SUB ruut (x%, y%, z%, s%) -END SUB + ' Define the vertices of a square in 3D space + xn(nump + 1) = x + yn(nump + 1) = y + zn(nump + 1) = z + s -SUB ruut (x, y, z, s) -xn(nump + 1) = x -yn(nump + 1) = y -zn(nump + 1) = z + s + xn(nump + 2) = x + s + yn(nump + 2) = y + zn(nump + 2) = z -xn(nump + 2) = x + s -yn(nump + 2) = y -zn(nump + 2) = z + xn(nump + 3) = x + yn(nump + 3) = y + zn(nump + 3) = z - s -xn(nump + 3) = x -yn(nump + 3) = y -zn(nump + 3) = z - s + xn(nump + 4) = x - s + yn(nump + 4) = y + zn(nump + 4) = z -xn(nump + 4) = x - s -yn(nump + 4) = y -zn(nump + 4) = z + ' Define the edges of the square + point1(numl + 1) = nump + 1 + point2(numl + 1) = nump + 2 + col(numl + 1) = 10 -point1(numl + 1) = nump + 1 -point2(numl + 1) = nump + 2 -col(numl + 1) = 10 + point1(numl + 2) = nump + 2 + point2(numl + 2) = nump + 3 + col(numl + 2) = 10 -point1(numl + 2) = nump + 2 -point2(numl + 2) = nump + 3 -col(numl + 2) = 10 + point1(numl + 3) = nump + 3 + point2(numl + 3) = nump + 4 + col(numl + 3) = 10 -point1(numl + 3) = nump + 3 -point2(numl + 3) = nump + 4 -col(numl + 3) = 10 + point1(numl + 4) = nump + 4 + point2(numl + 4) = nump + 1 + col(numl + 4) = 10 -point1(numl + 4) = nump + 4 -point2(numl + 4) = nump + 1 -col(numl + 4) = 10 + ' Update the counters for the next square + nump = nump + 4 + numl = numl + 4 -nump = nump + 4 -numl = numl + 4 END SUB SUB ruut2 (x, y, z, s) -xn(nump + 1) = x - s -yn(nump + 1) = y - s -zn(nump + 1) = z -xn(nump + 2) = x + s -yn(nump + 2) = y - s -zn(nump + 2) = z + ' Define the vertices of a square in 3D space + xn(nump + 1) = x - s + yn(nump + 1) = y - s + zn(nump + 1) = z -xn(nump + 3) = x + s -yn(nump + 3) = y + s -zn(nump + 3) = z + xn(nump + 2) = x + s + yn(nump + 2) = y - s + zn(nump + 2) = z -xn(nump + 4) = x - s -yn(nump + 4) = y + s -zn(nump + 4) = z + xn(nump + 3) = x + s + yn(nump + 3) = y + s + zn(nump + 3) = z -point1(numl + 1) = nump + 1 -point2(numl + 1) = nump + 2 -col(numl + 1) = 14 + xn(nump + 4) = x - s + yn(nump + 4) = y + s + zn(nump + 4) = z -point1(numl + 2) = nump + 2 -point2(numl + 2) = nump + 3 -col(numl + 2) = 14 + ' Define the edges of the square + point1(numl + 1) = nump + 1 + point2(numl + 1) = nump + 2 + col(numl + 1) = 14 -point1(numl + 3) = nump + 3 -point2(numl + 3) = nump + 4 -col(numl + 3) = 14 + point1(numl + 2) = nump + 2 + point2(numl + 2) = nump + 3 + col(numl + 2) = 14 -point1(numl + 4) = nump + 4 -point2(numl + 4) = nump + 1 -col(numl + 4) = 14 + point1(numl + 3) = nump + 3 + point2(numl + 3) = nump + 4 + col(numl + 3) = 14 + + point1(numl + 4) = nump + 4 + point2(numl + 4) = nump + 1 + col(numl + 4) = 14 -nump = nump + 4 -numl = numl + 4 + ' Update the counters for the next square + nump = nump + 4 + numl = numl + 4 END SUB SUB start -SCREEN 12 -CLS -FOR a = 1 TO 4000 -col(a) = 15 -NEXT a + ' Initialize the screen and clear it + SCREEN 12 + CLS -nump = 0 -numl = 0 + ' Set the initial color of all shapes + FOR a = 1 TO 4000 + col(a) = 15 + NEXT a + ' Initialize counters for vertices and edges + nump = 0 + numl = 0 -getcor + ' Generate the initial set of shapes + getcor END SUB