-' Program to render polygons at random locations and random colors.\r
-' By Svjatoslav Agejenko.\r
-' Email: svjatoslav@svjatoslav.eu\r
-' Homepage: http://www.svjatoslav.eu\r
-'\r
-' Changelog:\r
-' 2001, Initial version\r
-' 2024.08, Improved program readability using AI\r
-\r
-DEFINT A-Z\r
-DECLARE SUB fillPolygon (x1, y1, x2, y2, x3, y3, c)\r
-SCREEN 13\r
-\r
-MainLoop:\r
- x1 = RND * 318 + 1\r
- y1 = RND * 198 + 1\r
-\r
- x2 = RND * 318 + 1\r
- y2 = RND * 198 + 1\r
-\r
- x3 = RND * 318 + 1\r
- y3 = RND * 198 + 1\r
-\r
- fillPolygon x1, y1, x2, y2, x3, y3, RND * 255\r
- IF INKEY$ <> "" THEN SYSTEM\r
-GOTO MainLoop\r
-\r
-SUB fillPolygon (x1, y1, x2, y2, x3, y3, c)\r
- DIM yBuffer(-10 TO 210)\r
-\r
- tempX1 = x1\r
- tempY1 = y1\r
- tempX2 = x2\r
- tempY2 = y2\r
- GOSUB makeLine\r
-\r
- tempX1 = x1\r
- tempY1 = y1\r
- tempX2 = x3\r
- tempY2 = y3\r
- GOSUB makeLine\r
-\r
- tempX1 = x3\r
- tempY1 = y3\r
- tempX2 = x2\r
- tempY2 = y2\r
- GOSUB makeLine\r
-\r
-GOTO FillEnd\r
-\r
-makeLine:\r
- IF tempY2 < tempY1 THEN SWAP tempY1, tempY2: SWAP tempX1, tempX2\r
- FOR yIndex = tempY1 TO tempY2 - 1\r
- xPos = tempX1 + (tempX2 - tempX1) * ((yIndex - tempY1) / (tempY2 - tempY1))\r
- IF yBuffer(yIndex) = 0 THEN\r
- yBuffer(yIndex) = xPos\r
- ELSE\r
- LINE (xPos, yIndex)-(yBuffer(yIndex), yIndex), c\r
- END IF\r
- NEXT yIndex\r
-RETURN\r
-\r
-FillEnd:\r
-END SUB\r
+' 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