Better code readability.
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Thu, 26 Jun 2025 14:03:51 +0000 (17:03 +0300)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Thu, 26 Jun 2025 14:03:51 +0000 (17:03 +0300)
2D GFX/Presentations/AI/AI demo.bas

index 26664c3..70993ef 100644 (file)
@@ -390,44 +390,43 @@ END SUB
 \r
 SUB Scene1\r
 '\r
-' Loads & processes 3D data from "data.dat" to demonstrate simple line rendering.\r
-' The object is rotated in real-time, and lines are projected in 2D.\r
-' Timers and angles cause a rotating, animated effect.\r
+' Loads a 3D model from data.dat and demonstrates real-time rotation.\r
+' The model is projected to 2D and animated with a rotating effect.\r
 '\r
     SetPalette 0, 63, 20, 255\r
 \r
-    DIM pointX(0 TO 1000)\r
-    DIM pointY(0 TO 1000)\r
-    DIM pointZ(0 TO 1000)\r
+    DIM vertexX(0 TO 1000)\r
+    DIM vertexY(0 TO 1000)\r
+    DIM vertexZ(0 TO 1000)\r
 \r
     DIM projectedX(0 TO 1000)\r
     DIM projectedY(0 TO 1000)\r
 \r
-    DIM linePointOne!(0 TO 1500)\r
-    DIM linePointTwo!(0 TO 1500)\r
-\r
-    DIM lineBufferXOne(1 TO 1500)\r
-    DIM lineBufferYOne(1 TO 1500)\r
-    DIM lineBufferXTwo(1 TO 1500)\r
-    DIM lineBufferYTwo(1 TO 1500)\r
-\r
-    DIM numPoints, numLines\r
-    DIM angleOne, angleTwo, angleThree\r
-    DIM angleOneSpeed, angleTwoSpeed, angleThreeSpeed\r
-    DIM incPoints, incPolys\r
-    DIM timeCounter\r
-    DIM totalLines\r
-    DIM extraHelperIndex\r
-\r
-    timeCounter = 0\r
-    extraHelperIndex = 1\r
-    angleOne = 0\r
-    angleTwo = 0\r
-    numPoints = -1\r
-    numLines = 0\r
-    incPoints = 0\r
-    incPolys = 0\r
+    DIM lineStart(0 TO 1500)\r
+    DIM lineEnd(0 TO 1500)\r
+\r
+    DIM oldLineStartX(1 TO 1500)\r
+    DIM oldLineStartY(1 TO 1500)\r
+    DIM oldLineEndX(1 TO 1500)\r
+    DIM oldLineEndY(1 TO 1500)\r
+\r
+    DIM totalVertices, totalLines\r
+    DIM rotationAngle1, rotationAngle2, rotationAngle3\r
+    DIM rotationSpeed1, rotationSpeed2, rotationSpeed3\r
+    DIM incrementVertices, incrementPolygons\r
+    DIM animationTime\r
+    DIM currentLines\r
+    DIM progressIndex\r
+\r
+    animationTime = 0\r
+    progressIndex = 1\r
+    rotationAngle1 = 0\r
+    rotationAngle2 = 0\r
+    totalVertices = -1\r
     totalLines = 0\r
+    incrementVertices = 0\r
+    incrementPolygons = 0\r
+    currentLines = 0\r
 \r
     SetPalette 40, 40, 40, 254\r
     COLOR 254\r
@@ -436,117 +435,116 @@ SUB Scene1
 \r
     OPEN "data.dat" FOR INPUT AS #1\r
     INPUT #1, readTemp\r
-    INPUT #1, incPoints\r
-    INPUT #1, incPolys\r
+    INPUT #1, incrementVertices\r
+    INPUT #1, incrementPolygons\r
 \r
-    FOR loopIndex = 1 TO incPoints\r
-        INPUT #1, pxVal, pyVal, pzVal\r
-        numPoints = numPoints + 1\r
-        pointX(numPoints) = pxVal - 100\r
-        pointY(numPoints) = pyVal\r
-        pointZ(numPoints) = pzVal\r
-    NEXT loopIndex\r
+    FOR vertexIndex = 1 TO incrementVertices\r
+        INPUT #1, xVal, yVal, zVal\r
+        totalVertices = totalVertices + 1\r
+        vertexX(totalVertices) = xVal - 100\r
+        vertexY(totalVertices) = yVal\r
+        vertexZ(totalVertices) = zVal\r
+    NEXT vertexIndex\r
 \r
     INPUT #1, dummyVar, dummyVar, lineVal1, lineVal2, lineVal3\r
 \r
