Click here to Skip to main content
15,845,330 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
I am allowing users to draw a polygon in silverlight by clicking to draw. Then I loop through the points, convert them to longitude and latitude and then save to SQL(geography column).

The problem is that because of the world being round and all that(:P) it only works if the user draws clockwise else it tries to make the polygon right round the world and fails. I totally understand why this is happening but cannot think of how to figure out in code what order to put in the dots.

So how do I do this correctly? Or do I have to work out which way they are drawing, and if so how?

Thanks, Matt

p.s. the answer here: Contour Points direction clockwise or counterclockwise[^]
seems exactly what I want except I don't understand it ;)

1 solution

I did this last fall...

.. the key is to quickly (doesn't need to be very accurate float is probably good) the centroid then go points[o]->pCenter->points[1] or since you have an ordered N-gon, you just need to know if they went clockwise or counterclockwise as they did their drawing, so you'll only need to test the winding of one triangle...

here's the code:
vector2 a,b,c; //c is centeroid (supplied)

a -= c;
b -= c;
//'+' == cw  (clockwise)
//'0' == all points in same line
//'-' == ccw (counter clockwise)
var ret = (int)((b.x*a.y)-(a.x-b.y));
return (ret>=0)?((ret>0)?(1):(0)):(-1);

:thumbsup: if this helps.. vote me ^ ;P thx... :thumbsup:
Share this answer
Matt Cavanagh 4-Jan-11 1:05am    
Looks perfect, thank you :D
ely_bob 4-Jan-11 10:07am    
Glad to be of assistance...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900