From 7d81674dca998b1b322c971b01239bd8b6bd52f5 Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko Date: Sat, 26 Oct 2024 22:14:51 +0300 Subject: [PATCH] Using AI to improve code readability --- Graphics/3D/04vann2.bas | 462 +++++++++++++++++++++------------------- 1 file changed, 238 insertions(+), 224 deletions(-) diff --git a/Graphics/3D/04vann2.bas b/Graphics/3D/04vann2.bas index ae73f1b..45eeba2 100755 --- a/Graphics/3D/04vann2.bas +++ b/Graphics/3D/04vann2.bas @@ -1,24 +1,40 @@ -DECLARE SUB ruut2 (x!, y!, z!, s!) -DECLARE SUB ruut (x%, y%, z%, s%) -DECLARE SUB kuus (x!, y!, z!, s!) -DECLARE SUB porand () -' Cursor keys and to z, w - rotate +' Program renders 3D room with various decorative tiles on the wall and on the floor. +' User can freely fly around and look at the room from different angles. +' +' By Svjatoslav Agejenko. +' Email: svjatoslav@svjatoslav.eu +' Homepage: http://www.svjatoslav.eu +' +' Changelog: +' ?, Initial version +' 2024, Improved program readability using AI +' +' Keyboard controls: +' , , , - move around +' 2, 6, 4, 8 - look around (number pad) ' - speed down ' q - quit +' + / - - move up / down +' + + +DECLARE SUB renderSquare2 (x!, y!, z!, s!) +DECLARE SUB renderSquare (x%, y%, z%, s%) +DECLARE SUB renderHexagon (x!, y!, z!, s!) +DECLARE SUB generateCube () DECLARE SUB start () -DECLARE SUB addsq (x1%, y1%, z1%) -DECLARE SUB getcor () -DECLARE SUB mulcor () -DECLARE SUB nait3d () -DECLARE SUB calcsin () -DIM SHARED xn(4000), yn(4000), zn(4000) -DIM SHARED x(4000), y(4000), z(4000) - -DIM SHARED xo(4000), yo(4000), zo(4000) -DIM SHARED point1(4000), point2(4000) +DECLARE SUB addSquare (x1%, y1%, z1%) +DECLARE SUB getCornerVertices () +DECLARE SUB multiplyCoordinates () +DECLARE SUB calculateSine () +DIM SHARED xVertex(4000), yVertex(4000), zVertex(4000) +DIM SHARED xCoordinate(4000), yCoordinate(4000), zCoordinate(4000) + +DIM SHARED xOriginal(4000), yOriginal(4000), zOriginal(4000) +DIM SHARED point1Vertex(4000), point2Vertex(4000) DIM SHARED col(4000) -DIM SHARED nump, numl +DIM SHARED vertexCount, edgeCount DIM SHARED myx, myy, myz, mye, myk myx = 0 @@ -29,168 +45,167 @@ start nait3d -SUB addp (x%, y%, z%) +SUB addSquare (x1%, y1%, z1%) c = 1 ' Define the vertices of a square in 3D space - xn(nump + 1) = -100 + x - yn(nump + 1) = y - zn(nump + 1) = -100 + z + xVertex(vertexCount + 1) = -100 + x1 + yVertex(vertexCount + 1) = y1 + zVertex(vertexCount + 1) = -100 + z1 - xn(nump + 2) = 100 + x - yn(nump + 2) = y - zn(nump + 2) = -100 + z + xVertex(vertexCount + 2) = 100 + x1 + yVertex(vertexCount + 2) = y1 + zVertex(vertexCount + 2) = -100 + z1 - xn(nump + 3) = 100 + x - yn(nump + 3) = y - zn(nump + 3) = 100 + z + xVertex(vertexCount + 3) = 100 + x1 + yVertex(vertexCount + 3) = y1 + zVertex(vertexCount + 3) = 100 + z1 - xn(nump + 4) = -100 + x - yn(nump + 4) = y - zn(nump + 4) = 100 + z + xVertex(vertexCount + 4) = -100 + x1 + yVertex(vertexCount + 4) = y1 + zVertex(vertexCount + 4) = 100 + z1 ' Define the edges of the square - point1(numl + 1) = nump + 1 - point2(numl + 1) = nump + 2 - col(numl + 1) = c + point1Vertex(edgeCount + 1) = vertexCount + 1 + point2Vertex(edgeCount + 1) = vertexCount + 2 + col(edgeCount + 1) = c - point1(numl + 2) = nump + 2 - point2(numl + 2) = nump + 3 - col(numl + 2) = c + point1Vertex(edgeCount + 2) = vertexCount + 2 + point2Vertex(edgeCount + 2) = vertexCount + 3 + col(edgeCount + 2) = c - point1(numl + 3) = nump + 3 - point2(numl + 3) = nump + 4 - col(numl + 3) = c + point1Vertex(edgeCount + 3) = vertexCount + 3 + point2Vertex(edgeCount + 3) = vertexCount + 4 + col(edgeCount + 3) = c - point1(numl + 4) = nump + 4 - point2(numl + 4) = nump + 1 - col(numl + 4) = c + point1Vertex(edgeCount + 4) = vertexCount + 4 + point2Vertex(edgeCount + 4) = vertexCount + 1 + col(edgeCount + 4) = c ' Update the counters for the next square - nump = nump + 4 - numl = numl + 4 + vertexCount = vertexCount + 4 + edgeCount = edgeCount + 4 END SUB -SUB getcor +SUB getCornerVertices ' Define the vertices of a square in 3D space - xn(nump + 1) = -150 - yn(nump + 1) = -125 - zn(nump + 1) = -200 + xVertex(vertexCount + 1) = -150 + yVertex(vertexCount + 1) = -125 + zVertex(vertexCount + 1) = -200 - xn(nump + 2) = 150 - yn(nump + 2) = -125 - zn(nump + 2) = -200 + xVertex(vertexCount + 2) = 150 + yVertex(vertexCount + 2) = -125 + zVertex(vertexCount + 2) = -200 - xn(nump + 3) = 150 - yn(nump + 3) = 125 - zn(nump + 3) = -200 + xVertex(vertexCount + 3) = 150 + yVertex(vertexCount + 3) = 125 + zVertex(vertexCount + 3) = -200 - xn(nump + 4) = -150 - yn(nump + 4) = 125 - zn(nump + 4) = -200 + xVertex(vertexCount + 4) = -150 + yVertex(vertexCount + 4) = 125 + zVertex(vertexCount + 4) = -200 ' Define the edges of the square - point1(numl + 1) = nump + 1 - point2(numl + 1) = nump + 2 + point1Vertex(edgeCount + 1) = vertexCount + 1 + point2Vertex(edgeCount + 1) = vertexCount + 2 - point1(numl + 2) = nump + 2 - point2(numl + 2) = nump + 3 + point1Vertex(edgeCount + 2) = vertexCount + 2 + point2Vertex(edgeCount + 2) = vertexCount + 3 - point1(numl + 3) = nump + 3 - point2(numl + 3) = nump + 4 + point1Vertex(edgeCount + 3) = vertexCount + 3 + point2Vertex(edgeCount + 3) = vertexCount + 4 - point1(numl + 4) = nump + 4 - point2(numl + 4) = nump + 1 + point1Vertex(edgeCount + 4) = vertexCount + 4 + point2Vertex(edgeCount + 4) = vertexCount + 1 ' Define the vertices of another square in 3D space - xn(nump + 5) = -150 - yn(nump + 5) = -125 - zn(nump + 5) = 200 + xVertex(vertexCount + 5) = -150 + yVertex(vertexCount + 5) = -125 + zVertex(vertexCount + 5) = 200 - xn(nump + 6) = 150 - yn(nump + 6) = -125 - zn(nump + 6) = 200 + xVertex(vertexCount + 6) = 150 + yVertex(vertexCount + 6) = -125 + zVertex(vertexCount + 6) = 200 - xn(nump + 7) = 150 - yn(nump + 7) = 125 - zn(nump + 7) = 200 + xVertex(vertexCount + 7) = 150 + yVertex(vertexCount + 7) = 125 + zVertex(vertexCount + 7) = 200 - xn(nump + 8) = -150 - yn(nump + 8) = 125 - zn(nump + 8) = 200 + xVertex(vertexCount + 8) = -150 + yVertex(vertexCount + 8) = 125 + zVertex(vertexCount + 8) = 200 ' Define the edges of the second square - point1(numl + 5) = nump + 5 - point2(numl + 5) = nump + 6 + point1Vertex(edgeCount + 5) = vertexCount + 5 + point2Vertex(edgeCount + 5) = vertexCount + 6 - point1(numl + 6) = nump + 6 - point2(numl + 6) = nump + 7 + point1Vertex(edgeCount + 6) = vertexCount + 6 + point2Vertex(edgeCount + 6) = vertexCount + 7 - point1(numl + 7) = nump + 7 - point2(numl + 7) = nump + 8 + point1Vertex(edgeCount + 7) = vertexCount + 7 + point2Vertex(edgeCount + 7) = vertexCount + 8 - point1(numl + 8) = nump + 8 - point2(numl + 8) = nump + 5 + point1Vertex(edgeCount + 8) = vertexCount + 8 + point2Vertex(edgeCount + 8) = vertexCount + 5 ' Define the edges connecting the two squares into cube - point1(numl + 9) = nump + 5 - point2(numl + 9) = nump + 1 + point1Vertex(edgeCount + 9) = vertexCount + 5 + point2Vertex(edgeCount + 9) = vertexCount + 1 - point1(numl + 10) = nump + 6 - point2(numl + 10) = nump + 2 + point1Vertex(edgeCount + 10) = vertexCount + 6 + point2Vertex(edgeCount + 10) = vertexCount + 2 - point1(numl + 11) = nump + 7 - point2(numl + 11) = nump + 3 + point1Vertex(edgeCount + 11) = vertexCount + 7 + point2Vertex(edgeCount + 11) = vertexCount + 3 - point1(numl + 12) = nump + 8 - point2(numl + 12) = nump + 4 + point1Vertex(edgeCount + 12) = vertexCount + 8 + point2Vertex(edgeCount + 12) = vertexCount + 4 ' Update the counters for the next set of vertices and edges - nump = nump + 8 - numl = numl + 12 + vertexCount = vertexCount + 8 + edgeCount = edgeCount + 12 ' Define a pyramid in 3D space - xn(nump + 1) = -150 - yn(nump + 1) = -125 + 201 - zn(nump + 1) = 0 + xVertex(vertexCount + 1) = -150 + yVertex(vertexCount + 1) = -125 + 201 + zVertex(vertexCount + 1) = 0 - xn(nump + 2) = -150 - yn(nump + 2) = -125 + 201 - zn(nump + 2) = 89 + xVertex(vertexCount + 2) = -150 + yVertex(vertexCount + 2) = -125 + 201 + zVertex(vertexCount + 2) = 89 - xn(nump + 3) = -150 - yn(nump + 3) = -125 - zn(nump + 3) = 89 + xVertex(vertexCount + 3) = -150 + yVertex(vertexCount + 3) = -125 + zVertex(vertexCount + 3) = 89 - xn(nump + 4) = -150 - yn(nump + 4) = -125 - zn(nump + 4) = 0 + xVertex(vertexCount + 4) = -150 + yVertex(vertexCount + 4) = -125 + zVertex(vertexCount + 4) = 0 ' Define the edges of the pyramid - point1(numl + 1) = nump + 1 - point2(numl + 1) = nump + 2 + point1Vertex(edgeCount + 1) = vertexCount + 1 + point2Vertex(edgeCount + 1) = vertexCount + 2 - point1(numl + 2) = nump + 2 - point2(numl + 2) = nump + 3 + point1Vertex(edgeCount + 2) = vertexCount + 2 + point2Vertex(edgeCount + 2) = vertexCount + 3 - point1(numl + 3) = nump + 3 - point2(numl + 3) = nump + 4 + point1Vertex(edgeCount + 3) = vertexCount + 3 + point2Vertex(edgeCount + 3) = vertexCount + 4 - point1(numl + 4) = nump + 4 - point2(numl + 4) = nump + 1 + point1Vertex(edgeCount + 4) = vertexCount + 4 + point2Vertex(edgeCount + 4) = vertexCount + 1 ' Update the counters for the next set of vertices and edges - nump = nump + 4 - numl = numl + 4 - + vertexCount = vertexCount + 4 + edgeCount = edgeCount + 4 porand END SUB -SUB kuus (x, y, z, s) +SUB renderHexagon (x, y, z, s) b = 0 f = .3925 @@ -200,48 +215,48 @@ FOR a = 0 + f TO 6 + f STEP 6.28 / 8 y1 = COS(a) * s b = b + 1 - xn(nump + b) = x + x1 - yn(nump + b) = y - zn(nump + b) = z + y1 + xVertex(vertexCount + b) = x + x1 + yVertex(vertexCount + b) = y + zVertex(vertexCount + 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 +point1Vertex(edgeCount + 1) = vertexCount + 1 +point2Vertex(edgeCount + 1) = vertexCount + 2 +col(edgeCount + 1) = 12 -point1(numl + 2) = nump + 2 -point2(numl + 2) = nump + 3 -col(numl + 2) = 12 +point1Vertex(edgeCount + 2) = vertexCount + 2 +point2Vertex(edgeCount + 2) = vertexCount + 3 +col(edgeCount + 2) = 12 -point1(numl + 3) = nump + 3 -point2(numl + 3) = nump + 4 -col(numl + 3) = 12 +point1Vertex(edgeCount + 3) = vertexCount + 3 +point2Vertex(edgeCount + 3) = vertexCount + 4 +col(edgeCount + 3) = 12 -point1(numl + 4) = nump + 4 -point2(numl + 4) = nump + 5 -col(numl + 4) = 12 +point1Vertex(edgeCount + 4) = vertexCount + 4 +point2Vertex(edgeCount + 4) = vertexCount + 5 +col(edgeCount + 4) = 12 -point1(numl + 5) = nump + 5 -point2(numl + 5) = nump + 6 -col(numl + 5) = 12 +point1Vertex(edgeCount + 5) = vertexCount + 5 +point2Vertex(edgeCount + 5) = vertexCount + 6 +col(edgeCount + 5) = 12 -point1(numl + 6) = nump + 6 -point2(numl + 6) = nump + 7 -col(numl + 6) = 12 +point1Vertex(edgeCount + 6) = vertexCount + 6 +point2Vertex(edgeCount + 6) = vertexCount + 7 +col(edgeCount + 6) = 12 -point1(numl + 7) = nump + 7 -point2(numl + 7) = nump + 8 -col(numl + 7) = 12 +point1Vertex(edgeCount + 7) = vertexCount + 7 +point2Vertex(edgeCount + 7) = vertexCount + 8 +col(edgeCount + 7) = 12 -point1(numl + 8) = nump + 8 -point2(numl + 8) = nump + 1 -col(numl + 8) = 12 +point1Vertex(edgeCount + 8) = vertexCount + 8 +point2Vertex(edgeCount + 8) = vertexCount + 1 +col(edgeCount + 8) = 12 ' Update the counters for the next set of vertices and edges -nump = nump + b -numl = numl + 8 +vertexCount = vertexCount + b +edgeCount = edgeCount + 8 END SUB @@ -266,10 +281,10 @@ SUB nait3d 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 + FOR a = 1 TO vertexCount + xo = xVertex(a) - myx + yo = -yVertex(a) - myy + zo = zVertex(a) - myz x1 = (xo * C1 - zo * S1) z1 = (xo * S1 + zo * C1) @@ -278,32 +293,32 @@ SUB nait3d z2 = (yo * S2 + z1 * C2) ' Project the vertex onto the 2D screen - xo(a) = x(a) - yo(a) = y(a) + xOriginal(a) = xCoordinate(a) + yOriginal(a) = yCoordinate(a) IF z2 < 20 THEN - x(a) = -1 + xCoordinate(a) = -1 ELSE - x(a) = 320 + (x1 / z2 * 500) - y(a) = 240 + (y1 / z2 * 500) + xCoordinate(a) = 320 + (x1 / z2 * 500) + yCoordinate(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 + FOR a = 1 TO edgeCount + p1 = point1Vertex(a) + p2 = point2Vertex(a) + IF xOriginal(p1) = -1 OR xOriginal(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 + LINE (xOriginal(p1), yOriginal(p1))-(xOriginal(p2), yOriginal(p2)), 0 END IF - IF x(p1) = -1 OR x(p2) = -1 THEN + IF xCoordinate(p1) = -1 OR xCoordinate(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) + LINE (xCoordinate(p1), yCoordinate(p1))-(xCoordinate(p2), yCoordinate(p2)), col(a) END IF NEXT @@ -367,101 +382,101 @@ SUB porand ' 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 + renderHexagon x, -125, z, 6.53 + renderSquare 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 + renderSquare2 x, y, 200, 10 NEXT x NEXT y END SUB -SUB ruut (x%, y%, z%, s%) +SUB renderSquare (x%, y%, z%, s%) ' Define the vertices of a square in 3D space - xn(nump + 1) = x - yn(nump + 1) = y - zn(nump + 1) = z + s + xVertex(vertexCount + 1) = x% + yVertex(vertexCount + 1) = y% + zVertex(vertexCount + 1) = z% + s% - xn(nump + 2) = x + s - yn(nump + 2) = y - zn(nump + 2) = z + xVertex(vertexCount + 2) = x% + s% + yVertex(vertexCount + 2) = y% + zVertex(vertexCount + 2) = z% - xn(nump + 3) = x - yn(nump + 3) = y - zn(nump + 3) = z - s + xVertex(vertexCount + 3) = x% + yVertex(vertexCount + 3) = y% + zVertex(vertexCount + 3) = z% - s% - xn(nump + 4) = x - s - yn(nump + 4) = y - zn(nump + 4) = z + xVertex(vertexCount + 4) = x% - s% + yVertex(vertexCount + 4) = y% + zVertex(vertexCount + 4) = z% ' Define the edges of the square - point1(numl + 1) = nump + 1 - point2(numl + 1) = nump + 2 - col(numl + 1) = 10 + point1Vertex(edgeCount + 1) = vertexCount + 1 + point2Vertex(edgeCount + 1) = vertexCount + 2 + col(edgeCount + 1) = 10 - point1(numl + 2) = nump + 2 - point2(numl + 2) = nump + 3 - col(numl + 2) = 10 + point1Vertex(edgeCount + 2) = vertexCount + 2 + point2Vertex(edgeCount + 2) = vertexCount + 3 + col(edgeCount + 2) = 10 - point1(numl + 3) = nump + 3 - point2(numl + 3) = nump + 4 - col(numl + 3) = 10 + point1Vertex(edgeCount + 3) = vertexCount + 3 + point2Vertex(edgeCount + 3) = vertexCount + 4 + col(edgeCount + 3) = 10 - point1(numl + 4) = nump + 4 - point2(numl + 4) = nump + 1 - col(numl + 4) = 10 + point1Vertex(edgeCount + 4) = vertexCount + 4 + point2Vertex(edgeCount + 4) = vertexCount + 1 + col(edgeCount + 4) = 10 ' Update the counters for the next square - nump = nump + 4 - numl = numl + 4 + vertexCount = vertexCount + 4 + edgeCount = edgeCount + 4 END SUB -SUB ruut2 (x, y, z, s) +SUB renderSquare2 (x, y, z, s) ' Define the vertices of a square in 3D space - xn(nump + 1) = x - s - yn(nump + 1) = y - s - zn(nump + 1) = z + xVertex(vertexCount + 1) = x - s + yVertex(vertexCount + 1) = y - s + zVertex(vertexCount + 1) = z - xn(nump + 2) = x + s - yn(nump + 2) = y - s - zn(nump + 2) = z + xVertex(vertexCount + 2) = x + s + yVertex(vertexCount + 2) = y - s + zVertex(vertexCount + 2) = z - xn(nump + 3) = x + s - yn(nump + 3) = y + s - zn(nump + 3) = z + xVertex(vertexCount + 3) = x + s + yVertex(vertexCount + 3) = y + s + zVertex(vertexCount + 3) = z - xn(nump + 4) = x - s - yn(nump + 4) = y + s - zn(nump + 4) = z + xVertex(vertexCount + 4) = x - s + yVertex(vertexCount + 4) = y + s + zVertex(vertexCount + 4) = z ' Define the edges of the square - point1(numl + 1) = nump + 1 - point2(numl + 1) = nump + 2 - col(numl + 1) = 14 + point1Vertex(edgeCount + 1) = vertexCount + 1 + point2Vertex(edgeCount + 1) = vertexCount + 2 + col(edgeCount + 1) = 14 - point1(numl + 2) = nump + 2 - point2(numl + 2) = nump + 3 - col(numl + 2) = 14 + point1Vertex(edgeCount + 2) = vertexCount + 2 + point2Vertex(edgeCount + 2) = vertexCount + 3 + col(edgeCount + 2) = 14 - point1(numl + 3) = nump + 3 - point2(numl + 3) = nump + 4 - col(numl + 3) = 14 + point1Vertex(edgeCount + 3) = vertexCount + 3 + point2Vertex(edgeCount + 3) = vertexCount + 4 + col(edgeCount + 3) = 14 - point1(numl + 4) = nump + 4 - point2(numl + 4) = nump + 1 - col(numl + 4) = 14 + point1Vertex(edgeCount + 4) = vertexCount + 4 + point2Vertex(edgeCount + 4) = vertexCount + 1 + col(edgeCount + 4) = 14 ' Update the counters for the next square - nump = nump + 4 - numl = numl + 4 + vertexCount = vertexCount + 4 + edgeCount = edgeCount + 4 END SUB @@ -477,11 +492,10 @@ SUB start NEXT a ' Initialize counters for vertices and edges - nump = 0 - numl = 0 + vertexCount = 0 + edgeCount = 0 ' Generate the initial set of shapes - getcor + getCornerVertices END SUB - -- 2.20.1