Using AI to improve code readability
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Sun, 11 Aug 2024 06:31:01 +0000 (09:31 +0300)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Sun, 11 Aug 2024 06:31:01 +0000 (09:31 +0300)
Math/05graaf.bas
Math/gravi2.bas
Math/sin_cos.bas
Math/sinus.bas

index f79a0e7..cdb2607 100755 (executable)
@@ -1,60 +1,81 @@
-' 2D graph\r
-' made by Svjatoslav Agejenko\r
-' in 2003.12\r
-' H-Page: svjatoslav.eu\r
-' E-Mail: svjatoslav@svjatoslav.eu\r
\r
-DECLARE SUB init ()\r
-DECLARE SUB pp (x1, y1, x2, y2, c!)\r
-DIM SHARED mul\r
+' 2D Graph Plotter by Svjatoslav Agejenko\r
 \r
-mul = 100\r
-init\r
+' 2003.12, Created initial version\r
+' 2024.08, Updated for better readability and maintainability\r
 \r
-ox = -320 / mul\r
-oy = 0\r
+' Homepage: https://svjatoslav.eu\r
+' email: svjatoslav@svjatoslav.eu\r
 \r
-FOR x = -320 / mul TO 320 / mul STEP 1 / mul\r
+DECLARE SUB InitializeGraphicsEnvironment ()\r
+DECLARE SUB PlotPoint (x1 AS SINGLE, y1 AS SINGLE, x2 AS SINGLE, y2 AS SINGLE, colorCode AS INTEGER)\r
 \r
-y = 1 - (COS(x * 2)) + (SIN(x * 2))   ' <<Type your formula there!\r
+' Scaling factor for the graph\r
+DIM SHARED scaleFactor AS INTEGER\r
+scaleFactor = 100\r
 \r
-pp x, y, ox, oy, 14\r
-ox = x\r
-oy = y\r
-NEXT x\r
+' Initialize the graphics environment\r
+InitializeGraphicsEnvironment\r
 \r
-SUB init\r
-SCREEN 12\r
+' Set the origin of the graph\r
+DIM originX AS SINGLE\r
+DIM originY AS SINGLE\r
+originX = -320 / scaleFactor\r
+originY = 0\r
 \r
-FOR x = -320 TO 320\r
-IF x / mul = x \ mul THEN LINE (x + 320, 0)-(x + 320, 479), 1\r
-NEXT x\r
+' Loop to plot each point on the graph based on the formula\r
+DIM currentX AS SINGLE\r
+DIM currentY AS SINGLE\r
+FOR currentX = -320 / scaleFactor TO 320 / scaleFactor STEP 1 / scaleFactor\r
+    ' Function to calculate y-value based on x-value\r
+    currentY = 1 - (COS(currentX * 2)) + (SIN(currentX * 2))   ' User-defined formula\r
 \r
-FOR y = -240 TO 240\r
-IF y / mul = y \ mul THEN LINE (0, y + 240)-(639, y + 240), 1\r
-NEXT y\r
+    ' Plot the point and update the origin for the next segment\r
+    PlotPoint currentX, currentY, originX, originY, 14\r
+    originX = currentX\r
+    originY = currentY\r
+NEXT currentX\r
 \r
+' Subroutine to initialize the graphics window and grid\r
+SUB InitializeGraphicsEnvironment\r
+    SCREEN 12\r
 \r
+    ' Draw horizontal grid lines\r
+    DIM horizontalGridStep AS SINGLE\r
+    FOR horizontalGridStep = -320 TO 320\r
+        IF horizontalGridStep / scaleFactor = horizontalGridStep \ scaleFactor THEN\r
+            LINE (horizontalGridStep + 320, 0)-(horizontalGridStep + 320, 479), 1\r
+        END IF\r
+    NEXT horizontalGridStep\r
 \r
