Sort an array of Vector2 relative to origin clockwise

I found a piece of code that I’ve tried using to sort an array of Vector2’s in a clockwise order, relative to a specified origin point, but it seems sorta random as to whether it works or not, and it’s out of my knowledge to be able to fix it.

I found this code, from here and I’m using that one, but I suspect something is wrong with it and I’m unable to find the issue, so I need help with fixing the code if there’s an issue with it.

Here’s how I’m using it (in case I’m doing this part wrong):

``````Vector2[] clockwise = new Vector2[] { point1, point2, point3 };
Array.Sort(clockwise, new ClockwiseComparer((point1 + point2 + point3) / 3f));
``````

I’ve made a little test in my scene and this code works for me. Change the isClockwise method to this:

``````public static int IsClockwise(Vector2 first, Vector2 second, Vector2 origin)
{
Vector2 dir1 = first - origin;
Vector2 dir2 = second - origin;
float angle1 = Mathf.Atan2(dir1.x, dir1.y) * Mathf.Rad2Deg;
float angle2 = Mathf.Atan2(dir2.x, dir2.y) * Mathf.Rad2Deg;

if (angle1 < 0f) angle1 += 360;
if (angle2 < 0f) angle2 += 360;

return angle1.CompareTo(angle2);
}
``````

this does not work. points up to 9 are built correctly and then just do it randomly (((