From 54a126a336a646fe2447c9540b1d4630d31fe0eb Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko Date: Tue, 27 Aug 2024 15:38:17 +0300 Subject: [PATCH] Using AI to improve code readability --- Graphics/strange2.bas | 80 ++++++---- Graphics/texture.bas | 324 +++++++++++++++++++++++------------------ Graphics/tree.bas | 132 +++++++++-------- Simulation/interf.BAS | 63 ++++---- Simulation/interf2.BAS | 70 +++++---- Simulation/liquid.bas | 70 ++++++--- 6 files changed, 431 insertions(+), 308 deletions(-) diff --git a/Graphics/strange2.bas b/Graphics/strange2.bas index 35a4f9c..c150ee4 100755 --- a/Graphics/strange2.bas +++ b/Graphics/strange2.bas @@ -1,41 +1,65 @@ -' Svjatoslav Agejenko 2000 +' By Svjatoslav Agejenko. +' Email: svjatoslav@svjatoslav.eu +' Homepage: http://www.svjatoslav.eu +' +' Changelog: +' 2000, Initial version +' 2024.08, Improved program readability using AI -DECLARE SUB lu (x%, y%, l%, c2%) +DECLARE SUB DrawLine (x%, y%, l%, clr%) + DEFINT A-Z SCREEN 12 +' Set up the palette FOR a = 0 TO 16 -OUT &H3C8, a -OUT &H3C9, a * 4 -OUT &H3C9, a * 4 -OUT &H3C9, a * 4 + OUT &H3C8, a + OUT &H3C9, a * 4 + OUT &H3C9, a * 4 + OUT &H3C9, a * 4 NEXT +' Draw the pattern FOR x = 0 TO 200 -y = SQR((200 - x) * x) / 2 -lu x + 200, y + 200, y * 2, t -LINE (x + 200, y + 202)-(x + 200, y + 250), t -IF x < 195 THEN -LINE (300 + ((x - 100) / 2), y / 2 + 328)-(x + 200, y + 252), t -END IF -LINE (300 + ((x - 100) / 2), y / 2 + 330)-(300 + ((x - 100) / 2), y / 2 + 370), t + y = SQR((200 - x) * x) / 2 + DrawLine x + 200, y + 200, y * 2, t + LINE (x + 200, y + 202)-(x + 200, y + 250), t + IF x < 195 THEN + LINE (300 + ((x - 100) / 2), y / 2 + 328)-(x + 200, y + 252), t + END IF + LINE (300 + ((x - 100) / 2), y / 2 + 330)-(300 + ((x - 100) / 2), y / 2 + 370), t NEXT x -SUB lu (x, y, l, c2) - -c = 650 - y - x -1 -IF c > 30 THEN c = c - 30: GOTO 1 -c2 = c -IF c2 > 15 THEN c2 = 30 - c2 - -FOR y1 = y TO y - l STEP -1 -c1 = c -IF c1 > 15 THEN c1 = 30 - c1 -PSET (x, y1), c1 -c = c + 1 -IF c > 30 THEN c = c - 30 -NEXT y1 +SUB DrawLine (x, y, l, clr) + ' Calculate the initial color value + c = 650 - y - x + + ' Adjust color to be within a specific range +1 IF c > 30 THEN + c = c - 30 + GOTO 1 + END IF + + clr = c + IF clr > 15 THEN + clr = 30 - clr + END IF + + ' Draw a vertical line with varying colors + FOR y1 = y TO y - l STEP -1 + c1 = c + IF c1 > 15 THEN + c1 = 30 - c1 + END IF + + PSET (x, y1), c1 + + ' Increment the color and wrap around if necessary + c = c + 1 + IF c > 30 THEN + c = c - 30 + END IF + NEXT y1 END SUB diff --git a/Graphics/texture.bas b/Graphics/texture.bas index c6a48c2..dc3a666 100755 --- a/Graphics/texture.bas +++ b/Graphics/texture.bas @@ -1,170 +1,216 @@ ' Texture mapping -' by Svjatoslav Agejenko -' 04.2003 - -DECLARE SUB demo3 () -DECLARE SUB demo2 () -DECLARE SUB demo1 () -DECLARE SUB hline (x1!, y!, x2!, tx1!, ty1!, tx2!, ty2!) -DECLARE SUB polygon (x1!, y1!, x2!, y2!, x3!, y3!, tx1!, ty1!, tx2!, ty2!, tx3!, ty3!) -DECLARE SUB pline (x1!, y1!, x2!, y2!, tx1!, ty1!, tx2!, ty2!) -DECLARE SUB start () +' By Svjatoslav Agejenko. +' Email: svjatoslav@svjatoslav.eu +' Homepage: http://www.svjatoslav.eu +' +' Changelog: +' 2003.04, Initial version +' 2024.08, Improved program readability using AI + +DECLARE SUB Demo3() +DECLARE SUB Demo2() +DECLARE SUB Demo1() +DECLARE SUB HLine(x1!, y!, x2!, tx1!, ty1!, tx2!, ty2!) +DECLARE SUB Polygon(x1!, y1!, x2!, y2!, x3!, y3!, tx1!, ty1!, tx2!, ty2!, tx3!, ty3!) +DECLARE SUB PLine(x1!, y1!, x2!, y2!, tx1!, ty1!, tx2!, ty2!) +DECLARE SUB Start() DIM SHARED img(0 TO 100, 0 TO 100) DIM SHARED bufx(0 TO 199) DIM SHARED buftx(0 TO 199) DIM SHARED bufty(0 TO 199) -start -demo1 -demo2 -demo3 +Start +Demo1 +Demo2 +Demo3 SYSTEM -SUB demo1 -polygon 10, 10, 300, 80, 100, 180, 1, 1, 99, 1, 30, 99 - -a$ = INPUT$(1) -3 -x1 = RND * 300 + 10 -x2 = RND * 300 + 10 -x3 = RND * 300 + 10 -y1 = RND * 180 + 10 -y2 = RND * 180 + 10 -y3 = RND * 180 + 10 -polygon x1, y1, x2, y2, x3, y3, 1, 1, 99, 1, 30, 99 -IF INKEY$ = "" THEN GOTO 3 -CLS +SUB Demo1 + ' Draw a polygon with predefined vertices and texture coordinates + Polygon 10, 10, 300, 80, 100, 180, 1, 1, 99, 1, 30, 99 + + ' Wait for user input + a$ = INPUT$(1) + + ' Label to repeat the polygon drawing +3: + x1 = RND * 300 + 10 + x2 = RND * 300 + 10 + x3 = RND * 300 + 10 + y1 = RND * 180 + 10 + y2 = RND * 180 + 10 + y3 = RND * 180 + 10 + + ' Draw a polygon with random vertices and predefined texture coordinates + Polygon x1, y1, x2, y2, x3, y3, 1, 1, 99, 1, 30, 99 + + ' Repeat the process until a key is pressed + IF INKEY$ = "" THEN GOTO 3 + + CLS END SUB -SUB demo2 - -n = 0 -4 -x1 = SIN(n) * 80 + 160 -y1 = COS(n) * 80 + 100 -x2 = SIN(n + 2) * 80 + 160 -y2 = COS(n + 2) * 80 + 100 -x3 = SIN(n + 4) * 90 + 160 -y3 = COS(n + 4) * 90 + 100 -polygon x1, y1, x2, y2, x3, y3, 1, 1, 99, 1, 30, 99 -n = n + .1 -IF INKEY$ = "" THEN GOTO 4 -CLS +SUB Demo2 + n = 0 + + ' Label to repeat the polygon drawing +4: + x1 = SIN(n) * 80 + 160 + y1 = COS(n) * 80 + 100 + x2 = SIN(n + 2) * 80 + 160 + y2 = COS(n + 2) * 80 + 100 + x3 = SIN(n + 4) * 90 + 160 + y3 = COS(n + 4) * 90 + 100 + + ' Draw a polygon with calculated vertices and predefined texture coordinates + Polygon x1, y1, x2, y2, x3, y3, 1, 1, 99, 1, 30, 99 + + n = n + .1 + + ' Repeat the process until a key is pressed + IF INKEY$ = "" THEN GOTO 4 + + CLS END SUB -SUB demo3 - -n = 0 -5 -x1 = SIN(n) * 40 + 50 -y1 = COS(n) * 40 + 50 -x2 = SIN(n + 2) * 40 + 50 -y2 = COS(n + 2) * 40 + 50 -x3 = SIN(n + 4) * 40 + 50 -y3 = COS(n + 4) * 40 + 50 -polygon 1, 50, 300, 1, 100, 180, x1, y1, x2, y2, x3, y3 -n = n + .1 -IF INKEY$ = "" THEN GOTO 5 -CLS +SUB Demo3 + n = 0 + + ' Label to repeat the polygon drawing +5: + x1 = SIN(n) * 40 + 50 + y1 = COS(n) * 40 + 50 + x2 = SIN(n + 2) * 40 + 50 + y2 = COS(n + 2) * 40 + 50 + x3 = SIN(n + 4) * 40 + 50 + y3 = COS(n + 4) * 40 + 50 + + ' Draw a polygon with calculated vertices and predefined texture coordinates + Polygon 1, 50, 300, 1, 100, 180, x1, y1, x2, y2, x3, y3 + + n = n + .1 + + ' Repeat the process until a key is pressed + IF INKEY$ = "" THEN GOTO 5 + + CLS END SUB -SUB hline (x1, y, x2, tx1, ty1, tx2, ty2) - -IF INT(x2) = INT(x1) THEN GOTO 2 -IF x2 > x1 THEN - nx1 = INT(x1) - nx2 = INT(x2) - ntx1 = tx1 - nty1 = ty1 - ntx2 = tx2 - nty2 = ty2 -ELSE - nx1 = INT(x2) - nx2 = INT(x1) - ntx1 = tx2 - nty1 = ty2 - ntx2 = tx1 - nty2 = ty1 -END IF - -v = nx2 - nx1 -tvx = ntx2 - ntx1 -tvy = nty2 - nty1 - -FOR a = 0 TO v - rtx = tvx * a / v + ntx1 - rty = tvy * a / v + nty1 - PSET (a + nx1, y), img(rtx, rty) -NEXT a - -2 +SUB HLine(x1, y, x2, tx1, ty1, tx2, ty2) + + ' Exit if the horizontal line has zero length + IF INT(x2) = INT(x1) THEN GOTO 2 + + ' Determine the direction and initialize variables + IF x2 > x1 THEN + nx1 = INT(x1) + nx2 = INT(x2) + ntx1 = tx1 + nty1 = ty1 + ntx2 = tx2 + nty2 = ty2 + ELSE + nx1 = INT(x2) + nx2 = INT(x1) + ntx1 = tx2 + nty1 = ty2 + ntx2 = tx1 + nty2 = ty1 + END IF + + ' Calculate the line parameters + v = nx2 - nx1 + tvx = ntx2 - ntx1 + tvy = nty2 - nty1 + + FOR a = 0 TO v + rtx = tvx * a / v + ntx1 + rty = tvy * a / v + nty1 + + ' Set the pixel color using texture coordinates + PSET (a + nx1, y), img(rtx, rty) + NEXT a + +2: END SUB -SUB pline (x1, y1, x2, y2, tx1, ty1, tx2, ty2) -m = ABS(y2 - y1) -IF m = 0 THEN GOTO 1 - -vy = y2 - y1 -vx = x2 - x1 - -tvy = ty2 - ty1 -tvx = tx2 - tx1 - -FOR a = 0 TO m - rx = vx * a / m + x1 - ry = vy * a / m + y1 - trx = tvx * a / m + tx1 - try = tvy * a / m + ty1 -' PSET (rx, ry), 14 - IF bufx(ry) = -1 THEN - bufx(ry) = rx - buftx(ry) = trx - bufty(ry) = try - ELSE - hline bufx(ry), ry, rx, buftx(ry), bufty(ry), trx, try - END IF -NEXT a - -1 +SUB PLine(x1, y1, x2, y2, tx1, ty1, tx2, ty2) + + ' Calculate the number of vertical steps + m = ABS(y2 - y1) + + IF m = 0 THEN GOTO 1 + + vy = y2 - y1 + vx = x2 - x1 + + tvy = ty2 - ty1 + tvx = tx2 - tx1 + + FOR a = 0 TO m + rx = vx * a / m + x1 + ry = vy * a / m + y1 + trx = tvx * a / m + tx1 + try = tvy * a / m + ty1 + + ' Check if the buffer is empty for this row + IF bufx(ry) = -1 THEN + bufx(ry) = rx + buftx(ry) = trx + bufty(ry) = try + ELSE + HLine bufx(ry), ry, rx, buftx(ry), bufty(ry), trx, try + END IF + NEXT a + +1: END SUB -SUB polygon (x1, y1, x2, y2, x3, y3, tx1, ty1, tx2, ty2, tx3, ty3) +SUB Polygon(x1, y1, x2, y2, x3, y3, tx1, ty1, tx2, ty2, tx3, ty3) -FOR a = 0 TO 199 - bufx(a) = -1 -NEXT a + ' Initialize the buffer + FOR a = 0 TO 199 + bufx(a) = -1 + NEXT a -pline x1, y1, x2, y2, tx1, ty1, tx2, ty2 -pline x1, y1, x3, y3, tx1, ty1, tx3, ty3 -pline x3, y3, x2, y2, tx3, ty3, tx2, ty2 + ' Draw three lines connecting the vertices of the polygon + PLine x1, y1, x2, y2, tx1, ty1, tx2, ty2 + PLine x1, y1, x3, y3, tx1, ty1, tx3, ty3 + PLine x3, y3, x2, y2, tx3, ty3, tx2, ty2 END SUB -SUB start +SUB Start -SCREEN 13 + ' Set the screen mode to 320x200 with 256 colors + SCREEN 13 -FOR a = 1 TO 100 - x = RND * 150 - y = RND * 150 - c = RND * 255 - CIRCLE (x, y), RND * 20 + 3, c - PAINT (x, y), c -NEXT a + ' Draw random circles on the screen + FOR a = 1 TO 100 + x = RND * 150 + y = RND * 150 + c = RND * 255 + CIRCLE (x, y), RND * 20 + 3, c + PAINT (x, y), c + NEXT a -LOCATE 8, 8 -PRINT "Test!" -a$ = INPUT$(1) + ' Display "Test!" on the screen + LOCATE 8, 8 + PRINT "Test!" -FOR y = 0 TO 100 - FOR x = 0 TO 100 - img(x, y) = POINT(x + 20, y + 20) - PSET (x + 20, y + 20), 0 - NEXT x -NEXT y -CLS + ' Wait for user input + a$ = INPUT$(1) -END SUB + ' Copy the screen content to the image array and clear the screen + FOR y = 0 TO 100 + FOR x = 0 TO 100 + img(x, y) = POINT(x + 20, y + 20) + PSET (x + 20, y + 20), 0 + NEXT x + NEXT y + + CLS +END SUB diff --git a/Graphics/tree.bas b/Graphics/tree.bas index 642ccd8..a4ce143 100755 --- a/Graphics/tree.bas +++ b/Graphics/tree.bas @@ -1,23 +1,26 @@ ' Render tree that starts with single root and then branches out. -' made by Svjatoslav Agejenko -' in 2001 -' H-Page: svjatoslav.eu -' E-Mail: svjatoslav@svjatoslav.eu - -DECLARE SUB start () -DECLARE SUB show (d%) -DECLARE SUB setpal () -DECLARE SUB showpal () -DEFINT A-Y +' By Svjatoslav Agejenko. +' Email: svjatoslav@svjatoslav.eu +' Homepage: http://www.svjatoslav.eu +' +' Changelog: +' 2001, Initial version +' 2024.08, Improved program readability using AI +DECLARE SUB start() +DECLARE SUB show(d%) +DECLARE SUB setpal() +DECLARE SUB showpal() +DEFINT A - Y + DIM SHARED x(1 TO 500) DIM SHARED y(1 TO 500) DIM SHARED s(1 TO 500) DIM SHARED x4(1 TO 500) DIM SHARED y4(1 TO 500) - + DIM SHARED z(1 TO 500) DIM SHARED mitu @@ -31,69 +34,82 @@ x(1) = 420 y(1) = 340 s(1) = 70 * 100 z(1) = 1 - + +' Main loop to render the tree branches FOR tr = 1 TO 6 -FOR b = 1 TO 50 -FOR a = 1 TO mitu -show a -NEXT a -NEXT b - -FOR a = 1 TO mitu -x(mitu + a) = x(a) -y(mitu + a) = y(a) -s(mitu + a) = s(a) -z(mitu + a) = z(a) -x4(mitu + a) = RND * 4 - 2 -y4(mitu + a) = RND * 4 - 2 -NEXT a -mitu = mitu * 2 + FOR b = 1 TO 50 + FOR a = 1 TO mitu + show a + NEXT a + NEXT b + + ' Duplicate existing branches and add randomness + FOR a = 1 TO mitu + x(mitu + a) = x(a) + y(mitu + a) = y(a) + s(mitu + a) = s(a) + z(mitu + a) = z(a) + x4(mitu + a) = RND * 4 - 2 + y4(mitu + a) = RND * 4 - 2 + NEXT a + mitu = mitu * 2 NEXT tr + +' Wait for user input before exiting a$ = INPUT$(1) SYSTEM SUB setpal -FOR a = 0 TO 16 -' OUT &H3C7, a - OUT &H3C8, a - OUT &H3C9, a * 4 - OUT &H3C9, a * 4 - OUT &H3C9, a * 4 -NEXT + ' Set the palette colors + FOR a = 0 TO 16 + OUT &H3C8, a + OUT &H3C9, a * 4 + OUT &H3C9, a * 4 + OUT &H3C9, a * 4 + NEXT END SUB -SUB show (d) - -x1 = x(d) -y1 = y(d) -s1 = s(d) -z1 = z(d) +SUB show(d) + ' Retrieve current branch properties + x1 = x(d) + y1 = y(d) + s1 = s(d) + z1 = z(d) + + ' Calculate color based on angle + c = SIN(z1) * 7 + 9 + ' Draw and fill the circle representing the branch + CIRCLE (x1, y1), s1 / 100, c + PAINT (x1, y1), c -c = SIN(z1) * 7 + 9 -CIRCLE (x1, y1), s1 / 100, c -PAINT (x1, y1), c -x(d) = x(d) + (SIN(z1) * 1000) / (s1 + 15) -y(d) = y(d) + (COS(z1) * 1000) / (s1 + 15) + ' Update position based on current angle and size + x(d) = x(d) + (SIN(z1) * 1000) / (s1 + 15) + y(d) = y(d) + (COS(z1) * 1000) / (s1 + 15) -s(d) = s(d) / 1.01 -IF x4(d) >= 0 THEN z(d) = z(d) + .1 ELSE z(d) = z(d) - .1 -x(d) = x(d) + x4(d) -y(d) = y(d) + y4(d) + ' Decay the size slightly + s(d) = s(d) / 1.01 + + ' Update angle based on direction + IF x4(d) >= 0 THEN z(d) = z(d) + .1 ELSE z(d) = z(d) - .1 + + ' Move branch in its random direction + x(d) = x(d) + x4(d) + y(d) = y(d) + y4(d) END SUB SUB showpal -FOR a = 1 TO 16 -LINE (a * 10, 1)-(a * 10 + 10, 100), a, BF -NEXT a + ' Display the color palette at the top of the screen + FOR a = 1 TO 16 + LINE (a * 10, 1) - (a * 10 + 10, 100), a, BF + NEXT a END SUB SUB start - -SCREEN 12 -setpal -REM showpal -RANDOMIZE TIMER + ' Initialize graphics mode and set palette + SCREEN 12 + setpal + REM showpal + RANDOMIZE TIMER END SUB - diff --git a/Simulation/interf.BAS b/Simulation/interf.BAS index eea01d8..563e27a 100755 --- a/Simulation/interf.BAS +++ b/Simulation/interf.BAS @@ -1,65 +1,70 @@ ' Program simulates interference between two slightly different frequencies - -DECLARE SUB getfreq () -DECLARE SUB start () -DECLARE FUNCTION gety! (t!) +' By Svjatoslav Agejenko. +' Email: svjatoslav@svjatoslav.eu +' Homepage: http://www.svjatoslav.eu +' +' Changelog: +' ?, Initial version +' 2024.08, Improved program readability using AI + +DECLARE SUB GetFrequency () +DECLARE SUB Start () +DECLARE FUNCTION GetY! (t!) DIM SHARED freq(1 TO 100) DIM SHARED phase(1 TO 100) -start +Start 1 -frm = frm + 1 +frame = frame + 1 PSET (0, 0) FOR x = 0 TO 639 - - oy1 = y1 - oy2 = y2 - oy3 = y3 - y1 = gety(frm + x / 4) * 20 + 150 - y2 = gety(frm + x / 4 + (x / 50)) * 20 + 150 - y3 = y1 + y2 + oldY1 = y1 + oldY2 = y2 + oldY3 = y3 + + y1 = GetY(frame + x / 4) * 20 + 150 + y2 = GetY(frame + x / 4 + (x / 50)) * 20 + 150 + y3 = y1 + y2 - ' LINE (x + 1, 0)-(x + 1, 479), 12 - LINE (x, 0)-(x, 479), 0 + ' LINE (x + 1, 0)-(x + 1, 479), 12 + LINE (x, 0)-(x, 479), 0 - LINE (x - 1, oy1)-(x, y1), 1 - LINE (x - 1, oy2)-(x, y2), 2 - LINE (x - 1, oy3)-(x, y3), 15 + LINE (x - 1, oldY1)-(x, y1), 1 + LINE (x - 1, oldY2)-(x, y2), 2 + LINE (x - 1, oldY3)-(x, y3), 15 NEXT x GOTO 1 -SUB diplay +SUB Display END SUB -SUB getfreq +SUB GetFrequency FOR a = 1 TO 100 - freq(a) = RND / 2 + 1 + freq(a) = RND / 2 + 1 NEXT a FOR a = 1 TO 100 - phase(a) = RND * 100 + phase(a) = RND * 100 NEXT a - END SUB -FUNCTION gety (t) +FUNCTION GetY (t) y = 0 FOR a = 1 TO 1 - y = y + SIN(t * freq(a) + phase(a)) + y = y + SIN(t * freq(a) + phase(a)) NEXT a -gety = y +GetY = y END FUNCTION -SUB start +SUB Start SCREEN 12 -getfreq +GetFrequency END SUB - diff --git a/Simulation/interf2.BAS b/Simulation/interf2.BAS index 8f223a8..e1150bf 100755 --- a/Simulation/interf2.BAS +++ b/Simulation/interf2.BAS @@ -1,52 +1,60 @@ ' Program simulates lot of frequencies interfering with itself. ' As a result, interferogram is produced. +' +' By Svjatoslav Agejenko. +' Email: svjatoslav@svjatoslav.eu +' Homepage: http://www.svjatoslav.eu +' +' Changelog: +' ?, Initial version +' 2024.08, Improved program readability using AI -DIM SHARED freq(1 TO 100) -DIM SHARED phase(1 TO 100) + +DIM SHARED frequency(1 TO 100) +DIM SHARED phaseAngle(1 TO 100) SCREEN 12 PSET (0, 0) -FOR x = 0 TO 639 - - fs = (x - 320) / 5000 + 1 -' fs = (x - 320) / 50 + 1 +FOR xPos = 0 TO 639 - y = 0 - FOR t = 1 TO 5000 STEP 5 - y1 = SIN(t) - y2 = SIN(t * fs) - y = y + ABS(y1 + y2) - NEXT t + fs = (xPos - 320) / 5000 + 1 + ' fs = (xPos - 320) / 50 + 1 - y = y / 5 - IF y > 470 THEN y = 470 - IF y < 0 THEN y = 0 + yVal = 0 + FOR timeStep = 1 TO 5000 STEP 5 + y1 = SIN(timeStep) + y2 = SIN(timeStep * fs) + yVal = yVal + ABS(y1 + y2) + NEXT timeStep - LINE -(x, 479 - y), 15 + yVal = yVal / 5 + IF yVal > 470 THEN yVal = 470 + IF yVal < 0 THEN yVal = 0 -NEXT x + LINE -(xPos, 479 - yVal), 15 -SUB getfreq +NEXT xPos -FOR a = 1 TO 100 - freq(a) = RND / 7 + 1 -NEXT a +SUB getFrequencies -FOR a = 1 TO 100 - phase(a) = RND * 100 -NEXT a + ' Generates random frequencies and phase angles + FOR index = 1 TO 100 + frequency(index) = RND / 7 + 1 + NEXT index + FOR index = 1 TO 100 + phaseAngle(index) = RND * 100 + NEXT index END SUB -SUB gety (t) - -y = 0 -FOR a = 1 TO 100 - y = y + SIN(t * freq(a) + phase(a)) -NEXT a +SUB getYVal (timeStep) + yVal = 0 + ' Sum of sinusoids with different frequencies and phases + FOR index = 1 TO 100 + yVal = yVal + SIN(timeStep * frequency(index) + phaseAngle(index)) + NEXT index END SUB - diff --git a/Simulation/liquid.bas b/Simulation/liquid.bas index 1b60bcd..53d280e 100755 --- a/Simulation/liquid.bas +++ b/Simulation/liquid.bas @@ -1,52 +1,76 @@ ' Program simulates water spill and subsequent surface tension that ' tries to round out the sharp edges. ' -' made by Svjatoslav Agejenko -' in 2003.12 -' homepage: svjatoslav.eu -' email: svjatoslav@svjatoslav.eu - +' By Svjatoslav Agejenko. +' Email: svjatoslav@svjatoslav.eu +' Homepage: http://www.svjatoslav.eu +' +' Changelog: +' 2003.12, Initial version +' 2024.08, Improved program readability using AI + + DEFINT A-Z WIDTH 80, 50 VIEW PRINT 1 TO 50 RANDOMIZE TIMER CLS -DIM SHARED buf1(1 TO 80, 1 TO 50) -DIM SHARED buf2(1 TO 80, 1 TO 50) +DIM SHARED buffer1(1 TO 80, 1 TO 50) +DIM SHARED buffer2(1 TO 80, 1 TO 50) +' Initialize the buffers with random values (0 or 1) FOR y = 1 TO 50 FOR x = 1 TO 80 - buf1(x, y) = INT(RND * 2) + buffer1(x, y) = INT(RND * 2) NEXT x NEXT y +' Main simulation loop 1 FOR y = 2 TO 49 FOR x = 2 TO 79 - c = buf1(x - 1, y - 1) - c = c + buf1(x, y - 1) - c = c + buf1(x + 1, y - 1) - c = c + buf1(x - 1, y) - c = c + buf1(x + 1, y) - c = c + buf1(x - 1, y + 1) - c = c + buf1(x, y + 1) - c = c + buf1(x + 1, y + 1) - IF buf1(x, y) = 1 THEN - IF c > 3 THEN buf2(x, y) = 1 ELSE buf2(x, y) = 0 + ' Calculate the sum of neighboring cells + cellSum = buffer1(x - 1, y - 1) + cellSum = cellSum + buffer1(x, y - 1) + cellSum = cellSum + buffer1(x + 1, y - 1) + cellSum = cellSum + buffer1(x - 1, y) + cellSum = cellSum + buffer1(x + 1, y) + cellSum = cellSum + buffer1(x - 1, y + 1) + cellSum = cellSum + buffer1(x, y + 1) + cellSum = cellSum + buffer1(x + 1, y + 1) + + ' Apply the surface tension rules + IF buffer1(x, y) = 1 THEN + IF cellSum > 3 THEN + buffer2(x, y) = 1 + ELSE + buffer2(x, y) = 0 + END IF ELSE - IF c > 4 THEN buf2(x, y) = 1 ELSE buf2(x, y) = 0 + IF cellSum > 4 THEN + buffer2(x, y) = 1 + ELSE + buffer2(x, y) = 0 + END IF END IF NEXT x NEXT y +' Update the display and swap buffers FOR y = 1 TO 50 FOR x = 1 TO 80 - b = buf2(x, y) - buf1(x, y) = b + cellValue = buffer2(x, y) + buffer1(x, y) = cellValue + + ' Print the cell value on screen LOCATE y, x - IF b = 0 THEN PRINT "."; ELSE PRINT "#" + IF cellValue = 0 THEN + PRINT "."; + ELSE + PRINT "#" + END IF NEXT x NEXT y + SOUND 0, 3 GOTO 1 - -- 2.20.1