Enhanced/Additional 2D Graphics Commands by Ade15th Feb 2005 18:57
|
---|
Summary Triangle, TriangleFill, CircleFill, EllipseFill, FloodFill, BoxFill Description Existing 2D graphics commands are limited and as I am teaching my kids to program they need a bit more to play around with. They wanted to colour in their programmed drawings with so I wrote a FloodFill routine which is slightly different to the other one presented in the codebase. Also, it seemed odd that there was no triangle command and that circles and ellipses could not be filled in - so here are some initial additional 2d commands: Code ` This code was downloaded from The Game Creators ` It is reproduced here with full permission ` http://www.thegamecreators.com function Triangle(x1, y1, x2, y2, x3, y3) line x1, y1, x2, y2 line x2, y2, x3, y3 line x3, y3, x1, y1 endfunction function TriangleFill(x1, y1, x2, y2, x3, y3, colour) ink colour, 0 line x1, y1, x2, y2 line x2, y2, x3, y3 line x3, y3, x1, y1 rem center point of the triangle is simply to average the three x and y co-ords x = (x1 + x2 + x3) / 3 y = (y1 + y2 + y3) / 3 FloodFill(x, y, colour) endfunction function BoxFill(x1, y1, x2, y2, colour) box x1, y1, x2, y2, colour, colour, colour, colour endfunction function CircleFill(x, y, radius, colour) ink colour, 0 circle x, y, radius FloodFill(x, y, colour) endfunction function EllipseFill(x, y, radiusX, radiusY, colour) ink colour, 0 ellipse x, y, radiusX, radiusY FloodFill(x, y, colour) endfunction function FloodFill(x, y, fillColour as dword) backgroundColour = point(x, y) : rem this should work in all bpp display modes if backgroundColour = fillColour then exitfunction ink fillColour, 0 : rem setup the fill colour as foreground, and background as black (this is not used) lock pixels __FloodFill(x, y, backgroundColour) unlock pixels endfunction function __FloodFill(x, y, backgroundColour as dword) BitmapWidth = bitmap width() rem do current row as far as possible x1 = x - 1 while (x1 >= 0) and (point(x1, y) = backgroundColour) dec x1 endwhile rem from current point find righthand extent x2 = x + 1 while (x2 < BitmapWidth) and (point(x2, y) = backgroundColour) inc x2 endwhile inc x1 if x2 - x1 > 0 line x1, y, x2, y : rem draw a line between the extents for z = 1 to 2 if z = 1 y2 = y - 1 : rem process line of pixels above working along all breaks in the line else y2 = y + 1 : rem process line of pixels below working along all breaks in the line endif if (y > 0 and z = 1) or (y < bitmap height() - 1 and z = 2) x4 = x1 while x4 > 0 and point(x4, y2) = backgroundColour dec x4 endwhile x5 = x2 - 1 while x5 < BitmapWidth - 1 and point(x5, y2) = backgroundColour inc x5 endwhile newExtent = 1 for x3 = x4 to x5 - 1 if point(x3, y2) = backgroundColour if newExtent = 1 newExtent = 0 xstart = x3 endif else if newExtent = 0 __FloodFill(xstart, y2, backgroundColour) newExtent = 1 endif endif next if newExtent = 0 then __FloodFill(xstart, y2, backgroundColour) endif next endif endfunction |