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