Search code examples
c++mathmathematical-optimization

Efficiently recognize in which axis quarter is a given angle Angle


I would like to recognize in what axis quarter is a given angle In the Most efficient way

Quarter 1: Alpha > 0 && Alpha <= 90 (degrees)

Quarter 2: Alpha > 90 && Alpha <= 180 (degrees)

Quarter 3: Alpha > 180 && Alpha <= 270 (degrees)

Quarter 4: Alpha > 270 && Alpha <= 360 (degrees)

C++ Code

FORCEINLINE uint8 MapAngleToQuarter(float angle)
{
    angle = (int)angle % 360;
    float answer = 0;

    if(angle > 0 && angle <= 90)
    {
        answer = 1;
    }
    else if(angle > 90 && angle <= 180)
    {
        answer = 2;
    }
    else if(angle > 180 && angle <= 270)
    {
        answer = 3;
    }
    else if(angle > 270 && angle <= 360)
    {
        answer = 4;
    }
    return answer;
}

My question: Is there a better (more efficient) way to do the above task ?


Solution

  • With the angle in the (0...360] range

    FORCEINLINE uint8 MapAngleToQuarter(float angle) {
      int a = (int) angle;
      return (a - 1)/90 + 1;
    }