Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Just a note on convention: you should start function names with upper case, and variables with lower case. In the function <code>sortByAngle</code>, you have a reference to the parameter <code>angle</code> and the function <code>angle</code> simultaneously.</p> <p>Assuming <code>Angle(...)</code> is simply meant to calculate the angle between two points:</p> <pre><code>private static double Angle(Vertex v1, Vertex v2) { return Math.Atan2(v2.Y - v1.Y, v2.X - v1.X); } </code></pre> <p>will give you the angle from <code>v1</code> to <code>v2</code>, in radians between -pi and +pi. Do not mix degrees and radians. My suggestion is to always use radians, and only convert to degrees if necessary for human-readable output.</p> <pre><code>private static Vertex[] SortByAngle(Vertex[] vs, Vertex v, double angle) { List&lt;Vertex&gt; vertList = new List&lt;Vertex&gt;(vs); vertList.Sort((v1, v2) =&gt; AngleDifference(angle, Angle(v, v1)).CompareTo(AngleDifference(angle, Angle(v, v2)))); return vertList.ToArray(); } </code></pre> <p>uses <code>List.Sort</code> to sort the vertices from greatest to least angle difference between the vertices <code>point</code> and itself, and <code>angle</code>. The order of <code>v1</code> and <code>v2</code> are swapped in the input tuple to sort descending, that is, greatest difference first. The difference between angles is calculated like so:</p> <pre><code>private static double AngleDifference(double a, double b) { while (a &lt; b - Math.PI) a += Math.PI * 2; while (b &lt; a - Math.PI) b += Math.PI * 2; return Math.Abs(a - b); } </code></pre> <p>The first two lines ensure that the angles are not more than 180 degrees apart.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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