From 070c092e470ca95f3d6350afd905169c441d6c2c Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko Date: Tue, 15 Oct 2024 20:44:20 +0300 Subject: [PATCH] Refactoring code for better readability --- Graphics/3D/3dlife.bas | 690 +++++++++++++++++++++-------------------- 1 file changed, 352 insertions(+), 338 deletions(-) diff --git a/Graphics/3D/3dlife.bas b/Graphics/3D/3dlife.bas index d44a8f3..274a345 100755 --- a/Graphics/3D/3dlife.bas +++ b/Graphics/3D/3dlife.bas @@ -1,338 +1,352 @@ -DECLARE SUB PlaceCube (x!, y!, z!) -DECLARE SUB DrawCube (x!, y!, z!) - -DECLARE SUB CreateCollisionData () -DECLARE SUB UpdateCollisionData () -DECLARE SUB RenderScene () -DECLARE SUB InitializeEnvironment () -DECLARE SUB StartGame () -DIM SHARED numPoints, numLines, pointCount, lineCount -DIM SHARED pointsX(1 TO 3000), pointsY(1 TO 3000), pointsZ(1 TO 3000) -DIM SHARED renderedPointsX(1 TO 7000), renderedPointsY(1 TO 7000) -DIM SHARED originalRenderedPointsX(0 TO 9000), originalRenderedPointsY(0 TO 9000) -DIM SHARED oldPointCount, oldLineCount -DIM SHARED lineVertices1(1 TO 3800), lineVertices2(1 TO 3800), lineColors(1 TO 3800) -DIM SHARED originalLineVertices1(1 TO 3800), originalLineVertices2(1 TO 3800) -DIM SHARED myPositionX, myPositionY, myPositionZ -DIM SHARED myPreviousPositionX, myPreviousPositionY, myPreviousPositionZ -DIM SHARED angle1, angle2 -DIM SHARED angle1Str, angle2Str -DIM SHARED frameCount - -DIM SHARED gameOfLifeGrid(1 TO 50, 1 TO 50) -DIM SHARED nextGameOfLifeGrid(1 TO 50, 1 TO 50) - -StartGame -' InitializeEnvironment - -angle1 = 1.5 -10 -frameCount = frameCount + 1 - -CreateCollisionData -RenderScene - -myPositionX = SIN(frameCount / 20) * 12 -myPositionY = SIN(frameCount / 50) * 10 + 15 -myPositionZ = COS(frameCount / 20) * 12 - -angle1 = angle1 - .05 -angle2 = 2.2 + SIN(frameCount / 50) / 2 - -inputKey$ = INKEY$ -IF inputKey$ <> "" THEN SYSTEM -GOTO 10 - -SUB CreateCollisionData -pointCount = numPoints -lineCount = numLines - -FOR y = 1 TO 50 - FOR x = 1 TO 50 - IF gameOfLifeGrid(x, y) = 1 THEN - v = ABS(x - 26) + ABS(y - 26) + frameCount - PlaceCube x - 25, SIN(v / 5) * 5, y - 25 - END IF - NEXT x -NEXT y - -IF frameCount \ 10 = frameCount / 10 THEN - -FOR y = 2 TO 49 -FOR x = 2 TO 49 -c = gameOfLifeGrid(x - 1, y - 1) -c = c + gameOfLifeGrid(x, y - 1) -c = c + gameOfLifeGrid(x + 1, y - 1) -c = c + gameOfLifeGrid(x - 1, y) -c = c + gameOfLifeGrid(x + 1, y) -c = c + gameOfLifeGrid(x - 1, y + 1) -c = c + gameOfLifeGrid(x, y + 1) -c = c + gameOfLifeGrid(x + 1, y + 1) - -IF gameOfLifeGrid(x, y) = 1 THEN - IF (c > 3) OR (c < 2) THEN nextGameOfLifeGrid(x, y) = 0 ELSE nextGameOfLifeGrid(x, y) = 1 -ELSE - IF c = 3 THEN nextGameOfLifeGrid(x, y) = 1 ELSE nextGameOfLifeGrid(x, y) = 0 -END IF -NEXT x -NEXT y - -FOR y = 1 TO 50 -FOR x = 1 TO 50 - gameOfLifeGrid(x, y) = nextGameOfLifeGrid(x, y) -NEXT x -NEXT y -END IF - -END SUB - -SUB InitializeEnvironment -FOR z = -5 TO 5 -FOR x = -5 TO 5 -pointCount = pointCount + 1 -pointsX(pointCount) = x -pointsY(pointCount) = 0 -pointsZ(pointCount) = z -IF x > -5 THEN -lineCount = lineCount + 1 -lineVertices1(lineCount) = pointCount -lineVertices2(lineCount) = pointCount - 1 -lineColors(lineCount) = 1 -END IF -IF z > -5 THEN -lineCount = lineCount + 1 -lineVertices1(lineCount) = pointCount -lineVertices2(lineCount) = pointCount - 10 -lineColors(lineCount) = 1 -END IF -NEXT x -NEXT z - -numPoints = pointCount -numLines = lineCount - -END SUB - -SUB InitializeEnvironment1 -pointCount = 1 -pointsX(pointCount) = -2 -pointsY(pointCount) = 0 -pointsZ(pointCount) = 0 -pointCount = pointCount + 1 -pointsX(pointCount) = 2 -pointsY(pointCount) = 0 -pointsZ(pointCount) = 0 - -lineCount = 1 -lineVertices1(lineCount) = 1 -lineVertices2(lineCount) = 2 -lineColors(lineCount) = 14 - -END SUB - -SUB PlaceCube (x, y, z) - -v = 3 - -lineCount = lineCount + 1 -lineVertices1(lineCount) = pointCount + 1 -lineVertices2(lineCount) = pointCount + 2 -lineColors(lineCount) = v - -lineCount = lineCount + 1 -lineVertices1(lineCount) = pointCount + 2 -lineVertices2(lineCount) = pointCount + 3 -lineColors(lineCount) = v - -lineCount = lineCount + 1 -lineVertices1(lineCount) = pointCount + 3 -lineVertices2(lineCount) = pointCount + 4 -lineColors(lineCount) = v - -lineCount = lineCount + 1 -lineVertices1(lineCount) = pointCount + 4 -lineVertices2(lineCount) = pointCount + 1 -lineColors(lineCount) = v - -lineCount = lineCount + 1 -lineVertices1(lineCount) = pointCount + 5 -lineVertices2(lineCount) = pointCount + 6 -lineColors(lineCount) = v - -lineCount = lineCount + 1 -lineVertices1(lineCount) = pointCount + 6 -lineVertices2(lineCount) = pointCount + 7 -lineColors(lineCount) = v - -lineCount = lineCount + 1 -lineVertices1(lineCount) = pointCount + 7 -lineVertices2(lineCount) = pointCount + 8 -lineColors(lineCount) = v - -lineCount = lineCount + 1 -lineVertices1(lineCount) = pointCount + 8 -lineVertices2(lineCount) = pointCount + 5 -lineColors(lineCount) = v - -lineCount = lineCount + 1 -lineVertices1(lineCount) = pointCount + 1 -lineVertices2(lineCount) = pointCount + 5 -lineColors(lineCount) = v - -lineCount = lineCount + 1 -lineVertices1(lineCount) = pointCount + 2 -lineVertices2(lineCount) = pointCount + 6 -lineColors(lineCount) = v - -lineCount = lineCount + 1 -lineVertices1(lineCount) = pointCount + 3 -lineVertices2(lineCount) = pointCount + 7 -lineColors(lineCount) = v - -lineCount = lineCount + 1 -lineVertices1(lineCount) = pointCount + 4 -lineVertices2(lineCount) = pointCount + 8 -lineColors(lineCount) = v - -pointCount = pointCount + 1 -pointsX(pointCount) = x - .5 -pointsY(pointCount) = y -pointsZ(pointCount) = z - .5 - -pointCount = pointCount + 1 -pointsX(pointCount) = x + .5 -pointsY(pointCount) = y -pointsZ(pointCount) = z - .5 - -pointCount = pointCount + 1 -pointsX(pointCount) = x + .5 -pointsY(pointCount) = y -pointsZ(pointCount) = z + .5 - -pointCount = pointCount + 1 -pointsX(pointCount) = x - .5 -pointsY(pointCount) = y -pointsZ(pointCount) = z + .5 - -pointCount = pointCount + 1 -pointsX(pointCount) = x - .5 -pointsY(pointCount) = y + 1 -pointsZ(pointCount) = z - .5 - -pointCount = pointCount + 1 -pointsX(pointCount) = x + .5 -pointsY(pointCount) = y + 1 -pointsZ(pointCount) = z - .5 - -pointCount = pointCount + 1 -pointsX(pointCount) = x + .5 -pointsY(pointCount) = y + 1 -pointsZ(pointCount) = z + .5 - -pointCount = pointCount + 1 -pointsX(pointCount) = x - .5 -pointsY(pointCount) = y + 1 -pointsZ(pointCount) = z + .5 - -END SUB - -SUB RenderScene - -sineAngle1 = SIN(angle1) -cosineAngle1 = COS(angle1) -sineAngle2 = SIN(angle2) -cosineAngle2 = COS(angle2) - -FOR a = 1 TO pointCount -x = pointsX(a) + myPositionX -y = pointsY(a) - myPositionY -z = pointsZ(a) + myPositionZ - -x1 = x * sineAngle1 - z * cosineAngle1 -z1 = x * cosineAngle1 + z * sineAngle1 -y1 = y * sineAngle2 - z1 * cosineAngle2 -z2 = y * cosineAngle2 + z1 * sineAngle2 - -IF z2 < .5 THEN -renderedPointsX(a) = -1 -ELSE -renderedPointsX(a) = 320 + (x1 / z2 * 400) -renderedPointsY(a) = 240 - (y1 / z2 * 400) -END IF -NEXT a - -FOR a = 1 TO lineCount -l1 = originalLineVertices1(a) -l2 = originalLineVertices2(a) - -IF originalRenderedPointsX(l1) = -1 OR originalRenderedPointsX(l2) = -1 THEN ELSE LINE (originalRenderedPointsX(l1), originalRenderedPointsY(l1))-(originalRenderedPointsX(l2), originalRenderedPointsY(l2)), 0 -l1 = lineVertices1(a) -l2 = lineVertices2(a) -IF renderedPointsX(l1) = -1 OR renderedPointsX(l2) = -1 THEN ELSE LINE (renderedPointsX(l1), renderedPointsY(l1))-(renderedPointsX(l2), renderedPointsY(l2)), lineColors(a) -NEXT a - -IF lineCount < oldLineCount THEN -FOR a = lineCount + 1 TO oldLineCount -l1 = originalLineVertices1(a) -l2 = originalLineVertices2(a) -IF originalRenderedPointsX(l1) = -1 OR originalRenderedPointsX(l2) = -1 THEN ELSE LINE (originalRenderedPointsX(l1), originalRenderedPointsY(l1))-(originalRenderedPointsX(l2), originalRenderedPointsY(l2)), 0 -NEXT a -END IF - -FOR a = 1 TO pointCount -originalRenderedPointsX(a) = renderedPointsX(a) -originalRenderedPointsY(a) = renderedPointsY(a) -NEXT a -oldPointCount = pointCount - -FOR a = 1 TO lineCount -originalLineVertices1(a) = lineVertices1(a) -originalLineVertices2(a) = lineVertices2(a) -NEXT a -oldLineCount = lineCount - -END SUB - -SUB StartGame -SCREEN 12 -numPoints = 0 -numLines = 0 -pointCount = numPoints -lineCount = numLines -gridSize = 50 - -myPositionX = 4 -myPositionY = 15 -myPositionZ = 17 -angle1 = ATN(1) / 2 - .29 -angle2 = angle1 + 1 - -FOR a = 1 TO 1000 -lineColors(a) = 4 -NEXT a - -FOR a = 1 TO 1000 -originalLineVertices1(a) = 1 -originalLineVertices2(a) = 1 -NEXT a - -OPEN "3dlife.dat" FOR INPUT AS #1 -y = 20 -20 -IF EOF(1) <> 0 THEN GOTO 30 -x = 20 - -LINE INPUT #1, inputLine$ -FOR b = 1 TO LEN(inputLine$) - c$ = RIGHT$(LEFT$(inputLine$, b), 1) - IF c$ = "#" THEN gameOfLifeGrid(x, y) = 1 - x = x + 1 -NEXT b - -y = y + 1 -GOTO 20 -30 -CLOSE #1 - -END SUB - +' Program renders rotating 3D animation from cubes. +' Cubes appear and disappear according to Conway's Game of Life rules. + +DECLARE SUB PlaceCube (x!, y!, z!) +DECLARE SUB DrawCube (x!, y!, z!) + +DECLARE SUB CreateCollisionData () +DECLARE SUB UpdateCollisionData () +DECLARE SUB RenderScene () +DECLARE SUB InitializeEnvironment () +DECLARE SUB StartGame () + +DIM SHARED numPoints, numLines, pointCount, lineCount +DIM SHARED pointsX(1 TO 3000), pointsY(1 TO 3000), pointsZ(1 TO 3000) +DIM SHARED renderedPointsX(1 TO 7000), renderedPointsY(1 TO 7000) +DIM SHARED originalRenderedPointsX(0 TO 9000), originalRenderedPointsY(0 TO 9000) +DIM SHARED oldPointCount, oldLineCount +DIM SHARED lineVertices1(1 TO 3800), lineVertices2(1 TO 3800), lineColors(1 TO 3800) +DIM SHARED originalLineVertices1(1 TO 3800), originalLineVertices2(1 TO 3800) +DIM SHARED myPositionX, myPositionY, myPositionZ +DIM SHARED myPreviousPositionX, myPreviousPositionY, myPreviousPositionZ +DIM SHARED angle1, angle2 +DIM SHARED angle1Str, angle2Str +DIM SHARED frameCount + +DIM SHARED gameOfLifeGrid(1 TO 50, 1 TO 50) +DIM SHARED nextGameOfLifeGrid(1 TO 50, 1 TO 50) + +StartGame + +angle1 = 1.5 +10 +frameCount = frameCount + 1 + +CreateCollisionData +RenderScene + +myPositionX = SIN(frameCount / 20) * 12 +myPositionY = SIN(frameCount / 50) * 10 + 15 +myPositionZ = COS(frameCount / 20) * 12 + +angle1 = angle1 - .05 +angle2 = 2.2 + SIN(frameCount / 50) / 2 + +inputKey$ = INKEY$ +IF inputKey$ <> "" THEN SYSTEM +GOTO 10 + +SUB CreateCollisionData +pointCount = numPoints +lineCount = numLines + +FOR y = 1 TO 50 + FOR x = 1 TO 50 + IF gameOfLifeGrid(x, y) = 1 THEN + v = ABS(x - 26) + ABS(y - 26) + frameCount + PlaceCube x - 25, SIN(v / 5) * 5, y - 25 + END IF + NEXT x +NEXT y + +IF frameCount \ 10 = frameCount / 10 THEN + +FOR y = 2 TO 49 +FOR x = 2 TO 49 +c = gameOfLifeGrid(x - 1, y - 1) +c = c + gameOfLifeGrid(x, y - 1) +c = c + gameOfLifeGrid(x + 1, y - 1) +c = c + gameOfLifeGrid(x - 1, y) +c = c + gameOfLifeGrid(x + 1, y) +c = c + gameOfLifeGrid(x - 1, y + 1) +c = c + gameOfLifeGrid(x, y + 1) +c = c + gameOfLifeGrid(x + 1, y + 1) + +IF gameOfLifeGrid(x, y) = 1 THEN + IF (c > 3) OR (c < 2) THEN nextGameOfLifeGrid(x, y) = 0 ELSE nextGameOfLifeGrid(x, y) = 1 +ELSE + IF c = 3 THEN nextGameOfLifeGrid(x, y) = 1 ELSE nextGameOfLifeGrid(x, y) = 0 +END IF +NEXT x +NEXT y + +FOR y = 1 TO 50 +FOR x = 1 TO 50 + gameOfLifeGrid(x, y) = nextGameOfLifeGrid(x, y) +NEXT x +NEXT y +END IF + +END SUB + +SUB InitializeEnvironment +FOR z = -5 TO 5 +FOR x = -5 TO 5 +pointCount = pointCount + 1 +pointsX(pointCount) = x +pointsY(pointCount) = 0 +pointsZ(pointCount) = z +IF x > -5 THEN +lineCount = lineCount + 1 +lineVertices1(lineCount) = pointCount +lineVertices2(lineCount) = pointCount - 1 +lineColors(lineCount) = 1 +END IF +IF z > -5 THEN +lineCount = lineCount + 1 +lineVertices1(lineCount) = pointCount +lineVertices2(lineCount) = pointCount - 10 +lineColors(lineCount) = 1 +END IF +NEXT x +NEXT z + +numPoints = pointCount +numLines = lineCount + +END SUB + +SUB InitializeEnvironment1 +pointCount = 1 +pointsX(pointCount) = -2 +pointsY(pointCount) = 0 +pointsZ(pointCount) = 0 +pointCount = pointCount + 1 +pointsX(pointCount) = 2 +pointsY(pointCount) = 0 +pointsZ(pointCount) = 0 + +lineCount = 1 +lineVertices1(lineCount) = 1 +lineVertices2(lineCount) = 2 +lineColors(lineCount) = 14 + +END SUB + +SUB PlaceCube (x, y, z) + +v = 3 + +lineCount = lineCount + 1 +lineVertices1(lineCount) = pointCount + 1 +lineVertices2(lineCount) = pointCount + 2 +lineColors(lineCount) = v + +lineCount = lineCount + 1 +lineVertices1(lineCount) = pointCount + 2 +lineVertices2(lineCount) = pointCount + 3 +lineColors(lineCount) = v + +lineCount = lineCount + 1 +lineVertices1(lineCount) = pointCount + 3 +lineVertices2(lineCount) = pointCount + 4 +lineColors(lineCount) = v + +lineCount = lineCount + 1 +lineVertices1(lineCount) = pointCount + 4 +lineVertices2(lineCount) = pointCount + 1 +lineColors(lineCount) = v + +lineCount = lineCount + 1 +lineVertices1(lineCount) = pointCount + 5 +lineVertices2(lineCount) = pointCount + 6 +lineColors(lineCount) = v + +lineCount = lineCount + 1 +lineVertices1(lineCount) = pointCount + 6 +lineVertices2(lineCount) = pointCount + 7 +lineColors(lineCount) = v + +lineCount = lineCount + 1 +lineVertices1(lineCount) = pointCount + 7 +lineVertices2(lineCount) = pointCount + 8 +lineColors(lineCount) = v + +lineCount = lineCount + 1 +lineVertices1(lineCount) = pointCount + 8 +lineVertices2(lineCount) = pointCount + 5 +lineColors(lineCount) = v + +lineCount = lineCount + 1 +lineVertices1(lineCount) = pointCount + 1 +lineVertices2(lineCount) = pointCount + 5 +lineColors(lineCount) = v + +lineCount = lineCount + 1 +lineVertices1(lineCount) = pointCount + 2 +lineVertices2(lineCount) = pointCount + 6 +lineColors(lineCount) = v + +lineCount = lineCount + 1 +lineVertices1(lineCount) = pointCount + 3 +lineVertices2(lineCount) = pointCount + 7 +lineColors(lineCount) = v + +lineCount = lineCount + 1 +lineVertices1(lineCount) = pointCount + 4 +lineVertices2(lineCount) = pointCount + 8 +lineColors(lineCount) = v + +pointCount = pointCount + 1 +pointsX(pointCount) = x - .5 +pointsY(pointCount) = y +pointsZ(pointCount) = z - .5 + +pointCount = pointCount + 1 +pointsX(pointCount) = x + .5 +pointsY(pointCount) = y +pointsZ(pointCount) = z - .5 + +pointCount = pointCount + 1 +pointsX(pointCount) = x + .5 +pointsY(pointCount) = y +pointsZ(pointCount) = z + .5 + +pointCount = pointCount + 1 +pointsX(pointCount) = x - .5 +pointsY(pointCount) = y +pointsZ(pointCount) = z + .5 + +pointCount = pointCount + 1 +pointsX(pointCount) = x - .5 +pointsY(pointCount) = y + 1 +pointsZ(pointCount) = z - .5 + +pointCount = pointCount + 1 +pointsX(pointCount) = x + .5 +pointsY(pointCount) = y + 1 +pointsZ(pointCount) = z - .5 + +pointCount = pointCount + 1 +pointsX(pointCount) = x + .5 +pointsY(pointCount) = y + 1 +pointsZ(pointCount) = z + .5 + +pointCount = pointCount + 1 +pointsX(pointCount) = x - .5 +pointsY(pointCount) = y + 1 +pointsZ(pointCount) = z + .5 + +END SUB + +SUB RenderScene + +sineAngle1 = SIN(angle1) +cosineAngle1 = COS(angle1) +sineAngle2 = SIN(angle2) +cosineAngle2 = COS(angle2) + +FOR a = 1 TO pointCount +x = pointsX(a) + myPositionX +y = pointsY(a) - myPositionY +z = pointsZ(a) + myPositionZ + +x1 = x * sineAngle1 - z * cosineAngle1 +z1 = x * cosineAngle1 + z * sineAngle1 +y1 = y * sineAngle2 - z1 * cosineAngle2 +z2 = y * cosineAngle2 + z1 * sineAngle2 + +IF z2 < .5 THEN +renderedPointsX(a) = -1 +ELSE +renderedPointsX(a) = 320 + (x1 / z2 * 400) +renderedPointsY(a) = 240 - (y1 / z2 * 400) +END IF +NEXT a + +FOR a = 1 TO lineCount +l1 = originalLineVertices1(a) +l2 = originalLineVertices2(a) + +IF originalRenderedPointsX(l1) = -1 OR originalRenderedPointsX(l2) = -1 THEN +ELSE +LINE (originalRenderedPointsX(l1), originalRenderedPointsY(l1))-(originalRenderedPointsX(l2), originalRenderedPointsY(l2)), 0 +END IF + +l1 = lineVertices1(a) +l2 = lineVertices2(a) +IF renderedPointsX(l1) = -1 OR renderedPointsX(l2) = -1 THEN +ELSE +LINE (renderedPointsX(l1), renderedPointsY(l1))-(renderedPointsX(l2), renderedPointsY(l2)), lineColors(a) +END IF +NEXT a + +IF lineCount < oldLineCount THEN +FOR a = lineCount + 1 TO oldLineCount +l1 = originalLineVertices1(a) +l2 = originalLineVertices2(a) +IF originalRenderedPointsX(l1) = -1 OR originalRenderedPointsX(l2) = -1 THEN +ELSE +LINE (originalRenderedPointsX(l1), originalRenderedPointsY(l1))-(originalRenderedPointsX(l2), originalRenderedPointsY(l2)), 0 +END IF +NEXT a +END IF + +FOR a = 1 TO pointCount +originalRenderedPointsX(a) = renderedPointsX(a) +originalRenderedPointsY(a) = renderedPointsY(a) +NEXT a + +oldPointCount = pointCount + +FOR a = 1 TO lineCount +originalLineVertices1(a) = lineVertices1(a) +originalLineVertices2(a) = lineVertices2(a) +NEXT a + +oldLineCount = lineCount + +END SUB + +SUB StartGame +SCREEN 12 +numPoints = 0 +numLines = 0 +pointCount = numPoints +lineCount = numLines +gridSize = 50 + +myPositionX = 4 +myPositionY = 15 +myPositionZ = 17 +angle1 = ATN(1) / 2 - .29 +angle2 = angle1 + 1 + +FOR a = 1 TO 1000 +lineColors(a) = 4 +NEXT a + +FOR a = 1 TO 1000 +originalLineVertices1(a) = 1 +originalLineVertices2(a) = 1 +NEXT a + +OPEN "3dlife.dat" FOR INPUT AS #1 +y = 20 +20 +IF EOF(1) <> 0 THEN GOTO 30 +x = 20 + +LINE INPUT #1, inputLine$ +FOR b = 1 TO LEN(inputLine$) + c$ = RIGHT$(LEFT$(inputLine$, b), 1) + IF c$ = "#" THEN gameOfLifeGrid(x, y) = 1 + x = x + 1 +NEXT b + +y = y + 1 +GOTO 20 +30 +CLOSE #1 + +END SUB -- 2.20.1