-- public-domain code by Darel Rex Finley, 2007 -- from http://alienryderflex.com/polygon_fill/ -- converted to Lua by Stefan Reich w, h = 400, 400 polyX = {10, 100, 50} polyY = {20, 20, 60} polyCorners = #polyY -- int nodes, nodeX[MAX_POLY_CORNERS], pixelX, pixelY, i, j, swap ; nodeX = {} pixels = {} for i=1, w*h do pixels[i] = -1 end -- Loop through the rows of the image. for pixelY = 0, h-1 do -- Build a list of nodes. nodes = 0 j = polyCorners-1 for i=0, polyCorners-1 do if polyY[i+1] < pixelY and polyY[j+1] >= pixelY or polyY[j+1] < pixelY and polyY[i+1] >= pixelY then nodes = nodes+1 nodeX[nodes] = math.ceil(0.5+(polyX[i+1]+(pixelY-polyY[i+1])/(polyY[j+1]-polyY[i+1]) *(polyX[j+1]-polyX[i+1]))) end j = i end -- Sort the nodes, via a simple “Bubble” sort. i = 1 while i < nodes do if nodeX[i] > nodeX[i+1] then nodeX[i], nodeX[i+1] = nodeX[i+1], nodeX[i] if i > 1 then i = i-1 end else i = i+1 end end -- Fill the pixels between node pairs. for i=1, nodes-1, 2 do if nodeX[i ] >= w then goto raus end if nodeX[i+1] > 0 then if nodeX[i ] < 0 then nodeX[i ] = 0 end if nodeX[i+1] > w then nodeX[i+1] = w end for pixelX = nodeX[i], nodeX[i+1]-1 do pixels[pixelX+pixelY*w+1] = 0 end end end ::raus:: end