-LINE (0, 240)-(639, 240), 3\r
-LINE (320, 0)-(320, 479), 3\r
+    ' Draw vertical grid lines\r
+    DIM verticalGridStep AS SINGLE\r
+    FOR verticalGridStep = -240 TO 240\r
+        IF verticalGridStep / scaleFactor = verticalGridStep \ scaleFactor THEN\r
+            LINE (0, verticalGridStep + 240)-(639, verticalGridStep + 240), 1\r
+        END IF\r
+    NEXT verticalGridStep\r
+\r
+    ' Draw the central axis lines\r
+    LINE (0, 240)-(639, 240), 3\r
+    LINE (320, 0)-(320, 479), 3\r
 END SUB\r
 \r
-SUB pp (x, y, x1, y1, c)\r
-\r
-x2 = (x * mul) + 320\r
-y2 = 240 - (y * mul)\r
-x3 = (x1 * mul) + 320\r
-y3 = 240 - (y1 * mul)\r
-IF x2 < 0 THEN GOTO 1\r
-IF y2 < 0 THEN GOTO 1\r
-IF x2 > 639 THEN GOTO 1\r
-IF y2 > 479 THEN GOTO 1\r
-IF x3 < 0 THEN GOTO 1\r
-IF y3 < 0 THEN GOTO 1\r
-IF x3 > 639 THEN GOTO 1\r
-IF y3 > 479 THEN GOTO 1\r
-LINE (x2, y2)-(x3, y3), 14\r
-1\r
+' Subroutine to plot a point on the graph\r
+SUB PlotPoint (x AS SINGLE, y AS SINGLE, x1 AS SINGLE, y1 AS SINGLE, colorCode AS INTEGER)\r
+    ' Convert graph coordinates to screen pixel coordinates\r
+    DIM screenX1 AS INTEGER\r
+    DIM screenY1 AS INTEGER\r
+    DIM screenX2 AS INTEGER\r
+    DIM screenY2 AS INTEGER\r
+\r
+    screenX1 = (x * scaleFactor) + 320\r
+    screenY1 = 240 - (y * scaleFactor)\r
+    screenX2 = (x1 * scaleFactor) + 320\r
+    screenY2 = 240 - (y1 * scaleFactor)\r
+\r
+    ' Check if the point is within the screen boundaries before plotting\r
+    IF screenX1 >= 0 AND screenY1 >= 0 AND screenX1 <= 639 AND screenY1 <= 479 AND screenX2 >= 0 AND screenY2 >= 0 AND screenX2 <= 639 AND screenY2 <= 479 THEN\r
+        LINE (screenX1, screenY1)-(screenX2, screenY2), colorCode\r
+    END IF\r
 END SUB\r
 \r
index 6eec101..138dcdc 100755 (executable)
@@ -1,35 +1,54 @@
-' Gravitation simulation\r
-' made by Svjatoslav Agejenko\r
-' in 2001\r
-' homepage: svjatoslav.eu\r
-' email:    svjatoslav@svjatoslav.eu\r
\r
-DEFDBL A-Z\r
-SCREEN 12\r
-\r
-x = -200\r
-y = 0\r
-xs = -1\r
-ys = 3\r
-\r
-\r
-1\r
+' Gravitation Simulation\r
+' By Svjatoslav Agejenko\r
+' Homepage: svjatoslav.eu\r
+' Email:    svjatoslav@svjatoslav.eu\r
+\r
+\r
+' 2001,    Initial version\r
+' 2024.08, Improved code readability\r
+\r
+' This program simulates the gravitational pull of a central mass\r
+' on a small object in two-dimensional space. The simulation is\r
+' visualized on the screen with the central mass as a large circle\r
+' and the orbiting object as a smaller circle.\r
+\r
+DEFDBL A-Z  ' Declare all variables as double precision for accuracy\r
+SCREEN 12    ' Set the graphics mode to 640x480 resolution, 16 colors\r
+\r
+' Initialize position and velocity of the orbiting object\r
+objX = -200    ' X-coordinate of the object\r
+objY = 0       ' Y-coordinate of the object\r
+objVelX = -1   ' X-velocity (speed) of the object\r
+objVelY = 3     ' Y-velocity (speed) of the object\r
+\r
+' Draw the central mass as a large circle\r
 CIRCLE (320, 240), 100, 3\r
