Re: Draw & fill regular polygon?

Richard Fabian <[email protected]> Wed, 10 Aug 2011 14:21:33 +0100
Newsgroups gmane.games.devel.algorithms
Message-ID <CACot5u23iJ1n8pn5G=XN6xq_cVmzDa12Nv-a0VCsiv_-vM9ZVg@mail.gmail.com>
On 10 August 2011 12:49, Joel B <[email protected]> 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.

------------------------------------------------------------------------------
uberSVN's rich system and user administration capabilities and model 
configuration take the hassle out of deploying and managing Subversion and 
the tools developers use with it. Learn more about uberSVN and get a free 
download at:  http://p.sf.net/sfu/wandisco-dev2dev
_______________________________________________
GDAlgorithms-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gdalgorithms-list
Archives:
http://sourceforge.net/mailarchive/forum.php?forum_name=gdalgorithms-list