Using AI to improve code readability
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Thu, 15 Aug 2024 06:53:33 +0000 (09:53 +0300)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Thu, 15 Aug 2024 06:53:33 +0000 (09:53 +0300)
Graphics/Spirals/spiral2.bas
Graphics/Spirals/spiral4.bas

index a127ddb..6cac232 100644 (file)
@@ -1,40 +1,56 @@
-' spiral\r
-' made by Svjatoslav Agejenko\r
-' in 2003.12\r
-' H-Page: svjatoslav.eu\r
-' E-Mail: svjatoslav@svjatoslav.eu\r
\r
-DECLARE SUB linem (x1!, y1!, x2!, y2!, col!)\r
-DIM SHARED linevx(1 TO 100)\r
-DIM SHARED linevy(1 TO 100)\r
-DIM SHARED z\r
-DIM SHARED zz\r
+DECLARE SUB DrawLine (startX AS DOUBLE, startY AS DOUBLE, endX AS DOUBLE, endY AS DOUBLE, col AS INTEGER)\r
+' Program to render fancy looking spiral.\r
+' By Svjatoslav Agejenko.\r
+' Email: svjatoslav@svjatoslav.eu\r
+' Homepage: http://www.svjatoslav.eu\r
+'\r
+' Changelog:\r
+' 2003.12, Initial version\r
+' 2024.08, Imroved program readability using AI\r
+\r
+DIM SHARED lineVertexX(1 TO 100) AS DOUBLE\r
+DIM SHARED lineVertexY(1 TO 100) AS DOUBLE\r
+DIM SHARED depth AS INTEGER\r
+DIM SHARED tempDepth AS INTEGER\r
 SCREEN 12\r
-su = 200\r
-z = 0\r
-FOR a = 1 TO 30 STEP .1\r
-su = (30 - a) * 7\r
-x = SIN(a) * su + 200\r
-y = COS(a) * su + 200\r
-zz = a\r
-linem x + (x / 2) + (a * 3), (y - (x / 3)) + (a * 3), x + 25, y + 25 - (a * 3), z\r
-z = 15\r
-NEXT a\r
 \r
-a$ = INPUT$(1)\r
-SYSTEM\r
+' Initialize the scale factor for the spiral\r
+spiralScaleFactor = 200\r
+depth = 0\r
 \r
-SUB linem (x1, y1, x2, y2, col)\r
-x3 = (x2 - x1) / zz\r
-y3 = (y2 - y1) / zz\r
+' Generate the spiral by iterating through angles and scaling appropriately\r
+FOR angle = 1 TO 30 STEP .1\r
+    ' Calculate the current scale based on the remaining distance to the center\r
+    spiralScaleFactor = (30 - angle) * 7\r
+    ' Convert polar coordinates to cartesian for the current point\r
+    xPosition = SIN(angle) * spiralScaleFactor + 200\r
+    yPosition = COS(angle) * spiralScaleFactor + 200\r
+    ' Store the current depth (z-axis value)\r
+    tempDepth = angle\r
+    ' Draw a line from the previous point to the current point with a color based on depth\r
+    DrawLine xPosition + (xPosition / 2) + (angle * 3), (yPosition - (xPosition / 3)) + (angle * 3), xPosition + 25, yPosition + 25 - (angle * 3), depth\r
+    ' Set the color for the next segment\r
+    depth = 15\r
+NEXT angle\r
 \r
-FOR a = 1 TO zz\r
-IF linevx(a) > 0 THEN LINE (linevx(a), linevy(a))-(x1, y1), col\r
-linevx(a) = x1\r
-linevy(a) = y1\r
-x1 = x1 + x3\r
-y1 = y1 + y3\r
-LINE (linevx(a), linevy(a))-(x1, y1), col\r
-NEXT a\r
-END SUB\r
+' Wait for user input to close the program\r
+userInput$ = INPUT$(1)\r
 \r
