Re: Draw & fill regular polygon?
Manuel Massing <[email protected]> Wed, 10 Aug 2011 14:26:11 +0200
| Newsgroups | gmane.games.devel.algorithms |
|---|---|
| Message-ID | <[email protected]> |
Hi Joel,
> 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.
I think the easiest way to fill a convex polygon is to generate horizontal
spans, and fill them in.
Declare two arrays (e.g. "left" and "right"), which will be used to store the
horizontal spans of the polygon. Initialize them with max/min of your
datatype.
Rasterize each segment on the polygon boundary, by stepping along the y-axis.
and store the corresponding x coordinate. Something like:
// Caveat: hande horizontal segments, last segment, etc.
float dX = (float)(boundary[i+1].x - boundary[i].x)/(boundary[i+1].y -
boundary[i].y);
float X = boundary[i].x + 0.5;
for (int y = boundary[i].y; y < max_y; y++)
{
X+= dX;
left[y] = min(left[y], (int)X);
right[y] = max(right[y], (int)X);
}
Now, fill in the spans:
for (y = min_y; y < max_y; y++)
for (x = left[y]; x <= right[y]; x++)
put_pixel(x, y, color); // Do this via memset instead!
hope this helps.
Manuel
------------------------------------------------------------------------------
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