From: Svjatoslav Agejenko Date: Sat, 17 Aug 2024 13:09:50 +0000 (+0300) Subject: Using AI to improve code readability X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=commitdiff_plain;h=d7163fe711523b93935036d594e3f556224cb8ed;p=qbasicapps.git Using AI to improve code readability --- diff --git a/Graphics/Spirals/spiral8.bas b/Graphics/Spirals/spiral8.bas index 2f93ed5..ac3a5f8 100644 --- a/Graphics/Spirals/spiral8.bas +++ b/Graphics/Spirals/spiral8.bas @@ -1,84 +1,99 @@ -' Spiral -' made by Svjatoslav Agejenko -' in 2003.12 -' H-Page: svjatoslav.eu -' E-Mail: svjatoslav@svjatoslav.eu - +' Program to render fancy looking spiral. +' 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 fill (x1, y1, x2, y2, xx1, yy1, xx2, yy2) -DIM SHARED torux(1 TO 10000) -DIM SHARED toruy(1 TO 10000) -DIM SHARED sin1(1 TO 10000) -DIM SHARED cos1(1 TO 10000) -DIM SHARED tor +DECLARE SUB FillSegment (x1, y1, x2, y2, xx1, yy1, xx2, yy2) +DIM SHARED spiralX(1 TO 10000) +DIM SHARED spiralY(1 TO 10000) +DIM SHARED angles(1 TO 10000) +DIM SHARED phaseAngles(1 TO 10000) +DIM SHARED spiralLength SCREEN 12 -su = 200 -tor = 0 -FOR a = 1 TO 97 STEP .15 -tor = tor + 1 -su = SIN(a / 31) * 100 -x = SIN(a) * su * 3 + 320 -y = COS(a) * su + 250 -y = y - (COS(a / 31) * 200) -sin1(tor) = a -cos1(tor) = a / 31 -torux(tor) = x -toruy(tor) = y -PSET (x, y), 15 -NEXT a +stepUnit = 200 +spiralLength = 0 + +' Generate the spiral points +FOR angleIndex = 1 TO 97 STEP .15 + spiralLength = spiralLength + 1 + stepUnit = SIN(angleIndex / 31) * 100 + xPos = SIN(angleIndex) * stepUnit * 3 + 320 + yPos = COS(angleIndex) * stepUnit + 250 + yPos = yPos - (COS(angleIndex / 31) * 200) + angles(spiralLength) = angleIndex + phaseAngles(spiralLength) = angleIndex / 31 + spiralX(spiralLength) = xPos + spiralY(spiralLength) = yPos + PSET (xPos, yPos), 15 +NEXT angleIndex + +' Load texture data from file OPEN "texture.dat" FOR INPUT AS #1 -DIM SHARED text$(1 TO 1000) -a = 0 +DIM SHARED textureData$(1 TO 1000) +textureIndex = 0 1 -LINE INPUT #1, a$ -IF LEFT$(a$, 3) = "END" THEN GOTO 2 -a = a + 1 -text$(a) = a$ + LINE INPUT #1, textureLine$ + IF LEFT$(textureLine$, 3) = "END" THEN GOTO 2 + textureIndex = textureIndex + 1 + textureData$(textureIndex) = textureLine$ GOTO 1 2 -CLOSE #1 CLS -a = 1 -FOR c = 1 TO 20 -FOR b = 1 TO LEN(text$(c)) -a$ = RIGHT$(LEFT$(text$(c), b), 1) -a = a + 1 -IF a > tor - 43 THEN GOTO 3 -tee = SIN(sin1(a + 32)) -tee = tee - COS(cos1(a)) -IF tee <= 0 THEN -LINE (torux(a), toruy(a))-(torux(a + 1), toruy(a + 1)), 15 -LINE (torux(a), toruy(a))-(torux(a + 42), toruy(a + 42)), 15 -IF a$ = "M" THEN fill torux(a), toruy(a), torux(a + 1), toruy(a + 1), torux(a + 42), toruy(a + 42), torux(a + 43), toruy(a + 43) -END IF +' Apply texture to the spiral +textureIndex = 1 +FOR charIndex = 1 TO 20 + FOR textCharIndex = 1 TO LEN(textureData$(charIndex)) + textureChar$ = RIGHT$(LEFT$(textureData$(charIndex), textCharIndex), 1) + textureIndex = textureIndex + 1 + IF textureIndex > spiralLength - 43 THEN GOTO DONE + teeVal = SIN(angles(textureIndex + 32)) - COS(phaseAngles(textureIndex)) -NEXT b -NEXT c -3 + ' Draw lines if the condition is met + IF teeVal <= 0 THEN + LINE (spiralX(textureIndex), spiralY(textureIndex))-(spiralX(textureIndex + 1), spiralY(textureIndex + 1)), 15 + LINE (spiralX(textureIndex), spiralY(textureIndex))-(spiralX(textureIndex + 42), spiralY(textureIndex + 42)), 15 + ' Fill the segment if the character matches + IF textureChar$ = "M" THEN + CALL FillSegment(spiralX(textureIndex), spiralY(textureIndex), spiralX(textureIndex + 1), spiralY(textureIndex + 1), spiralX(textureIndex + 42), spiralY(textureIndex + 42), spiralX(textureIndex + 43), spiralY(textureIndex + 43)) + END IF + END IF + NEXT textCharIndex +NEXT charIndex +DONE: a$ = INPUT$(1) SYSTEM -SUB fill (zx1, zy1, zx2, zy2, zxx1, zyy1, zxx2, zyy2) -x1 = zx1 -y1 = zy1 -x2 = zx2 -y2 = zy2 -xx1 = zxx1 -yy1 = zyy1 -xx2 = zxx2 -yy2 = zyy2 -j = 10 -x3 = (x2 - x1) / j -y3 = (y2 - y1) / j -xx3 = (xx2 - xx1) / j -yy3 = (yy2 - yy1) / j -FOR a = 1 TO j -x1 = x1 + x3 -y1 = y1 + y3 -xx1 = xx1 + xx3 -yy1 = yy1 + yy3 -LINE (x1, y1)-(xx1, yy1), 15 -NEXT a +' Subroutine to fill a segment with lines +SUB FillSegment (x1, y1, x2, y2, xx1, yy1, xx2, yy2) + ' Assign input parameters to local variables + xStart = x1 + yStart = y1 + xEnd = x2 + yEnd = y2 + xxStart = xx1 + yyStart = yy1 + xxEnd = xx2 + yyEnd = yy2 + + ' Calculate step increments + j = 10 + xStep = (xEnd - xStart) / j + yStep = (yEnd - yStart) / j + xxStep = (xxEnd - xxStart) / j + yyStep = (yyEnd - yyStart) / j + + ' Draw lines between the points + FOR a = 1 TO j + xStart = xStart + xStep + yStart = yStart + yStep + xxStart = xxStart + xxStep + yyStart = yyStart + yyStep + LINE (xStart, yStart)-(xxStart, yyStart), 15 + NEXT a END SUB