title: Determine if angle lies between 2 other angles
body: <p>I am trying to figure out whether a angle lies between 2 other angles. I have been trying to create a simple function to perform this but none of my techniques will work for all possible values of the angles.</p>
  
  <p><strong>Can you help me edit my function to correctly determine if a angle lies between 2 other angles?</strong></p>
  
  <p><img src="https://i.stack.imgur.com/tCiUF.png" alt="enter image description here"></p>
  
  <p>In the above picture; I use the green point as the central point, then I determine the angle of each line to the green point. I then calculate the angle of the black point to the green point. I am trying to check if the angle of the black dot is <strong>BETWEEN</strong> the 2 lines' angles.</p>
  
  <p><em>NOTE: In my case; an angle(targetAngle) is said to lie between 2 other angles IF the difference between the 2 angles is &lt; 180 degrees AND the targetAngle lies in the cavity made by those 2 angles.</em></p>
  
  <p>The following code should work but it fails for these(which do lie between the angle):<br>
  - is_angle_between(150, 190, 110)<br>
  - is_angle_between(3, 41, 345)    </p>
  
  <pre><code>bool is_angle_between(int target, int angle1, int angle2) 
  {  
    int rAngle1 = ((iTarget - iAngle1) % 360 + 360) % 360;  
    int rAngle2 = ((iAngle2 - iAngle1) % 360 + 360) % 360;  
    return (0 &lt;= rAngle1 &amp;&amp; rAngle1 &lt;= rAngle2);  
  }  
  
  // Example usage  
  is_angle_between(3, 41, 345);  
  </code></pre>
  
  <p>Another technique I attempted which also doesn't work:</p>
  
  <pre><code>int is_angle_between(int target, int angle1, int angle2)
  {
    int dif1  = angle1-angle2;
    int dif2  = angle2-angle1;
    int uDif1 = convert_to_positive_angle( dif1 ); // for eg; convert -15 to 345
    int uDif2 = convert_to_positive_angle( dif2 );
  
    if (uDif1 &lt;= uDif2) {
      if (dif1 &lt; 0) {
        return (target &lt;= angle1 &amp;&amp; target &gt;= angle2);
      }
      else return (in_between_numbers(iTarget, iAngle1, iAngle2));
    }
    else {
      if (dif2 &lt; 0) {
        return (target &lt;= angle1 &amp;&amp; target &gt;= angle2);
      }
      else return (in_between_numbers(iTarget, iAngle1, iAngle2));
    }
  
    return -1;
  }
  </code></pre>