AddPolygon and Order of Points

F

farooq.omar

I have a list of points (point[] array) and I want to create a polygon
using the graphics path addPolygon routine. Is there a way to sort/
arrange the points in a clockwise manner so that the polygon is drawn
in a "proper" way.

Thanks
 
H

Hans Kesting

I have a list of points (point[] array) and I want to create a polygon
using the graphics path addPolygon routine. Is there a way to sort/
arrange the points in a clockwise manner so that the polygon is drawn
in a "proper" way.

Thanks

There is no "SortClockwise" method (that I know of), however you might be
able to build one:

Array has a Sort method that accepts an IComparer instance, to sort on some
programmer-defined criterion.

Build a comparer-class that you feed some center point (average X and Y values
of the points?) and compares two Point objects based on the angle from
that center to each Point.

Good luck,

Hans Kesting
 
G

Guest

If your points are already in an order, then look at summing the interior
angles of all pairs of segments. For any polygon with N sides, your sum will
be (N-2) * 180. When you go arround the wrong way, you will have a different
sum, so you know to reverse the segment order.

The harder problem is taking a collection of points and getting the polygon
that they define if you don't have a prescribed order. Take a look at
algorithms for the "traveling salesman" problem. The shortest path to visit
all should be a polygon that is resonable, then apply the method above.


Hans Kesting said:
I have a list of points (point[] array) and I want to create a polygon
using the graphics path addPolygon routine. Is there a way to sort/
arrange the points in a clockwise manner so that the polygon is drawn
in a "proper" way.

Thanks

There is no "SortClockwise" method (that I know of), however you might be
able to build one:

Array has a Sort method that accepts an IComparer instance, to sort on some
programmer-defined criterion.

Build a comparer-class that you feed some center point (average X and Y values
of the points?) and compares two Point objects based on the angle from
that center to each Point.

Good luck,

Hans Kesting
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top