# ave the content of polygon to 2d char array buffer for storing pixels

Hi All,
I have written code for draw a regular polygon for drawing rectangle, pentagon , hexagon etc.
Now i want to save the content of polygon to 2d char array buffer for storing pixels and save it into a bitmap image file.

But i am not able to do this.
can anyone help me?

Anuj

## Comments

• You don't have an algorithm for this?

A hexigon, rectangle... can all be drawn as polygons so let's just implement a general polygon painting algorithm. The specific shapes can be painted by just picking different sequences of points for the polygon.

Painting a polygon can be easily decomposed into painting a bunch of triangles that make up the polygon. You can come up with this bunch of triangles by taking the first point in the polygon and going shifting the other 2 vertices over 1 by 1 in adjacency until one of them reaches the last vertex.

Now the problem is reduced to filling a triangle.

To fill a triangle, start by splitting the triangle along one of the axis. Let's assume you pick the y=constant axis or a horizontal axis. Find the vertex with highest y value. Let's call this H for highest.
Find the vertex with lowest y-value. Let's call this L for lowest.
Find the vertex with the remaining y-value. Let's call this point m for middle even though it may not be half way between H and L.

Fill the top triangle down to the horizontal line of y=m.y. The verticies of this triangle would include H, m, and a calculated point where y=m.y and forms a straight line leading to L. Fill it in using a loop through the pixels within the region. Use the bit of math involved in the equation of 2D lines like y=mx+b if you have trouble finding where to loop through pixels.

Fill the lower triangle from horizontal line down. You can guess the right vertices.

I've implemented this stuff 3 or 4 times so it should work.

: Hi All,
: I have written code for draw a regular polygon for drawing
: rectangle, pentagon , hexagon etc.
: Now i want to save the content of polygon to 2d char array buffer
: for storing pixels and save it into a bitmap image file.
:
: But i am not able to do this.
: can anyone help me?
:
: Anuj
:
Sign In or Register to comment.

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!