Note that there are some explanatory texts on larger screens.

plurals
  1. POC# Sort List of X,Y Coordinates Clockwise
    text
    copied!<p>I am using .NET 2.0 so I don't have access to the nifty Linq; however, I have worked up some code that sorts a list of points clockwise/counterclockwise.</p> <p>My problem is that the sort works perfectly fine if the list is not already sorted, but if for some reason the list is already sorted the sort function fails miserably.</p> <p>I was wandering if someone could help point me in the right direction as to why this may be the cause.</p> <p>Here is my call to the sort:</p> <pre><code>positions.Sort(new Comparison&lt;Position&gt;(MapUtils.SortCornersClockwise)); </code></pre> <p>And here is the SortCornersClockwise function:</p> <pre><code>public static int SortCornersClockwise( Position A, Position B) { // Variables to Store the atans double aTanA, aTanB; // Reference Point Pixels reference = Program.main.reference; // Fetch the atans aTanA = Math.Atan2(A.Pixel.Y - reference.Y, A.Pixel.X - reference.X); aTanB = Math.Atan2(B.Pixel.Y - reference.Y, B.Pixel.X - reference.X); // Determine next point in Clockwise rotation if (aTanA &lt; aTanB) return -1; else if (aTanB &gt; aTanA) return 1; return 0; } </code></pre> <p>Where my reference is the point from which I determine the respective angle to each point in my list of points.</p> <p>Now say I have a list of points:</p> <pre><code>15778066, 27738237 15778169, 27738296 15778185, 27738269 15778082, 27738210 </code></pre> <p>These are already sorted in the correct order but calling the sort function yields:</p> <pre><code>15778082, 27738210 15778066, 27738237 15778185, 27738269 15778169, 27738296 </code></pre> <p>Now take another set of sample points:</p> <pre><code>15778180, 27738255 15778081, 27738192 15778064, 27738219 15778163, 27738282 </code></pre> <p>This list is not already in the correct order and calling the sort function yields:</p> <pre><code>15778064, 27738219 15778081, 27738192 15778180, 27738255 15778163, 27738282 </code></pre> <p>Which is sorted correctly. This pattern repeats it self for each set of coordinates that are already sorted and for those that aren't. Any ideas?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload