title: Simple polygon from unordered points
body: <p>I've been trying to create an algorithm for finding the order of points in a simple polygon. 
  The aim is when given points on a 2D plane (there is always possible to form a simple polygon btw) I output the order of points in a valid simple polygon. All points must be part of said polygon.</p>
  
  <p>I've somewhat achieved this, but it fails for some test cases. The way I have done this is by finding the geometrical centre </p>
  
  <pre><code>int centerX = (lowX + highX) / 2;
              int centerY = (lowY + highY) / 2;
  
              Point center = new Point(centerX, centerY, -1);
  </code></pre>
  
  <p>and then sorting all points by their polar angle. </p>
  
  <pre><code>Collections.sort(points, (a, b) -&gt; {
                  if(a == b || a.equals(b)) {
                      return 0;
                  }
                  double aTheta = Math.atan2((long)a.y - center.y, (long)a.x - center.x);
                  double bTheta = Math.atan2((long)b.y - center.y, (long)b.x - center.x);
                  if(aTheta &lt; bTheta) {
                      return -1;
                  }
                  else if(aTheta &gt; bTheta) {
                      return 1;
                  }
                  else {
                      double aDist = Math.sqrt((((long)center.x - a.x) * ((long)center.x - a.x)) +
                                              (((long)center.y - a.y) * ((long)center.y - a.y)));
  
                      double bDist = Math.sqrt((((long)center.x - b.x) * ((long)center.x - b.x)) +
                                              (((long)center.y - b.y) * ((long)center.y - b.y)));
  
                      if (aDist &lt; bDist) {
                          return -1;
                      } 
                      else {
                          return 1;
                      }
                  }    
              });
  </code></pre>
  
  <p>I'm struggling with finding out what makes this break for some of the test cases. Any help or pointers are greatly appreciated! Also wondering if there are any efficient, yet not overly complicated algorithms that can perform this. </p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I've found one of the failing test cases: When given the points <code>(101, 101), (100, 100), (105, 100), (103, 100), (107, 100), (102, 100), (109, 100)</code> Labeled 0 to 9 respectively</p>
  
  <p>My program outputs <code>2 4 6 0 3 5 1</code> but it is not a valid simple polygon</p>
  
  <p>It should be a permutation of <code>1 0 6 4 2 3 5</code></p>