Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying To Calculate the Difference between two angles (atan2)

Atan2(y,x) will return a float between -pi and pi. I want to calculate the distance between two angles, but the non-continuity is throwing me off.

See this for better understanding.

I want to be able to calculate the distance between Angle 1 and Angle 2.

The whole point of this is to be able to create a cone from the center to a specified angle. Essentially I will be evaluating:

if(DistanceFromAngle1 < pi/4 [45°])
{
  Angle2 is part of cone
}
like image 415
ATD Avatar asked Dec 12 '22 18:12

ATD


2 Answers

If by distance you mean the straight line joining the two interception points, you can calculate the distance by doing this:

SQRT( ( ABS|cos(A) - cos(B)| )^2 + ( ABS|sin(A) - sin(B)| )^2 )

SQRT = square root

ABS = Absolute value

If the distance is the angle, you calculate it by doing (pseudo-code)

var angle = ABS(A - B)
if(angle > pi) angle = 2*pi - angle
return angle
like image 148
António Almeida Avatar answered Dec 28 '22 09:12

António Almeida


dAngle1 = //convert angle1 to degrees
dAngle2 = // convert to degrees

delta = Math.Max(dAngle1, dAngle2) - Math.Min(dAngle1, dAngle2)
if (180 < delta) {
  delta = 360 - delta;
}

// convert delta to radians if you want
like image 28
Sam Axe Avatar answered Dec 28 '22 08:12

Sam Axe