From: Svjatoslav Agejenko Date: Tue, 15 Oct 2024 19:12:52 +0000 (+0300) Subject: Refactoring code for better readability X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=commitdiff_plain;h=42db1243616edc988aeb3df663ad655c288dbb4d;p=qbasicapps.git Refactoring code for better readability --- diff --git a/Graphics/poly.bas b/Graphics/poly.bas index 52291a5..9d69cf5 100755 --- a/Graphics/poly.bas +++ b/Graphics/poly.bas @@ -1,64 +1,81 @@ -' Program to render polygons at random locations and random colors. -' By Svjatoslav Agejenko. -' Email: svjatoslav@svjatoslav.eu -' Homepage: http://www.svjatoslav.eu -' -' Changelog: -' 2001, Initial version -' 2024.08, Improved program readability using AI - -DEFINT A-Z -DECLARE SUB fillPolygon (x1, y1, x2, y2, x3, y3, c) -SCREEN 13 - -MainLoop: - x1 = RND * 318 + 1 - y1 = RND * 198 + 1 - - x2 = RND * 318 + 1 - y2 = RND * 198 + 1 - - x3 = RND * 318 + 1 - y3 = RND * 198 + 1 - - fillPolygon x1, y1, x2, y2, x3, y3, RND * 255 - IF INKEY$ <> "" THEN SYSTEM -GOTO MainLoop - -SUB fillPolygon (x1, y1, x2, y2, x3, y3, c) - DIM yBuffer(-10 TO 210) - - tempX1 = x1 - tempY1 = y1 - tempX2 = x2 - tempY2 = y2 - GOSUB makeLine - - tempX1 = x1 - tempY1 = y1 - tempX2 = x3 - tempY2 = y3 - GOSUB makeLine - - tempX1 = x3 - tempY1 = y3 - tempX2 = x2 - tempY2 = y2 - GOSUB makeLine - -GOTO FillEnd - -makeLine: - IF tempY2 < tempY1 THEN SWAP tempY1, tempY2: SWAP tempX1, tempX2 - FOR yIndex = tempY1 TO tempY2 - 1 - xPos = tempX1 + (tempX2 - tempX1) * ((yIndex - tempY1) / (tempY2 - tempY1)) - IF yBuffer(yIndex) = 0 THEN - yBuffer(yIndex) = xPos - ELSE - LINE (xPos, yIndex)-(yBuffer(yIndex), yIndex), c - END IF - NEXT yIndex -RETURN - -FillEnd: -END SUB +' Program to render polygons at random locations and random colors. +' By Svjatoslav Agejenko. +' Email: svjatoslav@svjatoslav.eu +' Homepage: http://www.svjatoslav.eu + +' Changelog: +' 2001, Initial version +' 2024, Improved program readability using AI + +DEFINT A-Z +DECLARE SUB fillPolygon (x1, y1, x2, y2, x3, y3, c) +SCREEN 13 + +MainLoop: + ' Generate random coordinates for the first vertex + x1 = RND * 318 + 1 + y1 = RND * 198 + 1 + + ' Generate random coordinates for the second vertex + x2 = RND * 318 + 1 + y2 = RND * 198 + 1 + + ' Generate random coordinates for the third vertex + x3 = RND * 318 + 1 + y3 = RND * 198 + 1 + + ' Fill the polygon with a random color + fillPolygon x1, y1, x2, y2, x3, y3, RND * 255 + + ' Check if any key is pressed to exit the loop + IF INKEY$ <> "" THEN SYSTEM +GOTO MainLoop + +SUB fillPolygon (x1, y1, x2, y2, x3, y3, c) + ' Buffer array to store x-coordinates for each y-index + DIM yBuffer(-10 TO 210) + + ' Draw the line between the first and second vertices + tempX1 = x1 + tempY1 = y1 + tempX2 = x2 + tempY2 = y2 + GOSUB makeLine + + ' Draw the line between the first and third vertices + tempX1 = x1 + tempY1 = y1 + tempX2 = x3 + tempY2 = y3 + GOSUB makeLine + + ' Draw the line between the second and third vertices + tempX1 = x3 + tempY1 = y3 + tempX2 = x2 + tempY2 = y2 + GOSUB makeLine + +GOTO FillEnd + +makeLine: + ' Ensure that the start point is always below the end point + IF tempY2 < tempY1 THEN SWAP tempY1, tempY2: SWAP tempX1, tempX2 + + ' Loop through each y-index from the start to the end + FOR yIndex = tempY1 TO tempY2 - 1 + ' Calculate the x-position for the current y-index + xPos = tempX1 + (tempX2 - tempX1) * ((yIndex - tempY1) / (tempY2 - tempY1)) + + ' If the buffer is empty, store the x-position + IF yBuffer(yIndex) = 0 THEN + yBuffer(yIndex) = xPos + ELSE + ' Otherwise, draw a line between the stored and calculated positions + LINE (xPos, yIndex)-(yBuffer(yIndex), yIndex), c + END IF + NEXT yIndex +RETURN + +FillEnd: +END SUB