c++cmathcomputational-geometryatan2

Find clockwise angle between two points with respect to an arbitrary origin


I have two points A(X,Y) and B(P,Q) in the first quadrant. There's another point C(L,M). How do I find angle between CA and CB in clockwise direction?

I searched a lot and all the solution used atan2() but it finds the angle from origin with respect to x axis.

C and A can be assumed fixed. And B is can be anywhere in the first quadrant

C and A can be assumed fixed. And B is can be anywhere in the first quadrant. The angle must be clockwise and within range 0-360 (Or 0 to 360-1).

I am doing this in C/C++.

Edit : Adding code per request. This is a bit different, because I got stuck at a concept and needed clarification regarding it. This function ought to return if the point x,y lies between 50,50 and P. P is the angle with respect to CA.

bool isInsideAngle(long double x,long double y, long double p)
{   
    if((atan2(y,x) >= atan2(50,100)) && (atan2(y,x) <= (p * PI / 50)))
    {
        // cout<<"YES!";
        //   cout<<" atan2(y,x) = " <<atan2(y,x)*180/PI<<endl;
        //   cout<<" atan2(50,50) = " <<atan2(50,100)*180/PI<<endl;
        //   cout<<" (p * PI / 50) = "<<(p * PI / 50)*180/PI<<endl;
        return true;
    }

    else
        return false;

 }

Solution

  • How do I find angle between CA and CB in clockwise direction?

    // The atan2 functions return arctan y/x in the interval [−π , +π] radians
    double Dir_C_to_A = atan2(Ay - Cy, Ax - Cx);
    double Dir_C_to_B = atan2(By - Cy, Bx - Cx);
    double Angle_ACB = Dir_C_to_A - Dir_C_to_B;
    
    // Handle wrap around
    const double Pi = acos(-1);  // or use some π constant
    if (Angle_ACB > Pi) Angle_ACB -= 2*Pi;
    else if (Angle_ACB < -Pi) Angle_ACB += 2*Pi;
    
    // Answer is in the range of [-pi...pi]
    return Angle_ACB;