Re: [Algorithms] Draw & fill regular polygon?
Brought to you by:
vexxed72
From: Richard F. <ra...@gm...> - 2011-08-10 13:27:33
|
On 10 August 2011 12:49, Joel B <one...@ea...> wrote: > I should know how to do this, but i don't. I only have the ability in my system to draw lines (x1,y1,x2,y2)' to a bitmap, or write to the bitmap as a byte array. Can someone tell me the stepwise procedure to draw a polygon of n sides and then fill it? Having trouble finding anything online that doesn't use pre-existing primitives or libraries. > > Thanks, > > Joel if you already have a line drawing function, then use the code from that to update a pair of arrays of min/max on the x axis: int xmin[SCREEN_HEIGHT] = { SCREEN_WIDTH... } int xmax[SCREEN_HEIGHT] = { -1... } then for each step of your line drawing function update the min/max: replace your: put_pixel(x,y,colour) with: xmin[y] = min(x,xmin[y]); xmax[y] = max(x,xmax[y]); Once you have the arrays, it's a simple case of iterating over them and rendering all pixels between the min and max where the difference is positive. foreach y in SCREEN_HEIGHT: if( xmax[y] > xmin[y] ) foreach( x in range( xmin[y], xmax[y] ): put_pixel( x,y, colour ) and you have an untextured triangle. if you want it textured, don't forget to store only UV values when the max/min is updated. -- fabs(); Just because the world is full of people that think just like you, doesn't mean the other ones can't be right. |