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