#### Howdy, Stranger!

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

#### Categories

Welcome to the new platform of Programmers Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use it's exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

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

Posts: 14Member
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
· ·

• Posts: 675Member
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
:
· ·