From dd1591f76d2810480a85f4016d8170d28887078b Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko Date: Thu, 26 Jun 2025 16:52:38 +0300 Subject: [PATCH] Better code readability. --- 2D GFX/Presentations/AI/AI demo.bas | 80 +++++++++++++++-------------- 1 file changed, 41 insertions(+), 39 deletions(-) diff --git a/2D GFX/Presentations/AI/AI demo.bas b/2D GFX/Presentations/AI/AI demo.bas index 306e0bc..be9f839 100644 --- a/2D GFX/Presentations/AI/AI demo.bas +++ b/2D GFX/Presentations/AI/AI demo.bas @@ -141,55 +141,57 @@ SUB DrawRoundedBox (xOne, yOne, xTwo, yTwo) END SUB -SUB FillPolygon (xCoordOne, yCoordOne, xCoordTwo, yCoordTwo, xCoordThree, yCoordThree, colorValue) +SUB FillPolygon (x1, y1, x2, y2, x3, y3, fillColor) ' -' Fills a triangular region using a simple scanline approach. -' 1) Draw lines between points to find intersections on each horizontal row. -' 2) Use the pairs of intersection points to draw horizontal lines in colorValue. +' Fills a triangular area using a scanline algorithm. +' 1. Finds intersection points between edges and horizontal scanlines +' 2. Draws horizontal lines between pairs of intersections ' DIM scanIntersection(-100 TO 300) - localX1 = xCoordOne - localY1 = yCoordOne - localX2 = xCoordTwo - localY2 = yCoordTwo - GOSUB fillLines - - localX1 = xCoordOne - localY1 = yCoordOne - localX2 = xCoordThree - localY2 = yCoordThree - GOSUB fillLines - - localX1 = xCoordThree - localY1 = yCoordThree - localX2 = xCoordTwo - localY2 = yCoordTwo - GOSUB fillLines - - GOTO polygonDone - -fillLines: - ' If the second point is above the first, swap them - IF localY2 < localY1 THEN - SWAP localY1, localY2 - SWAP localX1, localX2 + startPointX = x1 + startPointY = y1 + endPointX = x2 + endPointY = y2 + GOSUB drawEdges + + startPointX = x1 + startPointY = y1 + endPointX = x3 + endPointY = y3 + GOSUB drawEdges + + startPointX = x3 + startPointY = y3 + endPointX = x2 + endPointY = y2 + GOSUB drawEdges + + GOTO finishPolygon + +drawEdges: + ' Swap points if needed to ensure we draw from top to bottom + IF endPointY < startPointY THEN + SWAP startPointY, endPointY + SWAP startPointX, endPointX END IF - ' Walk through rows from localY1 up to localY2 - FOR yFill = localY1 TO localY2 - 1 - xFill = localX1 + (localX2 - localX1) * ((yFill - localY1) / (localY2 - localY1)) - IF scanIntersection(yFill) = 0 THEN - ' This is the first intersection discovered on this row - scanIntersection(yFill) = xFill + ' For each scanline between the two points + FOR yScan = startPointY TO endPointY - 1 + ' Calculate x position along the edge + xScan = startPointX + (endPointX - startPointX) * ((yScan - startPointY) / (endPointY - startPointY)) + + ' Store first intersection point + IF scanIntersection(yScan) = 0 THEN + scanIntersection(yScan) = xScan ELSE - ' Second intersection found -> draw horizontal line to the first intersection - LINE (xFill, yFill)-(scanIntersection(yFill), yFill), colorValue + ' Draw horizontal line between intersections + LINE (xScan, yScan)-(scanIntersection(yScan), yScan), fillColor END IF - NEXT yFill + NEXT yScan RETURN -polygonDone: +finishPolygon: END SUB -- 2.20.1