From: Svjatoslav Agejenko Date: Sat, 17 Aug 2024 13:00:40 +0000 (+0300) Subject: Using AI to improve code readability X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=commitdiff_plain;h=3dca85c6844c82217a3e053ee0aa689c2aa53134;p=qbasicapps.git Using AI to improve code readability --- diff --git a/Graphics/Texture generation/map1.bas b/Graphics/Texture generation/map1.bas index 3d40578..56ffcce 100755 --- a/Graphics/Texture generation/map1.bas +++ b/Graphics/Texture generation/map1.bas @@ -1,36 +1,45 @@ -' surface -' made by Svjatoslav Agejenko -' in 2003.12 -' H-Page: svjatoslav.eu -' E-Mail: svjatoslav@svjatoslav.eu - -DEFINT A-Z -SCREEN 13 -RANDOMIZE TIMER +' Yellow flame +' By Svjatoslav Agejenko. +' Email: svjatoslav@svjatoslav.eu +' Homepage: http://www.svjatoslav.eu +' +' Changelog: +' 2003.12, Initial version +' 2024.08, Improved program readability using AI -FOR a = 0 TO 255 -OUT &H3C8, a -OUT &H3C9, SIN(a / 21) * 30 + 30 -OUT &H3C9, SIN(a / 34) * 30 + 30 -OUT &H3C9, SIN(a / 10) * 30 + 30 -NEXT a +DEFINT A-Z ' Define all variables as integers +SCREEN 13 ' Set graphics mode to 320x200 with 256 colors +RANDOMIZE TIMER ' Seed the random number generator +' Initialize palette registers with sine wave colors +FOR paletteIndex = 0 TO 255 + OUT &H3C8, paletteIndex + OUT &H3C9, INT(SIN(paletteIndex / 21) * 30 + 30) + OUT &H3C9, INT(SIN(paletteIndex / 34) * 30 + 30) + OUT &H3C9, INT(SIN(paletteIndex / 10) * 30 + 30) +NEXT paletteIndex +' Generate the surface pattern FOR y = 1 TO 199 -FOR x = 1 TO 319 -c1 = POINT(x, y - 1) -c2 = POINT(x - 1, y) -c3 = POINT(x - 1, y - 1) -c4 = POINT(x - 2, y) -c = (c1 + c2 + c3 + c4) \ 4 + (RND * 5 - 2) -IF c < 0 THEN c = 0 -IF c > 63 THEN c = 63 -PSET (x, y), c -NEXT x -NEXT y + FOR x = 1 TO 319 + prevPixel = POINT(x, y - 1) + leftPixel = POINT(x - 1, y) + diagPixel = POINT(x - 1, y - 1) + left2Pixel = POINT(x - 2, y) + + ' Calculate the average of surrounding pixels and add some randomness + newColor = (prevPixel + leftPixel + diagPixel + left2Pixel) \ 4 + (RND * 5 - 2) -a$ = INPUT$(1) + ' Clamp the color value within the valid range + IF newColor < 0 THEN newColor = 0 + IF newColor > 63 THEN newColor = 63 + + ' Set the pixel with the calculated color + PSET (x, y), newColor + NEXT x +NEXT y -SYSTEM +' Wait for user input to exit +userInput$ = INPUT$(1) diff --git a/Graphics/Texture generation/map3.bas b/Graphics/Texture generation/map3.bas index c156f33..927409b 100755 --- a/Graphics/Texture generation/map3.bas +++ b/Graphics/Texture generation/map3.bas @@ -1,86 +1,94 @@ -' Cloud surface -' made by Svjatoslav Agejenko -' in 2003.12 -' H-Page: svjatoslav.eu -' E-Mail: svjatoslav@svjatoslav.eu - -DECLARE SUB box (x1%, y1%, s%) -DECLARE SUB setpal () -DECLARE SUB start () +DECLARE SUB DrawPixels (x1 AS INTEGER, y1 AS INTEGER, s AS INTEGER) +' Program to render cloud surface using diamond square algorithm. +' By Svjatoslav Agejenko. +' Email: svjatoslav@svjatoslav.eu +' Homepage: http://www.svjatoslav.eu +' +' Changelog: +' 2003.12, Initial version +' 2024.08, Improved program readability using AI + +DECLARE SUB DrawBox (x1 AS INTEGER, y1 AS INTEGER, s AS INTEGER) +DECLARE SUB SetPalette () +DECLARE SUB InitializeProgram () DEFINT A-Z -start +InitializeProgram -DIM SHARED lm -lm = 127 +DIM SHARED maxLightness AS INTEGER +maxLightness = 127 +DIM scale AS INTEGER +scale = 2 ^ 8 -s = 2 ^ 8 -1 -s = s \ 2 -x1 = (319 \ s) - 1 -y1 = (199 \ s) - 1 +1 : +scale = scale \ 2 +x1 = (319 \ scale) - 1 +y1 = (199 \ scale) - 1 FOR y = 0 TO y1 -FOR x = 0 TO x1 -box x * s, y * s, s -NEXT x + FOR x = 0 TO x1 + DrawPixels x * scale, y * scale, scale + NEXT x NEXT y -IF s > 2 THEN GOTO 1 -a$ = INPUT$(1) -SYSTEM - -SUB box (x1, y1, s) -c1 = POINT(x1, y1) -c2 = POINT(x1 + s, y1) -c3 = POINT(x1, y1 + s) -c4 = POINT(x1 + s, y1 + s) - -sp = s \ 2 -k = s * 2 -kp = k / 2 - -cc2 = ((c1 + c2) / 2) + (RND * k) - kp -IF cc2 > lm THEN cc2 = lm -IF cc2 < 0 THEN cc2 = 0 - -cc3 = ((c1 + c3) / 2) + (RND * k) - kp -IF cc3 > lm THEN cc3 = lm -IF cc3 < 0 THEN cc3 = 0 - -cc4 = ((c2 + c4) / 2) + (RND * k) - kp -IF cc4 > lm THEN cc4 = lm -IF cc4 < 0 THEN cc4 = 0 - -cc5 = ((c3 + c4) / 2) + (RND * k) - kp -IF cc5 > lm THEN cc5 = lm -IF cc5 < 0 THEN cc5 = 0 - -cc1 = ((cc2 + cc3 + cc4 + cc5) / 4) + (RND * k) - kp -IF cc1 > lm THEN cc1 = lm -IF cc1 < 0 THEN cc1 = 0 - - - -PSET (x1 + sp, y1 + sp), cc1 -PSET (x1 + sp, y1), cc2 -PSET (x1, y1 + sp), cc3 -PSET (x1 + s, y1 + sp), cc4 -PSET (x1 + sp, y1 + s), cc5 +IF scale > 2 THEN GOTO 1 +WAITa$ = INPUT$(1) + +SUB DrawPixels (x1 AS INTEGER, y1 AS INTEGER, s AS INTEGER) + ' Get the lightness values for the corners of the box + c1 = POINT(x1, y1) + c2 = POINT(x1 + s, y1) + c3 = POINT(x1, y1 + s) + c4 = POINT(x1 + s, y1 + s) + + ' Calculate the midpoint lightness values + sp = s \ 2 + k = s * 2 + kp = k / 2 + + cc2 = ((c1 + c2) / 2) + (RND * k) - kp + IF cc2 > maxLightness THEN cc2 = maxLightness + IF cc2 < 0 THEN cc2 = 0 + + cc3 = ((c1 + c3) / 2) + (RND * k) - kp + IF cc3 > maxLightness THEN cc3 = maxLightness + IF cc3 < 0 THEN cc3 = 0 + + cc4 = ((c2 + c4) / 2) + (RND * k) - kp + IF cc4 > maxLightness THEN cc4 = maxLightness + IF cc4 < 0 THEN cc4 = 0 + + cc5 = ((c3 + c4) / 2) + (RND * k) - kp + IF cc5 > maxLightness THEN cc5 = maxLightness + IF cc5 < 0 THEN cc5 = 0 + + ' Calculate the central lightness value + cc1 = ((cc2 + cc3 + cc4 + cc5) / 4) + (RND * k) - kp + IF cc1 > maxLightness THEN cc1 = maxLightness + IF cc1 < 0 THEN cc1 = 0 + + ' Set the calculated lightness values for the box + PSET (x1 + sp, y1 + sp), cc1 + PSET (x1 + sp, y1), cc2 + PSET (x1, y1 + sp), cc3 + PSET (x1 + s, y1 + sp), cc4 + PSET (x1 + sp, y1 + s), cc5 END SUB -SUB setpal -FOR a = 0 TO 255 -OUT &H3C8, a -OUT &H3C9, a / 4 -OUT &H3C9, a / 3 -OUT &H3C9, a / 2.3 -NEXT a +SUB InitializeProgram + ' Set the screen mode and initialize the color palette + SCREEN 13 + SetPalette + RANDOMIZE TIMER END SUB -SUB start -SCREEN 13 -setpal -RANDOMIZE TIMER +SUB SetPalette + ' Set the color palette for lightness levels + FOR a = 0 TO 255 + OUT &H3C8, a + OUT &H3C9, a / 4 + OUT &H3C9, a / 3 + OUT &H3C9, a / 2.3 + NEXT a END SUB diff --git a/Graphics/Texture generation/oldpaper.bas b/Graphics/Texture generation/oldpaper.bas index 72a92ab..8bdca0d 100755 --- a/Graphics/Texture generation/oldpaper.bas +++ b/Graphics/Texture generation/oldpaper.bas @@ -1,51 +1,84 @@ -' Old paper surface -' made by Svjatoslav Agejenko -' in 2003.12 -' H-Page: svjatoslav.eu -' E-Mail: svjatoslav@svjatoslav.eu - -DECLARE SUB paper (x1%, y1%, x2%, y2%) +' Program to render surface resembling old paper. +' By Svjatoslav Agejenko. +' Email: svjatoslav@svjatoslav.eu +' Homepage: http://www.svjatoslav.eu +' +' Changelog: +' 2003.12, Initial version +' 2024.08, Improved program readability using AI + +DECLARE SUB DrawPaper (x1 AS INTEGER, y1 AS INTEGER, x2 AS INTEGER, y2 AS INTEGER) + DEFINT A-Z SCREEN 12 RANDOMIZE TIMER -FOR a = 0 TO 15 -OUT &H3C8, a -OUT &H3C9, a * 3 -OUT &H3C9, a * 3 -OUT &H3C9, a * 2 -NEXT a +' Set color palette +FOR colorIndex = 0 TO 15 + OUT &H3C8, colorIndex + OUT &H3C9, colorIndex * 3 + OUT &H3C9, colorIndex * 3 + OUT &H3C9, colorIndex * 2 +NEXT colorIndex + +' Generate and draw paper surfaces continuously until a key is pressed 1 -x1 = RND * 600 + 20 -x2 = RND * 600 + 20 -y1 = RND * 400 + 40 -y2 = RND * 400 + 40 -IF x1 > x2 THEN SWAP x1, x2 -IF y1 > y2 THEN SWAP y1, y2 -paper x1, y1, x2, y2 + x1 = RND * 600 + 20 + x2 = RND * 600 + 20 + y1 = RND * 400 + 40 + y2 = RND * 400 + 40 + + ' Ensure x1 is less than x2 and y1 is less than y2 + IF x1 > x2 THEN SWAP x1, x2 + IF y1 > y2 THEN SWAP y1, y2 + + ' Draw the paper with the calculated coordinates + CALL DrawPaper(x1, y1, x2, y2) + + ' Continue drawing until any key is pressed + IF INKEY$ <> "" THEN SYSTEM GOTO 1 -a$ = INPUT$(1) - -SYSTEM - -SUB paper (x1, y1, x2, y2) -yl = y2 + 1 -z = 0 -LINE (x1, y1)-(x2, y1), 0 -LINE (x2, y1)-(x2, y2), 0 -FOR y = y1 + 1 TO y2 -c = 0 -FOR x = x1 TO x2 -p = p + 1 -IF p > 23 THEN z = RND * 1: p = 0 -c1 = POINT(x, y - 1) -c = (c1 + c) / 2 + (RND * (2 + (5 / y)) - (3 / (yl - y))) - z -IF c < 0 THEN c = 0 -IF c > 15 THEN c = 15 -PSET (x - 1, y), c -NEXT x -NEXT y +SUB DrawPaper (x1 AS INTEGER, y1 AS INTEGER, x2 AS INTEGER, y2 AS INTEGER) + DIM c AS INTEGER + DIM z AS SINGLE + DIM p AS INTEGER + DIM c1 AS INTEGER + DIM yl AS INTEGER + + ' Initialize variables + yl = y2 + 1 + z = 0 + + ' Draw the bottom and right borders of the paper + LINE (x1, y1)-(x2, y1), 0 + LINE (x2, y1)-(x2, y2), 0 + + ' Generate the texture of the paper + FOR y = y1 + 1 TO y2 + c = 0 + FOR x = x1 TO x2 + p = p + 1 + + ' Randomly reset the pattern counter + IF p > 23 THEN + z = RND * 1 + p = 0 + END IF + + ' Get the color of the previous row + c1 = POINT(x, y - 1) + + ' Calculate the average color with random variation + c = (c1 + c) / 2 + (RND * (2 + (5 / y))) - (3 / (yl - y)) - z + + ' Clamp the color value within the range [0, 15] + IF c < 0 THEN c = 0 + IF c > 15 THEN c = 15 + ' Set the pixel color for the current position + PSET (x - 1, y), c + NEXT x + NEXT y END SUB diff --git a/Graphics/Texture generation/test3.bas b/Graphics/Texture generation/test3.bas index da21913..c23659c 100755 --- a/Graphics/Texture generation/test3.bas +++ b/Graphics/Texture generation/test3.bas @@ -1,19 +1,27 @@ ' Strange surface -' made by Svjatoslav Agejenko -' in 2003.12 +' Made by Svjatoslav Agejenko in 2003.12 ' H-Page: svjatoslav.eu ' E-Mail: svjatoslav@svjatoslav.eu - + SCREEN 13 -FOR y = 1 TO 199 -FOR x = 1 TO 319 -c = SIN((x ^ 2 + y ^ 2) / 10) * 10 -IF c < 0 THEN c = 0 -IF c > 15 THEN c = 15 -PSET (x, y), c + 16 -NEXT x -NEXT y +' Initialize the screen mode to 320x200 with 16 colors + +' Outer loop for the vertical axis (y-coordinate) +FOR ycoordinate = 1 TO 199 + ' Inner loop for the horizontal axis (x-coordinate) + FOR xcoordinate = 1 TO 319 + ' Calculate the sine value based on the squared distances from the origin + colorvalue = SIN((xcoordinate ^ 2 + ycoordinate ^ 2) / 10) * 10 + ' Clamp the color value to the range [0, 15] + IF colorvalue < 0 THEN colorvalue = 0 + IF colorvalue > 15 THEN colorvalue = 15 + ' Set the pixel color at (xcoordinate, ycoordinate) with an offset to use the full 16-color palette + PSET (xcoordinate, ycoordinate), colorvalue + 16 + NEXT xcoordinate +NEXT ycoordinate +' Wait for a key press before exiting +WAIT 30, 1 \ No newline at end of file diff --git a/Graphics/Texture generation/wood.bas b/Graphics/Texture generation/wood.bas index 9d3efa3..2bf38d0 100755 --- a/Graphics/Texture generation/wood.bas +++ b/Graphics/Texture generation/wood.bas @@ -1,49 +1,73 @@ -' Wood surface -' made by Svjatoslav Agejenko -' in 2003.12 -' H-Page: svjatoslav.eu -' E-Mail: svjatoslav@svjatoslav.eu - -DECLARE SUB wood (x1%, y1%) -DECLARE SUB paper (x1%, y1%) +' Program to render surface resembling wood. +' By Svjatoslav Agejenko. +' Email: svjatoslav@svjatoslav.eu +' Homepage: http://www.svjatoslav.eu +' +' Changelog: +' 2003.12, Initial version +' 2024.08, Improved program readability using AI + +DECLARE SUB DrawWood(xPos%, yPos%) +DECLARE SUB DrawPaper(xPos%, y1Pos%) DEFINT A-Z SCREEN 12 RANDOMIZE TIMER -FOR a = 0 TO 15 -OUT &H3C8, a -OUT &H3C9, a * 4 -OUT &H3C9, a * 3 -OUT &H3C9, a * 0 -NEXT a - -1 -x1 = RND * 400 + 200 -y1 = RND * 100 + 200 -wood x1, y1 -GOTO 1 +' Set palette colors +FOR colorIndex = 0 TO 15 + OUT &H3C8, colorIndex + OUT &H3C9, colorIndex * 4 + OUT &H3C9, colorIndex * 3 + OUT &H3C9, colorIndex * 0 +NEXT colorIndex + +' Main loop to draw wood at random positions +100: +xPos = RND * 400 + 200 +yPos = RND * 100 + 200 +CALL DrawWood(xPos, yPos) +GOTO 100 + +' Wait for user input to exit a$ = INPUT$(1) -SYSTEM - -SUB wood (x1, y1) -yl = y1 + 1 -LINE (0, 0)-(x1, y1), 0, BF -LINE (5, 5)-(x1 - 5, y1 - 5), 8, BF -LINE (10, 10)-(x1 - 10, y1 - 10), 15, BF -pe = RND * 300 -FOR y = y1 - 1 TO 0 STEP -1 -FOR x = x1 - 1 TO 0 STEP -1 -p = p + 1 -IF p > x1 THEN z = RND * 13: p = SIN((y + pe) / 100) * x1 -c1 = POINT(x, y + 1) -c2 = POINT(x, y) -c = (c1 * 2 + c2 + c * 3 + z) / 7 + RND * 1 -IF c < 0 THEN c = 0 -IF c > 15 THEN c = 15 -PSET (x + 1, y), c -NEXT x -NEXT y - -END SUB +SUB DrawWood (xPos, yPos) + DIM yl AS INTEGER + DIM pe AS INTEGER + DIM p AS INTEGER + DIM z AS INTEGER + DIM c AS INTEGER + DIM c1 AS INTEGER + DIM c2 AS INTEGER + + ' Draw the outline of the wood + yl = yPos + 1 + LINE (0, 0)-(xPos, yPos), 0, BF + LINE (5, 5)-(xPos - 5, yl - 5), 8, BF + LINE (10, 10)-(xPos - 10, yl - 10), 15, BF + + ' Initialize random factor for color variation + pe = RND * 300 + + ' Draw the wood texture + FOR y = yPos - 1 TO 0 STEP -1 + FOR x = xPos - 1 TO 0 STEP -1 + p = p + 1 + IF p > xPos THEN + z = RND * 13 + p = SIN((y + pe) / 100) * xPos + END IF + c1 = POINT(x, y + 1) + c2 = POINT(x, y) + c = (c1 * 2 + c2 + c * 3 + z) / 7 + RND * 1 + + ' Ensure color value is within the valid range + IF c < 0 THEN c = 0 + IF c > 15 THEN c = 15 + + ' Set the pixel color for the wood texture + PSET (x + 1, y), c + NEXT x + NEXT y +END SUB \ No newline at end of file