+SUB DrawLine (startX AS DOUBLE, startY AS DOUBLE, endX AS DOUBLE, endY AS DOUBLE, col AS INTEGER)\r
+    ' Calculate the step increments for x and y based on the depth\r
+    deltaX = (endX - startX) / tempDepth\r
+    deltaY = (endY - startY) / tempDepth\r
+\r
+    FOR segmentIndex = 1 TO tempDepth\r
+        ' If there is a previous vertex, draw a line to the new starting point\r
+        IF lineVertexX(segmentIndex) > 0 THEN LINE (lineVertexX(segmentIndex), lineVertexY(segmentIndex))-(startX, startY), col\r
+        ' Store the current starting point as the next vertex\r
+        lineVertexX(segmentIndex) = startX\r
+        lineVertexY(segmentIndex) = startY\r
+        ' Increment the starting point by the calculated deltas\r
+        startX = startX + deltaX\r
+        startY = startY + deltaY\r
+        ' Draw a line from the stored vertex to the new starting point\r
+        LINE (lineVertexX(segmentIndex), lineVertexY(segmentIndex))-(startX, startY), col\r
+    NEXT segmentIndex\r
+END SUB\r
index 3431132..f98bff5 100644 (file)
@@ -1,30 +1,43 @@
-' spiral\r
-' made by Svjatoslav Agejenko\r
-' in 2003.12\r
-' H-Page: svjatoslav.eu\r
-' E-Mail: svjatoslav@svjatoslav.eu\r
\r
-DIM SHARED torux(1 TO 10000)\r
-DIM SHARED toruy(1 TO 10000)\r
-DIM SHARED tor\r
-SCREEN 12\r
-su = 200\r
-tor = 0\r
-FOR a = 1 TO 100 STEP .05\r
-tor = tor + 1\r
-su = 100 - a\r
-x = SIN(a) * su * 3 + 320\r
-y = COS(a) * su + 300\r
-y = y + (SIN((a + 20) / 10) * a)\r
-torux(tor) = x\r
-toruy(tor) = y\r
-PSET (x, y), 15\r
-NEXT a\r
-\r
-FOR a = 1 TO tor - 125\r
-LINE (torux(a), toruy(a))-(torux(a + 125), toruy(a + 125)), 15\r
-NEXT a\r
+' Program to render fancy looking spiral.\r
+' By Svjatoslav Agejenko.\r
+' Email: svjatoslav@svjatoslav.eu\r
+' Homepage: http://www.svjatoslav.eu\r
+'\r
+' Changelog:\r
+' 2003.12, Initial version\r
+' 2024.08, Imroved program readability using AI\r
 \r
-a$ = INPUT$(1)\r
-SYSTEM\r
+DIM SHARED spiralX(1 TO 10000) AS SINGLE ' X coordinates of the spiral points\r
+DIM SHARED spiralY(1 TO 10000) AS SINGLE ' Y coordinates of the spiral points\r
+DIM SHARED pointCount AS INTEGER ' Total number of points plotted\r
+SCREEN 12 ' Set screen resolution to 640x480 with 16 colors\r
+\r
+' Initialize the scale factor for the spiral\r
+scaleFactor = 200\r
+pointCount = 0\r
+\r
+' Calculate and plot each point on the spiral\r
+FOR angle = 1 TO 100 STEP .05\r
+    pointCount = pointCount + 1\r
+    scaleFactor = 100 - angle ' Update the scaling factor as the loop progresses\r
+\r
+    ' Calculate the X and Y coordinates based on the sine and cosine of the angle\r
+    spiralX(pointCount) = SIN(angle) * scaleFactor * 3 + 320\r
+    spiralY(pointCount) = COS(angle) * scaleFactor + 300\r
 \r
+    ' Apply a vertical displacement to create a more dynamic effect\r
+    spiralY(pointCount) = spiralY(pointCount) + (SIN((angle + 20) / 10) * angle)\r
+\r
+    ' Plot the point on the screen\r
+    PSET (spiralX(pointCount), spiralY(pointCount)), 15\r
+NEXT angle\r
+\r
+' Draw lines between points to create the spiral effect\r
+FOR segmentStart = 1 TO pointCount - 125\r
+    LINE (spiralX(segmentStart), spiralY(segmentStart)) - _\r
+         (spiralX(segmentStart + 125), spiralY(segmentStart + 125)), 15\r
+NEXT segmentStart\r
+\r
+' Wait for user input before exiting\r
+a$ = INPUT$(1)\r
+END ' Exit the program\r