-CIRCLE (320, 240), 2, 3\r
-x = x + xs\r
-y = y + ys\r
 \r
-v = SQR(x * x + y * y)\r
-j = 1 / v * 20\r
-'j = .1\r
+' Main simulation loop\r
+DO\r
+\r
+    ' Draw a small circle to represent the orbiting object\r
+    CIRCLE (objX + 320, objY + 240), 2, 14\r
+\r
+    ' Update the position of the orbiting object\r
+    objX = objX + objVelX\r
+    objY = objY + objVelY\r
+\r
+    ' Calculate the distance from the central mass\r
+    dist = SQR(objX * objX + objY * objY)\r
+\r
+    ' Calculate the gravitational acceleration towards the center\r
+    gravAccel = 20 / dist   ' Gravitational constant for this simulation\r
 \r
-s = ABS(x) + ABS(y)\r
-xs = xs + (j * (-x) / s)\r
-ys = ys + (j * (-y) / s)\r
+    ' Adjust velocities based on gravitational pull and distance\r
+    objVelX = objVelX + (gravAccel * (-objX) / dist)\r
+    objVelY = objVelY + (gravAccel * (-objY) / dist)\r
 \r
+    ' Draw a line to show the object's trajectory\r
+    LINE (objX + 320, objY + 240)-(320, 240), 1\r
+   \r
+   \r
+    SOUND 0, .1\r
 \r
-CIRCLE (x + 320, y + 240), 2, 14\r
-LINE (x + 320, y + 240)-(320, 240), 1\r
-SOUND 0, .1\r
-GOTO 1\r
+LOOP\r
 \r
index 81a1186..6ab72ad 100755 (executable)
-' SIN & COS table\r
-' made by Svjatoslav Agejenko\r
-' in 2003.12\r
-' homepage: svjatoslav.eu\r
-' email:    svjatoslav@svjatoslav.eu\r
\r
-xs = 640\r
-ys = 480\r
-scr = 12 'Video mode\r
-strs = 0\r
-\r
-xs = xs / 11.3\r
-ys = ys / 11.7\r
-\r
-IF strs = 0 THEN ELSE GOTO 1\r
-\r
-SELECT CASE scr\r
-    CASE 12, 11\r
-        strs = 16\r
-\r
-    CASE 9, 10\r
-        strs = 14\r
-\r
-    CASE 1, 13, 2, 7, 8\r
-        strs = 8\r
-END SELECT\r
-1\r
-\r
-SCREEN scr\r
-\r
-FOR b = 1 TO 10\r
-    LINE (0, b * ys)-(xs * 10, b * ys), 8\r
-    LINE (b * xs, 0)-(b * xs, ys * 10), 8\r
-    LOCATE 10 * ys / strs + 2, b * xs / 8 + 1\r
-    PRINT CHR$(b + 48)\r
-NEXT b\r
-\r
-LOCATE 10 * ys / strs + 2, xs * 10 / 8 + 0\r
-PRINT 10\r
-LOCATE 1 * ys / strs + 1, xs * 10 / 8 + 3\r
-PRINT -1\r
-LOCATE 5 * ys / strs + 1, xs * 10 / 8 + 3\r
-PRINT 0\r
-LOCATE 10 * ys / strs, xs * 10 / 8 + 3\r
-PRINT 1\r
-\r
-LINE (0, ys * 5 + 1)-(xs * 10, ys * 5 + 1), 14\r
-LINE (5 * xs + 1, 0)-(5 * xs + 1, 10 * ys), 14\r
-\r
-FOR a = 0 TO 10 STEP .05\r
-    x = a * xs\r
-    y = SIN(a) * ys * 5 + ys * 5\r
-    IF a > 0 THEN LINE (x1, y1)-(x, y), 15\r
-    x1 = x\r
-    y1 = y\r
-NEXT a\r
-LOCATE y / strs + 1, xs * 10 / 8\r
-PRINT "sin"\r
-\r
-FOR a = 0 TO 10 STEP .05\r
-    x = a * xs\r
-    y = COS(a) * ys * 5 + ys * 5\r
-    IF a > 0 THEN LINE (x1, y1)-(x, y), 12\r
-    x1 = x\r
-    y1 = y\r
-NEXT a\r
-LOCATE y / strs + 1, xs * 10 / 8\r
-PRINT "cos"\r
+' SIN & COS table generator\r
+' Created by Svjatoslav Agejenko.\r
+' Homepage: https://svjatoslav.eu\r
+' Email:    svjatoslav@svjatoslav.eu\r
 \r
