From: Svjatoslav Agejenko Date: Sat, 26 Oct 2024 07:10:18 +0000 (+0300) Subject: Using AI to improve code readability X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=commitdiff_plain;h=c8ae604dd729c765eccd3c1b357ac4ea8eb0c4f7;p=qbasicapps.git Using AI to improve code readability --- diff --git a/Graphics/Animations/matrix.bas b/Graphics/Animations/matrix.bas index c44486c..565bbf1 100755 --- a/Graphics/Animations/matrix.bas +++ b/Graphics/Animations/matrix.bas @@ -1,5 +1,13 @@ -' Svjatoslav Agejenko -' year: 2002 +' Program renders animation/screensaver. +' It is inspired by The Matrix movie. +' +' By Svjatoslav Agejenko. +' Email: svjatoslav@svjatoslav.eu +' Homepage: http://www.svjatoslav.eu +' +' Changelog: +' 2002, Initial version +' 2024, Improved program readability using AI DECLARE SUB adus (a%) DECLARE SUB pt (x%, y%) @@ -24,297 +32,458 @@ usm = 8 start +' Main loop of the program 2 FOR a = 1 TO usm -IF uso(a) = 0 THEN adus a -addn usx(a), usy(a) -usy(a) = usy(a) + 1 -IF usy(a) > 13 THEN usy(a) = 0 -uso(a) = uso(a) - 1 -NEXT a + ' Check if the usage count is zero + IF uso(a) = 0 THEN adus a + + ' Add new position to the current object + addn usx(a), usy(a) + ' Update the y-coordinate of the current object + usy(a) = usy(a) + 1 + ' Check if the y-coordinate exceeds the screen height + IF usy(a) > 13 THEN usy(a) = 0 + + ' Decrease the usage count of the current object + uso(a) = uso(a) - 1 +NEXT a + +' Update the screen FOR y = 0 TO 13 -FOR x = 0 TO 18 -a = pat(x, y) -a = a + 1 -IF a = 2 THEN pah(x, y) = 2: pt x, y -IF a = 5 THEN pah(x, y) = 3: pt x, y -IF a = 30 THEN pag(x, y) = 0: pt x, y -pat(x, y) = a -NEXT x + FOR x = 0 TO 18 + ' Get the current pixel value + a = pat(x, y) + + ' Increment the pixel value + a = a + 1 + + ' Check if the pixel value is equal to 2 + IF a = 2 THEN + pah(x, y) = 2 + pt x, y + + ' Check if the pixel value is equal to 5 + ELSEIF a = 5 THEN + pah(x, y) = 3 + pt x, y + + ' Check if the pixel value is equal to 30 + ELSEIF a = 30 THEN + pag(x, y) = 0 + pt x, y + END IF + + ' Update the pixel value + pat(x, y) = a + NEXT x NEXT y +' Wait for a short period of time 3 IF ABS(tmr - TIMER) < .1 THEN GOTO 3 + +' Update the timer tmr = TIMER + +' Check if any key is pressed IF INKEY$ <> "" THEN SYSTEM + +' Go back to the main loop GOTO 2 SUB addn (x, y) -pag(x, y) = RND * 8 + 1 -pah(x, y) = 1 -pat(x, y) = 0 -pt x, y + ' Set a random font for the new pixel + pag(x, y) = RND * 8 + 1 + + ' Set the palette index for the new pixel + pah(x, y) = 1 + ' Initialize the pixel value + pat(x, y) = 0 + ' Draw the new pixel on the screen + pt x, y END SUB SUB adus (a) -usx(a) = RND * 18 -usy(a) = RND * 13 -uso(a) = RND * 5 + 3 + ' Set random initial positions for the object + usx(a) = RND * 18 + usy(a) = RND * 13 + + ' Set a random usage count for the object + uso(a) = RND * 5 + 3 END SUB SUB getfnt -FOR a = 1 TO 9 -LOCATE 1, 1 -PRINT "Loading:" + STR$(a * 10) + "%" -LINE (49, 49)-(83, 83), 0, BF -putsym 50, 50, a -smo 50, 50, 82, 82, 1 -GET (50, 50)-(82, 82), font1(1, a) - -LINE (49, 49)-(83, 83), 0, BF -putsym 50, 50, a -smo 50, 50, 82, 82, 2 -GET (50, 50)-(82, 82), font2(1, a) - -LINE (49, 49)-(83, 83), 0, BF -putsym 50, 50, a -smo 50, 50, 82, 82, 3 -GET (50, 50)-(82, 82), font3(1, a) + ' Load fonts from the screen + FOR a = 1 TO 9 + LOCATE 1, 1 -NEXT a -CLS -END SUB + ' Print the loading progress + PRINT "Loading: " + STR$(a * 10) + "%" -SUB pt (x, y) -x1 = x * 32 + 12 -y1 = y * 32 + 15 - -a = pag(x, y) -b = pah(x, y) - -IF a = 0 THEN -LINE (x1, y1)-(x1 + 32, y1 + 32), 0, BF -ELSE -SELECT CASE b -CASE 1 -PUT (x1, y1), font1(1, a), PSET -CASE 2 -PUT (x1, y1), font2(1, a), PSET -CASE 3 -PUT (x1, y1), font3(1, a), PSET -END SELECT -END IF + ' Draw a square on the screen + LINE (49, 49)-(83, 83), 0, BF -END SUB + ' Draw a symbol on the screen + putsym 50, 50, a -SUB putsym (x, y, a) -SELECT CASE a -CASE 1 -LINE (x + 10, y + 5)-(x + 10, y + 20), 14 -LINE (x + 5, y + 15)-(x + 20, y + 15), 14 -LINE (x + 15, y + 25)-(x + 20, y + 25), 14 -LINE (x + 20, y + 25)-(x + 25, y + 20), 14 -LINE (x + 25, y + 20)-(x + 25, y + 5), 14 -CASE 2 -LINE (x + 5, y + 15)-(x + 25, y + 10), 14 -LINE (x + 15, y + 5)-(x + 10, y + 25), 14 -LINE (x + 25, y + 5)-(x + 20, y + 20), 14 -LINE (x + 20, y + 30)-(x + 30, y + 20), 14 -CASE 3 -LINE (x + 5, y + 5)-(x + 5, y + 25), 14 -LINE (x + 5, y + 5)-(x + 25, y + 25), 14 -LINE (x + 5, y + 25)-(x + 25, y + 25), 14 -LINE (x + 10, y + 10)-(x + 25, y + 5), 14 -CASE 4 -LINE (x + 10, y + 5)-(x + 20, y + 5), 14 -LINE (x + 20, y + 5)-(x + 25, y + 10), 14 -LINE (x + 25, y + 20)-(x + 20, y + 25), 14 -LINE (x + 20, y + 25)-(x + 10, y + 25), 14 -LINE (x + 10, y + 25)-(x + 10, y + 5), 14 -LINE (x + 5, y + 15)-(x + 20, y + 15), 14 -CASE 5 -LINE (x + 5, y + 5)-(x + 10, y + 10), 14 -LINE (x + 10, y + 10)-(x + 10, y + 25), 14 -LINE (x + 10, y + 25)-(x + 5, y + 30), 14 -LINE (x + 10, y + 25)-(x + 15, y + 30), 14 -LINE (x + 15, y + 30)-(x + 25, y + 30), 14 -LINE (x + 10, y + 20)-(x + 25, y + 20), 14 -CASE 6 -LINE (x + 5, y + 5)-(x + 10, y + 5), 14 -LINE (x + 5, y + 5)-(x + 5, y + 10), 14 -LINE (x + 10, y + 10)-(x + 10, y + 15), 14 -LINE (x + 10, y + 15)-(x + 20, y + 30), 14 -LINE (x + 20, y + 30)-(x + 25, y + 30), 14 -LINE (x + 5, y + 30)-(x + 10, y + 30), 14 -LINE (x + 25, y + 15)-(x + 10, y + 30), 14 -CASE 7 -LINE (x + 5, y + 15)-(x + 10, y + 15), 14 -LINE (x + 10, y + 15)-(x + 25, y + 5), 14 -LINE (x + 5, y + 25)-(x + 10, y + 25), 14 -LINE (x + 10, y + 25)-(x + 15, y + 5), 14 -LINE (x + 20, y + 5)-(x + 20, y + 20), 14 -PSET (x + 15, y + 25), 14 -PSET (x + 22, y + 25), 14 -CASE 8 -'line (x+,y+)-(x+,y+),15 -LINE (x + 15, y + 10)-(x + 15, y + 25), 14 -LINE (x + 20, y + 15)-(x + 20, y + 25), 14 -LINE (x + 5, y + 20)-(x + 10, y + 25), 14 -LINE (x + 10, y + 25)-(x + 25, y + 25), 14 -CASE 9 -LINE (x + 5, y + 5)-(x + 25, y + 5), 14 -LINE (x + 15, y + 5)-(x + 5, y + 20), 14 -LINE (x + 15, y + 5)-(x + 25, y + 20), 14 -LINE (x + 15, y + 5)-(x + 15, y + 25), 14 -LINE (x + 5, y + 30)-(x + 20, y + 20), 14 - -END SELECT + ' Smooth the symbol + smo 50, 50, 82, 82, 1 + + ' Get the smoothed symbol from the screen + GET (50, 50)-(82, 82), font1(1, a) + + ' Draw another square on the screen + LINE (49, 49)-(83, 83), 0, BF + + ' Draw another symbol on the screen + putsym 50, 50, a + + ' Smooth the other symbol + smo 50, 50, 82, 82, 2 + + ' Get the smoothed symbol from the screen + GET (50, 50)-(82, 82), font2(1, a) + + ' Draw yet another square on the screen + LINE (49, 49)-(83, 83), 0, BF + ' Draw yet another symbol on the screen + putsym 50, 50, a + ' Smooth the last symbol + smo 50, 50, 82, 82, 3 + ' Get the smoothed symbol from the screen + GET (50, 50)-(82, 82), font3(1, a) + NEXT a + ' Clear the screen + CLS +END SUB + +SUB pt (x, y) + ' Calculate the screen coordinates for the pixel + x1 = x * 32 + 12 + y1 = y * 32 + 15 + + ' Get the current font and palette index + a = pag(x, y) + b = pah(x, y) + + ' Check if the pixel is empty + IF a = 0 THEN + ' Draw an empty square on the screen + LINE (x1, y1)-(x1 + 32, y1 + 32), 0, BF + ELSE + ' Select the appropriate font based on the palette index + SELECT CASE b + CASE 1 + ' Draw the pixel using the first font + PUT (x1, y1), font1(1, a), PSET + CASE 2 + ' Draw the pixel using the second font + PUT (x1, y1), font2(1, a), PSET + CASE 3 + ' Draw the pixel using the third font + PUT (x1, y1), font3(1, a), PSET + END SELECT + END IF +END SUB + +SUB putsym (x, y, a) + ' Select the appropriate symbol based on the input value + SELECT CASE a + CASE 1 + ' Draw the first symbol + LINE (x + 10, y + 5)-(x + 10, y + 20), 14 + LINE (x + 5, y + 15)-(x + 20, y + 15), 14 + LINE (x + 15, y + 25)-(x + 20, y + 25), 14 + LINE (x + 20, y + 25)-(x + 25, y + 20), 14 + LINE (x + 25, y + 20)-(x + 25, y + 5), 14 + CASE 2 + ' Draw the second symbol + LINE (x + 5, y + 15)-(x + 25, y + 10), 14 + LINE (x + 15, y + 5)-(x + 10, y + 25), 14 + LINE (x + 25, y + 5)-(x + 20, y + 20), 14 + LINE (x + 20, y + 30)-(x + 30, y + 20), 14 + CASE 3 + ' Draw the third symbol + LINE (x + 5, y + 5)-(x + 5, y + 25), 14 + LINE (x + 5, y + 5)-(x + 25, y + 25), 14 + LINE (x + 5, y + 25)-(x + 25, y + 25), 14 + LINE (x + 10, y + 10)-(x + 25, y + 5), 14 + CASE 4 + ' Draw the fourth symbol + LINE (x + 10, y + 5)-(x + 20, y + 5), 14 + LINE (x + 20, y + 5)-(x + 25, y + 10), 14 + LINE (x + 25, y + 20)-(x + 20, y + 25), 14 + LINE (x + 20, y + 25)-(x + 10, y + 25), 14 + LINE (x + 10, y + 25)-(x + 10, y + 5), 14 + LINE (x + 5, y + 15)-(x + 20, y + 15), 14 + CASE 5 + ' Draw the fifth symbol + LINE (x + 5, y + 5)-(x + 10, y + 10), 14 + LINE (x + 10, y + 10)-(x + 10, y + 25), 14 + LINE (x + 10, y + 25)-(x + 5, y + 30), 14 + LINE (x + 10, y + 25)-(x + 15, y + 30), 14 + LINE (x + 15, y + 30)-(x + 25, y + 30), 14 + LINE (x + 10, y + 20)-(x + 25, y + 20), 14 + CASE 6 + ' Draw the sixth symbol + LINE (x + 5, y + 5)-(x + 10, y + 5), 14 + LINE (x + 5, y + 5)-(x + 5, y + 10), 14 + LINE (x + 10, y + 10)-(x + 10, y + 15), 14 + LINE (x + 10, y + 15)-(x + 20, y + 30), 14 + LINE (x + 20, y + 30)-(x + 25, y + 30), 14 + LINE (x + 5, y + 30)-(x + 10, y + 30), 14 + LINE (x + 25, y + 15)-(x + 10, y + 30), 14 + CASE 7 + ' Draw the seventh symbol + LINE (x + 5, y + 15)-(x + 10, y + 15), 14 + LINE (x + 10, y + 15)-(x + 25, y + 5), 14 + LINE (x + 5, y + 25)-(x + 10, y + 25), 14 + LINE (x + 10, y + 25)-(x + 15, y + 5), 14 + LINE (x + 20, y + 5)-(x + 20, y + 20), 14 + PSET (x + 15, y + 25), 14 + PSET (x + 22, y + 25), 14 + CASE 8 + ' Draw the eighth symbol + LINE (x + 15, y + 10)-(x + 15, y + 25), 14 + LINE (x + 20, y + 15)-(x + 20, y + 25), 14 + LINE (x + 5, y + 20)-(x + 10, y + 25), 14 + LINE (x + 10, y + 25)-(x + 25, y + 25), 14 + CASE 9 + ' Draw the ninth symbol + LINE (x + 5, y + 5)-(x + 25, y + 5), 14 + LINE (x + 15, y + 5)-(x + 5, y + 20), 14 + LINE (x + 15, y + 5)-(x + 25, y + 20), 14 + LINE (x + 15, y + 5)-(x + 15, y + 25), 14 + LINE (x + 5, y + 30)-(x + 20, y + 20), 14 + END SELECT END SUB SUB setpal (b) -SELECT CASE b -CASE 2 -FOR a = 0 TO 14 -OUT &H3C8, a -OUT &H3C9, a * 2 -OUT &H3C9, a * 4.5 -OUT &H3C9, a * 3 -NEXT a -CASE 1 -FOR a = 0 TO 14 -OUT &H3C8, a -OUT &H3C9, 0 -OUT &H3C9, 0 -OUT &H3C9, 0 -NEXT a -OUT &H3C8, 15 -OUT &H3C9, 20 -OUT &H3C9, 63 -OUT &H3C9, 63 -END SELECT + ' Set the color palette based on the input value + SELECT CASE b + CASE 2 + ' Set the colors for palette 2 + FOR a = 0 TO 14 + OUT &H3C8, a + OUT &H3C9, a * 2 + OUT &H3C9, a * 4.5 + OUT &H3C9, a * 3 + NEXT a + CASE 1 + ' Set the colors for palette 1 + FOR a = 0 TO 14 + OUT &H3C8, a + OUT &H3C9, 0 + OUT &H3C9, 0 + OUT &H3C9, 0 + NEXT a + ' Set the color for the background + OUT &H3C8, 15 + OUT &H3C9, 20 + OUT &H3C9, 63 + OUT &H3C9, 63 + END SELECT END SUB SUB smo (x1, y1, x2, y2, r) + ' Initialize the smoothing variable + c1 = 0 -c1 = 0 + ' Perform horizontal smoothing + FOR y = y1 TO y2 + FOR x = x1 TO x2 + ' Get the current pixel value + c = POINT(x, y) -FOR y = y1 TO y2 -FOR x = x1 TO x2 -c = POINT(x, y) -c1 = c1 - 5 -IF c1 < 0 THEN c1 = 0 -IF c > c1 THEN c1 = c -PSET (x, y), c1 -NEXT x -NEXT y + ' Update the smoothing variable + c1 = c1 - 5 -FOR x = x1 TO x2 -c1 = 0 -FOR y = y1 TO y2 -c = POINT(x, y) -c1 = c1 - 5 -IF c1 < 0 THEN c1 = 0 -IF c > c1 THEN c1 = c -PSET (x, y), c1 -NEXT y -NEXT x - - -FOR y = y1 TO y2 -c1 = 0 -FOR x = x2 TO x1 STEP -1 -c = POINT(x, y) -c1 = c1 - 5 -IF c1 < 0 THEN c1 = 0 -IF c > c1 THEN c1 = c -PSET (x, y), c1 -NEXT x -NEXT y + ' Ensure the smoothing variable is non-negative + IF c1 < 0 THEN c1 = 0 + ' Update the smoothing variable if the current pixel value is greater + IF c > c1 THEN c1 = c + + ' Set the smoothed pixel value + PSET (x, y), c1 + NEXT x + NEXT y -FOR x = x1 TO x2 -c1 = 0 -FOR y = y2 TO y1 STEP -1 -c = POINT(x, y) -c1 = c1 - 5 -IF c1 < 0 THEN c1 = 0 -IF c > c1 THEN c1 = c -PSET (x, y), c1 -NEXT y -NEXT x + ' Perform vertical smoothing + FOR x = x1 TO x2 + c1 = 0 + FOR y = y1 TO y2 + ' Get the current pixel value + c = POINT(x, y) + ' Update the smoothing variable + c1 = c1 - 5 + ' Ensure the smoothing variable is non-negative + IF c1 < 0 THEN c1 = 0 + + ' Update the smoothing variable if the current pixel value is greater + IF c > c1 THEN c1 = c + + ' Set the smoothed pixel value + PSET (x, y), c1 + NEXT y + NEXT x + + ' Perform diagonal smoothing + FOR y = y1 TO y2 + c1 = 0 + FOR x = x1 TO x2 + ' Get the current pixel value + c = POINT(x, y) + ' Update the smoothing variable + c1 = c1 - 5 -IF r = 1 THEN GOTO 1 -rr = r + 1 + ' Ensure the smoothing variable is non-negative + IF c1 < 0 THEN c1 = 0 + + ' Update the smoothing variable if the current pixel value is greater + IF c > c1 THEN c1 = c + + ' Set the smoothed pixel value + PSET (x, y), c1 + NEXT x + NEXT y + + ' Perform diagonal smoothing in the opposite direction + FOR x = x1 TO x2 + c1 = 0 + FOR y = y2 TO y1 STEP -1 + ' Get the current pixel value + c = POINT(x, y) + + ' Update the smoothing variable + c1 = c1 - 5 + ' Ensure the smoothing variable is non-negative + IF c1 < 0 THEN c1 = 0 -FOR y = y1 TO y2 -c1 = 0 -FOR x = x1 TO x2 -c = POINT(x, y) -c1 = (c1 * r + c) / rr -c2 = c1 - r -IF c2 < 0 THEN c2 = 0 -PSET (x, y), c2 -NEXT x -NEXT y + ' Update the smoothing variable if the current pixel value is greater + IF c > c1 THEN c1 = c -FOR x = x1 TO x2 -c1 = 0 -FOR y = y1 TO y2 -c = POINT(x, y) -c1 = c1 - 5 -IF c1 < 0 THEN c1 = 0 -IF c > c1 THEN c1 = c -PSET (x, y), c1 -NEXT y -NEXT x - - -FOR y = y1 TO y2 -c1 = 0 -FOR x = x2 TO x1 STEP -1 -c = POINT(x, y) -c1 = c1 - 5 -IF c1 < 0 THEN c1 = 0 -IF c > c1 THEN c1 = c -PSET (x, y), c1 -NEXT x -NEXT y + ' Set the smoothed pixel value + PSET (x, y), c1 + NEXT y + NEXT x + ' Check if the recursion depth is equal to 1 + IF r = 1 THEN GOTO 1 -FOR x = x1 TO x2 -c1 = 0 -FOR y = y2 TO y1 STEP -1 -c = POINT(x, y) -c1 = c1 - 5 -IF c1 < 0 THEN c1 = 0 -IF c > c1 THEN c1 = c -PSET (x, y), c1 -NEXT y -NEXT x + ' Increment the recursion depth + rr = r + 1 -1 + ' Perform horizontal smoothing with averaging + FOR y = y1 TO y2 + c1 = 0 + FOR x = x1 TO x2 + ' Get the current pixel value + c = POINT(x, y) + + ' Update the smoothed pixel value using averaging + c1 = (c1 * r + c) / rr + + ' Ensure the smoothed pixel value is non-negative + c2 = c1 - r + + IF c2 < 0 THEN c2 = 0 + + ' Set the final smoothed pixel value + PSET (x, y), c2 + NEXT x + NEXT y + ' Perform vertical smoothing with averaging + FOR x = x1 TO x2 + c1 = 0 + FOR y = y1 TO y2 + ' Get the current pixel value + c = POINT(x, y) + + ' Update the smoothing variable + c1 = c1 - 5 + ' Ensure the smoothing variable is non-negative + IF c1 < 0 THEN c1 = 0 + ' Update the smoothing variable if the current pixel value is greater + IF c > c1 THEN c1 = c + + ' Set the smoothed pixel value + PSET (x, y), c1 + NEXT y + NEXT x + + ' Perform diagonal smoothing with averaging + FOR y = y1 TO y2 + c1 = 0 + FOR x = x2 TO x1 STEP -1 + ' Get the current pixel value + c = POINT(x, y) + + ' Update the smoothing variable + c1 = c1 - 5 + + ' Ensure the smoothing variable is non-negative + IF c1 < 0 THEN c1 = 0 + + ' Update the smoothing variable if the current pixel value is greater + IF c > c1 THEN c1 = c + + ' Set the smoothed pixel value + PSET (x, y), c1 + NEXT x + NEXT y + + ' Perform diagonal smoothing in the opposite direction with averaging + FOR x = x1 TO x2 + c1 = 0 + FOR y = y2 TO y1 STEP -1 + ' Get the current pixel value + c = POINT(x, y) + + ' Update the smoothing variable + c1 = c1 - 5 + + ' Ensure the smoothing variable is non-negative + IF c1 < 0 THEN c1 = 0 + + ' Update the smoothing variable if the current pixel value is greater + IF c > c1 THEN c1 = c + + ' Set the smoothed pixel value + PSET (x, y), c1 + NEXT y + NEXT x + +1 END SUB SUB start -SCREEN 12 -setpal 1 -getfnt -setpal 2 + ' Set the screen mode to 12 + SCREEN 12 + + ' Set the color palette to 1 + setpal 1 + + ' Load the fonts + getfnt + ' Set the color palette to 2 + setpal 2 END SUB