Better code readability.
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Thu, 26 Jun 2025 13:52:38 +0000 (16:52 +0300)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Thu, 26 Jun 2025 13:52:38 +0000 (16:52 +0300)
2D GFX/Presentations/AI/AI demo.bas

index 306e0bc..be9f839 100644 (file)
@@ -141,55 +141,57 @@ SUB DrawRoundedBox (xOne, yOne, xTwo, yTwo)
 END SUB\r
 \r
 \r
-SUB FillPolygon (xCoordOne, yCoordOne, xCoordTwo, yCoordTwo, xCoordThree, yCoordThree, colorValue)\r
+SUB FillPolygon (x1, y1, x2, y2, x3, y3, fillColor)\r
 '\r
-' Fills a triangular region using a simple scanline approach.\r
-'   1) Draw lines between points to find intersections on each horizontal row.\r
-'   2) Use the pairs of intersection points to draw horizontal lines in colorValue.\r
+' Fills a triangular area using a scanline algorithm.\r
+' 1. Finds intersection points between edges and horizontal scanlines\r
+' 2. Draws horizontal lines between pairs of intersections\r
 '\r
     DIM scanIntersection(-100 TO 300)\r
 \r
-    localX1 = xCoordOne\r
-    localY1 = yCoordOne\r
-    localX2 = xCoordTwo\r
-    localY2 = yCoordTwo\r
-    GOSUB fillLines\r
-\r
-    localX1 = xCoordOne\r
-    localY1 = yCoordOne\r
-    localX2 = xCoordThree\r
-    localY2 = yCoordThree\r
-    GOSUB fillLines\r
-\r
-    localX1 = xCoordThree\r
-    localY1 = yCoordThree\r
-    localX2 = xCoordTwo\r
-    localY2 = yCoordTwo\r
-    GOSUB fillLines\r
-\r
-    GOTO polygonDone\r
-\r
-fillLines:\r
-    ' If the second point is above the first, swap them\r
-    IF localY2 < localY1 THEN\r
-        SWAP localY1, localY2\r
-        SWAP localX1, localX2\r
+    startPointX = x1\r
+    startPointY = y1\r
+    endPointX = x2\r
+    endPointY = y2\r
+    GOSUB drawEdges\r
+\r
+    startPointX = x1\r
+    startPointY = y1\r
+    endPointX = x3\r
+    endPointY = y3\r
+    GOSUB drawEdges\r
+\r
+    startPointX = x3\r
+    startPointY = y3\r
+    endPointX = x2\r
+    endPointY = y2\r
+    GOSUB drawEdges\r
+\r
+    GOTO finishPolygon\r
+\r
+drawEdges:\r
+    ' Swap points if needed to ensure we draw from top to bottom\r
+    IF endPointY < startPointY THEN\r
+        SWAP startPointY, endPointY\r
+        SWAP startPointX, endPointX\r
     END IF\r
 \r
-    ' Walk through rows from localY1 up to localY2\r
-    FOR yFill = localY1 TO localY2 - 1\r
-        xFill = localX1 + (localX2 - localX1) * ((yFill - localY1) / (localY2 - localY1))\r
-        IF scanIntersection(yFill) = 0 THEN\r
-            ' This is the first intersection discovered on this row\r
-            scanIntersection(yFill) = xFill\r
+    ' For each scanline between the two points\r
+    FOR yScan = startPointY TO endPointY - 1\r
+        ' Calculate x position along the edge\r
+        xScan = startPointX + (endPointX - startPointX) * ((yScan - startPointY) / (endPointY - startPointY))\r
+\r
+        ' Store first intersection point\r
+        IF scanIntersection(yScan) = 0 THEN\r
+            scanIntersection(yScan) = xScan\r
         ELSE\r
-            ' Second intersection found -> draw horizontal line to the first intersection\r
-            LINE (xFill, yFill)-(scanIntersection(yFill), yFill), colorValue\r
+            ' Draw horizontal line between intersections\r
+            LINE (xScan, yScan)-(scanIntersection(yScan), yScan), fillColor\r
         END IF\r
-    NEXT yFill\r
+    NEXT yScan\r
     RETURN\r
 \r
-polygonDone:\r
+finishPolygon:\r
 END SUB\r
 \r
 \r