From: Svjatoslav Agejenko Date: Sat, 26 Oct 2024 06:59:43 +0000 (+0300) Subject: Using AI to improve code readability X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=commitdiff_plain;h=0cb2800b732c243cc76e681d6186d4d5f746d398;p=qbasicapps.git Using AI to improve code readability --- diff --git a/Graphics/Animations/2drot.bas b/Graphics/Animations/2drot.bas index 96804e1..b2146f4 100755 --- a/Graphics/Animations/2drot.bas +++ b/Graphics/Animations/2drot.bas @@ -1,50 +1,63 @@ -' Program to render rotating pixels +' This program showcases the rotation of points on an X-Y coordinate +' system using trigonometric functions, specifically sine and cosine. By +' simulating the effect of rotating a collection of grid points around +' the origin, it demonstrates the mathematical principles behind 2D +' rotation. +' ' By Svjatoslav Agejenko. ' Email: svjatoslav@svjatoslav.eu ' Homepage: http://www.svjatoslav.eu -' + ' Changelog: ' 2003.12, Initial version -' 2024.08, Improved program readability using AI +' 2024, Improved program readability using AI - -DIM SHARED pointX(1000) ' Point x & y location -DIM SHARED pointY(1000) -DIM SHARED oldPointX(1000) ' Old point x & y location -DIM SHARED oldPointY(1000) +DIM SHARED pointXCoordinates(1000) ' Array to store x coordinates of points +DIM SHARED pointYCoordinates(1000) ' Array to store y coordinates of points +DIM SHARED oldPointXCoordinates(1000) ' Array to store previous x coordinates of points +DIM SHARED oldPointYCoordinates(1000) ' Array to store previous y coordinates of points SCREEN 13 -numPoints = 0 ' Set points to their place +numPoints = 0 ' Initialize the number of points FOR pointXVal = -10 TO 10 FOR pointYVal = -10 TO 10 numPoints = numPoints + 1 - pointX(numPoints) = pointXVal - pointY(numPoints) = pointYVal + pointXCoordinates(numPoints) = pointXVal + pointYCoordinates(numPoints) = pointYVal NEXT pointYVal NEXT pointXVal -' Rotate points now -rotationAngle = 0 ' Start angle +' Main rotation loop +rotationAngle = 0 ' Initialize the rotation angle to 0 + 1 - rotationAngle = rotationAngle + 0.01 - sineOfAngle = SIN(rotationAngle) - cosineOfAngle = COS(rotationAngle) + rotationAngle = rotationAngle + .01 ' Increment the rotation angle by 0.01 radians + + ' Calculate the sine and cosine of the current rotation angle + sineOfRotationAngle = SIN(rotationAngle) + cosineOfRotationAngle = COS(rotationAngle) + + FOR pointIndex = 1 TO numPoints + PSET (oldPointXCoordinates(pointIndex), oldPointYCoordinates(pointIndex)), 0 ' Clear the previous position + + xCoordinate = pointXCoordinates(pointIndex) + yCoordinate = pointYCoordinates(pointIndex) + + ' Calculate the new x and y coordinate after rotation + newXCoordinate = xCoordinate * sineOfRotationAngle + yCoordinate * cosineOfRotationAngle + newYCoordinate = xCoordinate * cosineOfRotationAngle - yCoordinate * sineOfRotationAngle - FOR a = 1 TO numPoints - PSET (oldPointX(a), oldPointY(a)), 0 + ' Scale and translate the new x and y coordinates to the center of the screen + newXCoordinate = newXCoordinate * 7 + 160 + newYCoordinate = newYCoordinate * 7 + 100 - x = pointX(a) - y = pointY(a) + ' Store x and y on-screen coordinates for clearing on next iteration + oldPointXCoordinates(pointIndex) = newXCoordinate + oldPointYCoordinates(pointIndex) = newYCoordinate - newX = x * sineOfAngle + y * cosineOfAngle - newY = x * cosineOfAngle - y * sineOfAngle - newX = newX * 7 + 160 - newY = newY * 7 + 100 + PSET (newXCoordinate, newYCoordinate), 15 ' Draw the point at the new position + NEXT pointIndex - oldPointX(a) = newX - oldPointY(a) = newY - PSET (newX, newY), 15 - NEXT a +IF INKEY$ = "" THEN GOTO 1 ' Continue rotating if no key is pressed -IF INKEY$ = "" THEN GOTO 1 \ No newline at end of file