15,844,673 members
See more: , +
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 ;)
Posted

## Solution 1

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:

Matt Cavanagh 4-Jan-11 1:05am
Looks perfect, thank you :D
ely_bob 4-Jan-11 10:07am