+' 2003.12, Initial version.\r
+' 2024.08, Updated code readability.\r
+\r
+\r
+' Screen dimensions and video mode settings\r
+screenWidth = 640\r
+screenHeight = 480\r
+videoMode = 12 ' Video mode switch (0 for text mode, non-zero for graphics mode)\r
+stringSize = 0 ' String size for text mode\r
+\r
+' Adjust screen dimensions for a more accurate representation\r
+screenWidth = screenWidth / 11.3\r
+screenHeight = screenHeight / 11.7\r
+\r
+' Determine string size based on video mode\r
+IF stringSize = 0 THEN\r
+    SELECT CASE videoMode\r
+        CASE 12, 11\r
+            stringSize = 16\r
+\r
+        CASE 9, 10\r
+            stringSize = 14\r
+\r
+        CASE 1, 13, 2, 7, 8\r
+            stringSize = 8\r
+    END SELECT\r
+ELSE\r
+    GOTO InitializeScreen\r
+END IF\r
+\r
+InitializeScreen:\r
+SCREEN videoMode\r
+\r
+' Draw grid and label axes\r
+FOR gridLine = 1 TO 10\r
+    ' Draw horizontal grid lines\r
+    LINE (0, gridLine * screenHeight)-(screenWidth * 10, gridLine * screenHeight), 8\r
+\r
+    ' Draw vertical grid lines\r
+    LINE (gridLine * screenWidth, 0)-(gridLine * screenWidth, screenHeight * 10), 8\r
+\r
+    ' Label horizontal axis with numbers\r
+    textRow = 10 * screenHeight / stringSize + 2\r
+    textCol = gridLine * screenWidth / 8 + 1\r
+    LOCATE textRow, textCol\r
+    PRINT CHR$(gridLine + 48);\r
+NEXT gridLine\r
+\r
+' Label the end of the horizontal axis\r
+LOCATE 10 * screenHeight / stringSize + 2, screenWidth * 10 / 8\r
+PRINT "10";\r
+\r
+' Label special points on the vertical axis\r
+LOCATE 1 * screenHeight / stringSize + 1, screenWidth * 10 / 8 + 3\r
+PRINT "-1";\r
+LOCATE 5 * screenHeight / stringSize + 1, screenWidth * 10 / 8 + 3\r
+PRINT "0";\r
+LOCATE 10 * screenHeight / stringSize, screenWidth * 10 / 8 + 3\r
+PRINT "1";\r
+\r
+' Draw central horizontal and vertical lines\r
+LINE (0, screenHeight * 5 + 1)-(screenWidth * 10, screenHeight * 5 + 1), 14\r
+LINE (5 * screenWidth + 1, 0)-(5 * screenWidth + 1, 10 * screenHeight), 14\r
+\r
+' Plot SIN function\r
+FOR angle = 0 TO 10 STEP .05\r
+    xPosition = angle * screenWidth\r
+    yPosition = SIN(angle) * screenHeight * 5 + screenHeight * 5\r
+    IF angle > 0 THEN LINE (xPositionPrev, yPositionPrev)-(xPosition, yPosition), 15\r
+    xPositionPrev = xPosition\r
+    yPositionPrev = yPosition\r
+NEXT angle\r
+\r
+' Label the SIN curve\r
+textRow = yPosition / stringSize + 1\r
+textCol = screenWidth * 10 / 8\r
+LOCATE textRow, textCol\r
+PRINT "sin";\r
+\r
+' Plot COS function\r
+FOR angle = 0 TO 10 STEP .05\r
+    xPosition = angle * screenWidth\r
+    yPosition = COS(angle) * screenHeight * 5 + screenHeight * 5\r
+    IF angle > 0 THEN LINE (xPositionPrev, yPositionPrev)-(xPosition, yPosition), 12\r
+    xPositionPrev = xPosition\r
+    yPositionPrev = yPosition\r
+NEXT angle\r
+\r
+' Label the COS curve\r
+textRow = yPosition / stringSize + 1\r
+textCol = screenWidth * 10 / 8\r
+LOCATE textRow, textCol\r
+PRINT "cos";\r
+\r
+' Wait for user input before exiting\r
 a$ = INPUT$(1)\r
 SYSTEM\r
 \r
