From: Svjatoslav Agejenko Date: Tue, 23 Jul 2024 07:49:33 +0000 (+0300) Subject: AI improved tutorials by adding comments and fixing code style X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=commitdiff_plain;h=00482b8dcb6d46c909eb03a47c9f4f91e0e0e457;p=qbasicapps.git AI improved tutorials by adding comments and fixing code style --- diff --git a/Graphics/3D/3dball.bas b/Graphics/3D/3dball.bas index 2a01547..0a4b70a 100755 --- a/Graphics/3D/3dball.bas +++ b/Graphics/3D/3dball.bas @@ -1,144 +1,175 @@ -' Svjatoslav Agejenko -' 1999 +' 1999, initial version by Svjatoslav Agejenko +' 2024, used AI to enhance program readability -DECLARE SUB newns () -DECLARE SUB getcor () -DECLARE SUB show (ang1!, ang2!, ang3!) +DECLARE SUB InitializeScene () +DECLARE SUB UpdateOrientation () +DECLARE SUB DisplayPoints (angle1 AS SINGLE, angle2 AS SINGLE, angle3 AS SINGLE) RANDOMIZE TIMER SCREEN 12 -DIM SHARED px(1 TO 1000) -DIM SHARED py(1 TO 1000) -DIM SHARED pz(1 TO 1000) - -DIM SHARED ox(1 TO 1000) -DIM SHARED oy(1 TO 1000) - -DIM SHARED mitup - -DIM SHARED pallx, pally -DIM SHARED pallxs, pallys - -DIM SHARED paln1s, paln2s, paln3s - -mitup = 500 ' Number of dots - -getcor - - -nrk1 = 0 -nrk2 = 0 -nrk3 = 0 - -1 -show nrk1, nrk2, nrk3 - -nrk1 = nrk1 + paln1s -nrk2 = nrk2 + paln2s -nrk3 = nrk3 + paln3s - -pallys = pallys + .1 -pallx = pallx + pallxs -pally = pally + pallys - -IF pally > 160 THEN pallys = -pallys: pallxs = pallxs + (RND * 2 - 1): newns -IF pallx < -200 THEN pallxs = -pallxs: newns -IF pallx > 200 THEN pallxs = -pallxs: newns - -a$ = INKEY$ -IF a$ <> "" THEN CLS : SYSTEM -GOTO 1 - -SUB getcor -PRINT "Calculating cordinates" -PRINT "Please wait....." - -FOR a = 1 TO mitup -ang1 = RND * 100 -ang2 = RND * 100 -ang3 = RND * 100 - -s1 = SIN(ang1) -c1 = COS(ang1) -s2 = SIN(ang2) -c2 = COS(ang2) -s3 = SIN(ang3) -c3 = COS(ang3) - -x = 50 -y = 0 -z = 0 - -x1 = x * s1 + y * c1 -y1 = x * c1 - y * s1 - -z1 = z * s2 + y1 * c2 -y2 = z * c2 - y1 * s2 - -z2 = z1 * s3 + x1 * c3 -x2 = z1 * c3 - x1 * s3 - -px(a) = x2 -py(a) = y2 -pz(a) = z2 - -NEXT a - - -pallxs = 2 + RND -pally = -100 - -newns - -CLS +' Shared arrays to hold the positions of points in 3D space +DIM SHARED pointX(1 TO 1000) AS SINGLE +DIM SHARED pointY(1 TO 1000) AS SINGLE +DIM SHARED pointZ(1 TO 1000) AS SINGLE + +' Shared arrays to hold the previous on-screen positions of points +DIM SHARED prevX(1 TO 1000) AS INTEGER +DIM SHARED prevY(1 TO 1000) AS INTEGER + +' Shared variables for simulation control +DIM SHARED totalPoints AS INTEGER +DIM SHARED ballX +DIM SHARED ballY +DIM SHARED ballVelocityX +DIM SHARED ballVelocityY + +' Shared variables for rotation angles and their velocities +DIM SHARED rotationAngle1 +DIM SHARED rotationAngle2 +DIM SHARED rotationAngle3 +DIM SHARED angularVelocity1 +DIM SHARED angularVelocity2 +DIM SHARED angularVelocity3 + +' Initialize the total number of points to be displayed +totalPoints = 500 + +CALL InitializeScene + +' Initialization of rotation angles and ball position +rotationAngle1 = 0 +rotationAngle2 = 0 +rotationAngle3 = 0 + +' Main loop +DO + CALL DisplayPoints(rotationAngle1, rotationAngle2, rotationAngle3) + + ' Update the rotation angles based on their velocities + rotationAngle1 = rotationAngle1 + angularVelocity1 + rotationAngle2 = rotationAngle2 + angularVelocity2 + rotationAngle3 = rotationAngle3 + angularVelocity3 + + ' Add force of gravity + ballVelocityY = ballVelocityY + .1 + + ' Update the ball's position and velocity + ballY = ballY + ballVelocityY + ballX = ballX + ballVelocityX + + ' Check for ball bounce conditions + IF ballY > 160 THEN + ballVelocityY = -ballVelocityY + ballVelocityX = ballVelocityX + (RND * 2 - 1) + CALL UpdateOrientation + END IF + + IF ballX < -200 OR ballX > 200 THEN + ballVelocityX = -ballVelocityX + CALL UpdateOrientation + END IF + + ' Check for user input to exit the program + a$ = INKEY$ + IF a$ <> "" THEN + CLS + SYSTEM + END IF +LOOP + +' Subroutine to display the points on the screen +SUB DisplayPoints (angle1 AS SINGLE, angle2 AS SINGLE, angle3 AS SINGLE) + + ' Calculate the sine and cosine for rotation angles + s1 = SIN(angle1) + c1 = COS(angle1) + s2 = SIN(angle2) + c2 = COS(angle2) + s3 = SIN(angle3) + c3 = COS(angle3) + + ' For each point, apply rotation transformations and project to 2D + FOR a = 1 TO totalPoints + x = pointX(a) + y = pointY(a) + z = pointZ(a) + + x1 = x * s1 + y * c1 + y1 = x * c1 - y * s1 + + z1 = z * s2 + y1 * c2 + y2 = z * c2 - y1 * s2 + + z2 = z1 * s3 + x1 * c3 + x2 = z1 * c3 - x1 * s3 + + ' Perspective projection + z2 = z2 + 200 + + ' Convert to screen coordinates and apply ball offset + xScreen = x2 / z2 * 320 + 320 + ballX + yScreen = y2 / z2 * 300 + 240 + ballY + + ' Erase the previous point position and draw the new one + PSET (prevX(a), prevY(a)), 0 + PSET (xScreen, yScreen), 3 + + ' Update the previous on-screen positions + prevX(a) = xScreen + prevY(a) = yScreen + NEXT a END SUB -SUB newns -paln1s = (RND - .5) / 16 -paln2s = (RND - .5) / 16 -paln3s = (RND - .5) / 16 -END SUB - -SUB show (ang1, ang2, ang3) - - -s1 = SIN(ang1) -c1 = COS(ang1) -s2 = SIN(ang2) -c2 = COS(ang2) -s3 = SIN(ang3) -c3 = COS(ang3) +' Subroutine to initialize the 3D points and ball properties +SUB InitializeScene + PRINT "Calculating coordinates" + PRINT "Please wait....." + ' Generate random 3D coordinates for each point + FOR a = 1 TO totalPoints + ang1 = RND * 100 + ang2 = RND * 100 + ang3 = RND * 100 -FOR a = 1 TO mitup + s1 = SIN(ang1) + c1 = COS(ang1) + s2 = SIN(ang2) + c2 = COS(ang2) + s3 = SIN(ang3) + c3 = COS(ang3) -x = px(a) -y = py(a) -z = pz(a) + ' Apply rotation transformations to the point + x = 50 + y = 0 + z = 0 -x1 = x * s1 + y * c1 -y1 = x * c1 - y * s1 + x1 = x * s1 + y * c1 + y1 = x * c1 - y * s1 -z1 = z * s2 + y1 * c2 -y2 = z * c2 - y1 * s2 + z1 = z * s2 + y1 * c2 + y2 = z * c2 - y1 * s2 -z2 = z1 * s3 + x1 * c3 -x2 = z1 * c3 - x1 * s3 + z2 = z1 * s3 + x1 * c3 + x2 = z1 * c3 - x1 * s3 -z2 = z2 + 200 - -x = x2 / z2 * 320 + 320 + pallx -y = y2 / z2 * 300 + 240 + pally - -PSET (ox(a), oy(a)), 0 -PSET (x, y), 3 -ox(a) = x -oy(a) = y - -NEXT a + pointX(a) = x2 + pointY(a) = y2 + pointZ(a) = z2 + NEXT a + ' Set the initial ball velocity + ballVelocityX = 2 + RND + ballY = -100 + CALL UpdateOrientation + CLS +END SUB +' Subroutine to update the angular velocities for rotation +SUB UpdateOrientation + angularVelocity1 = (RND - .5) / 16 + angularVelocity2 = (RND - .5) / 16 + angularVelocity3 = (RND - .5) / 16 END SUB