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 continuity throws me away.

See this for a better understanding.

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

The thing is to create a cone from the center to a given angle. Essentially, I will evaluate:

if(DistanceFromAngle1 < pi/4 [45°]) { Angle2 is part of cone } 
+4
source share
5 answers

If by distance you mean a straight line connecting two intercept points, you can calculate the distance by doing the following:

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

SQRT = square root

ABS = Absolute Value

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

 var angle = ABS(A - B) if(angle > pi) angle = 2*pi - angle return angle 
+4
source
 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 
+3
source

π / 2 is 90 °, not 45 °. I'm going to suggest that you want to know if angle 2 is in an interval centered around angle 1, which extends 45 ° from it in both directions.

You can take the difference between angle 2 and angle 1 and decrease modulo 2π until the difference is in [-π, π). This will give the sign distance between angle 2 and angle 1. Then check if it is (-π / 4, π / 4). Since the value returned by atan2 is always between -π and π, the original difference will always be between -2π and 2π, so you can break it all down into one check:

  if (angle2 - angle1 < -7π/4 || (angle2 - angle1 > -π/4 && angle2 - angle1 < π/4) || angle2 - angle1 > 7π/4) { angle2 is less than 45° away from angle1 } 
+1
source

I want to give an alternative solution: you can use vector math. This can be useful, for example, if you have two vectors as a starting point (which seems to be your case).

Given the two normalized vectors a and b , the angle between them just becomes acos(dot(a, b)) = acos(ax*bx + ay*by) .

To get the normalized vector at an angle, alpha , you can use a = vec2(cos(alpha), sin(alpha)) , for example.

To normalize a denormal vector, use na = a / length(a) = a / sqrt(dot(a, a)) .

0
source

EDIT: I'm not sure if this will work. I translated it from some python code, but I'm not sure if divmod(radians, math.pi*2)[1] is the same behavior as System.Math.IEEERemainder(radians, Math.PI*2.0) . Need to check ...

EDIT2: I think using% is correct

EDIT3: Blah, this is not true because it returns a negative value for negative numbers. Does anyone know how to get divmod in python in C #?

How to calculate the angle between two angles:

 public static double NormalizeAngle(double radians) { return fmod(radians,Math.PI*2.0); # this method doesn't exist, see above } public static double ArcLength(double radians1, double radians2) { radians1 = NormalizeAngle(radians1); radians2 = NormalizeAngle(radians2); return Math.Min(NormalizeAngle(radians1 - radians2, NormalizeAngle(radians2 - radians1)); } 

How it works, he tries to get around both ways, all mod 2pi calculations, and he chooses one with a shorter distance.

0
source

All Articles