-    FOR loopIndex = 1 TO incPolys - 1\r
+    FOR polygonIndex = 1 TO incrementPolygons - 1\r
         INPUT #1, dummyVar, dummyVar, lineVal1!, lineVal2!, lineVal3!\r
-        newLineOne! = lineVal1!\r
-        newLineTwo! = lineVal2!\r
+        newLineStart! = lineVal1!\r
+        newLineEnd! = lineVal2!\r
         GOSUB addLine\r
-        newLineOne! = lineVal2!\r
-        newLineTwo! = lineVal3!\r
+        newLineStart! = lineVal2!\r
+        newLineEnd! = lineVal3!\r
         GOSUB addLine\r
-        newLineOne! = lineVal1!\r
-        newLineTwo! = lineVal3!\r
+        newLineStart! = lineVal1!\r
+        newLineEnd! = lineVal3!\r
         GOSUB addLine\r
         LOCATE 4, 10\r
-        PRINT STR$(INT(loopIndex / (incPolys - 1) * 100)) + "% ready"\r
-    NEXT loopIndex\r
+        PRINT STR$(INT(polygonIndex / (incrementPolygons - 1) * 100)) + "% ready"\r
+    NEXT polygonIndex\r
     CLOSE 1\r
     CLS\r
 \r
 rotateLoop:\r
-    timeCounter = timeCounter + 1\r
+    animationTime = animationTime + 1\r
 \r
     quitKey$ = INKEY$\r
     IF quitKey$ = "q" THEN END\r
 \r
-    varA = COS(timeCounter / 25)\r
-    angleOne = COS(timeCounter / 29) * varA\r
-    angleTwo = (globalPiI / 2) + SIN(timeCounter / 42) * varA\r
+    varA = COS(animationTime / 25)\r
+    rotationAngle1 = COS(animationTime / 29) * varA\r
+    rotationAngle2 = (globalPiI / 2) + SIN(animationTime / 42) * varA\r
 \r
-    sin1 = SIN(angleOne)\r
-    cos1 = COS(angleOne)\r
-    sin2 = SIN(angleTwo)\r
-    cos2 = COS(angleTwo)\r
+    sin1 = SIN(rotationAngle1)\r
+    cos1 = COS(rotationAngle1)\r
+    sin2 = SIN(rotationAngle2)\r
+    cos2 = COS(rotationAngle2)\r
 \r
-    IF extraHelperIndex >= 1 THEN\r
-        totalLines = totalLines + extraHelperIndex\r
-        extraHelperIndex = extraHelperIndex + .03\r
-        IF totalLines > numLines THEN totalLines = numLines: extraHelperIndex = 0\r
+    IF progressIndex >= 1 THEN\r
+        currentLines = currentLines + progressIndex\r
+        progressIndex = progressIndex + .03\r
+        IF currentLines > totalLines THEN currentLines = totalLines: progressIndex = 0\r
     END IF\r
 \r
-    ' Project each 3D point to 2D\r
-    FOR loopIndex = 0 TO numPoints\r
-        localX = pointX(loopIndex)\r
-        localY = pointY(loopIndex)\r
-        localZ = pointZ(loopIndex)\r
+    ' Project each 3D vertex to 2D\r
+    FOR vertexIndex = 0 TO totalVertices\r
+        x = vertexX(vertexIndex)\r
+        y = vertexY(vertexIndex)\r
+        z = vertexZ(vertexIndex)\r
 \r
-        zTemp = localZ * sin1 + localY * cos1\r
-        yTemp = localY * sin1 - localZ * cos1\r
+        zTemp = z * sin1 + y * cos1\r
+        yTemp = y * sin1 - z * cos1\r
 \r
-        zFinal = zTemp * sin2 + localX * cos2\r
-        xFinal = localX * sin2 - zTemp * cos2\r
+        zFinal = zTemp * sin2 + x * cos2\r
+        xFinal = x * sin2 - zTemp * cos2\r
 \r
         zFinal = zFinal + 100\r
         xFinal = xFinal / zFinal * 74 * 2\r
         yFinal = yTemp / zFinal * 65 * 2\r
 \r