index b4444ee..0504391 100755 (executable)
@@ -1,27 +1,44 @@
-' Sinus calculator\r
-' made by Svjatoslav Agejenko\r
-' in 2003.12\r
-' H-Page: svjatoslav.eu\r
-' E-Mail: svjatoslav@svjatoslav.eu\r
\r
-\r
-' this program calculates sinus without using SIN function\r
+' Sinus Calculator\r
+' by Svjatoslav Agejenko in 2003.12\r
+' Homepage: https://svjatoslav.eu\r
+' Email: svjatoslav@svjatoslav.eu\r
 \r
+' This program calculates the sine of an angle without using\r
+' the built-in SIN function\r
 \r
 SCREEN 12\r
 \r
+' Draw a horizontal baseline\r
 LINE (0, 240)-(640, 240), 15\r
 \r
-r = 0\r
-r1 = 1\r
-FOR x = 1 TO 639\r
-y = SIN(x / 100) * 100 + 240  ' using SIN\r
-PSET (x, y), 15\r
-\r
-r1 = r1 + ((0 - r) / 10000)\r
-r = r + r1                   '  without SIN\r
-y = r\r
-PSET (x, y + 241), 12\r
-\r
-NEXT x\r
+' Initialize variables for the sine wave calculation\r
+' r represents the current value of the sine approximation\r
+' r1 represents the rate of change of the sine approximation\r
+LET radius = 0\r
+LET rateOfChange = 1\r
+\r
+' Iterate over each horizontal pixel to calculate and plot the sine values\r
+FOR angleDegrees = 1 TO 639\r
+    ' Calculate the actual sine value using the built-in SIN function\r
+    ' for comparison.\r
+    ' Scale and translate the sine wave to fit within the screen coordinates.\r
+    LET trueSineY = SIN(angleDegrees / 100) * 100 + 240\r
+    PSET (angleDegrees, trueSineY), 15\r
+\r
+    ' Update the rate of change and the radius (sine approximation)\r
+    ' This is a simple implementation of the differential equation\r
+    ' that defines the sine function, effectively integrating over time\r
+    LET rateOfChange = rateOfChange + ((0 - radius) / 10000)\r
+    LET radius = radius + rateOfChange\r
+\r
+    ' Calculate the approximate sine value using our own method\r
+    ' Offset the plot by 241 pixels to display below the true sine wave\r
+    LET approxSineY = radius\r
+    PSET (angleDegrees, approxSineY + 241), 12\r
+NEXT angleDegrees\r
+\r
+' Wait for a key press before ending the program\r
+PRINT "Press any key to exit."\r
+DO UNTIL INKEY$ <> ""\r
+LOOP\r
 \r