-        projectedX(loopIndex) = xFinal + 160\r
-        projectedY(loopIndex) = yFinal + 80\r
-    NEXT loopIndex\r
-\r
-    ' Draw lines\r
-    FOR loopIndex = 1 TO totalLines\r
-        lineStart = linePointOne!(loopIndex)\r
-        lineEnd = linePointTwo!(loopIndex)\r
-        x1Temp = projectedX(lineStart)\r
-        y1Temp = projectedY(lineStart)\r
-        x2Temp = projectedX(lineEnd)\r
-        y2Temp = projectedY(lineEnd)\r
-        LINE (lineBufferXOne(loopIndex), lineBufferYOne(loopIndex))-(lineBufferXTwo(loopIndex), lineBufferYTwo(loopIndex)), 0\r
-        LINE (x1Temp, y1Temp)-(x2Temp, y2Temp), 255\r
-        lineBufferXOne(loopIndex) = x1Temp\r
-        lineBufferYOne(loopIndex) = y1Temp\r
-        lineBufferXTwo(loopIndex) = x2Temp\r
-        lineBufferYTwo(loopIndex) = y2Temp\r
-    NEXT loopIndex\r
+        projectedX(vertexIndex) = xFinal + 160\r
+        projectedY(vertexIndex) = yFinal + 80\r
+    NEXT vertexIndex\r
+\r
+    ' Draw lines between vertices\r
+    FOR lineIndex = 1 TO currentLines\r
+        startVertex = lineStart(lineIndex)\r
+        endVertex = lineEnd(lineIndex)\r
+        x1 = projectedX(startVertex)\r
+        y1 = projectedY(startVertex)\r
+        x2 = projectedX(endVertex)\r
+        y2 = projectedY(endVertex)\r
+        LINE (oldLineStartX(lineIndex), oldLineStartY(lineIndex))-(oldLineEndX(lineIndex), oldLineEndY(lineIndex)), 0\r
+        LINE (x1, y1)-(x2, y2), 255\r
+        oldLineStartX(lineIndex) = x1\r
+        oldLineStartY(lineIndex) = y1\r
+        oldLineEndX(lineIndex) = x2\r
+        oldLineEndY(lineIndex) = y2\r
+    NEXT lineIndex\r
 \r
     SOUND 0, .5\r
-    IF timeCounter < 280 THEN GOTO rotateLoop\r
+    IF animationTime < 280 THEN GOTO rotateLoop\r
     GOTO endScene\r
 \r
 addLine:\r
-    FOR checkIndex = 1 TO numLines\r
-        IF linePointOne!(checkIndex) = newLineOne! THEN\r
-            IF linePointTwo!(checkIndex) = newLineTwo! THEN RETURN\r
+    FOR checkIndex = 1 TO totalLines\r
+        IF lineStart(checkIndex) = newLineStart! THEN\r
+            IF lineEnd(checkIndex) = newLineEnd! THEN RETURN\r
         END IF\r
-        IF linePointOne!(checkIndex) = newLineTwo! THEN\r
-            IF linePointTwo!(checkIndex) = newLineOne! THEN RETURN\r
+        IF lineStart(checkIndex) = newLineEnd! THEN\r
+            IF lineEnd(checkIndex) = newLineStart! THEN RETURN\r
         END IF\r
     NEXT checkIndex\r
-    numLines = numLines + 1\r
-    linePointOne!(numLines) = newLineOne!\r
-    linePointTwo!(numLines) = newLineTwo!\r
+    totalLines = totalLines + 1\r
+    lineStart(totalLines) = newLineStart!\r
+    lineEnd(totalLines) = newLineEnd!\r
     RETURN\r
 \r
 endScene:\r
-    globalAngleOne = angleOne\r
-    globalAngleTwo = angleTwo\r
+    globalAngleOne = rotationAngle1\r
+    globalAngleTwo = rotationAngle2\r
 END SUB\r
 \r
-\r
 SUB Scene2\r
 '\r
 ' Loads 3D data from "data.dat" and projects triangular faces in 2D.\r