Skip to main content
added 52 characters in body
Source Link
bennedich
  • 12.4k
  • 6
  • 36
  • 42

This gives a signed angle for any angles:

a = targetA - sourceA a = (a + 180) % 360 - 180 

Beware in many languages the modulo operation returns a value with the same sign as the dividend (like C, C++, C#, JavaScript, full list here). This requires a custom mod function like so:

mod = (a, n) -> a - floor(a/n) * n 

Or so:

mod = (a, n) -> (a % n + n) % n 

If angles are within [-180, 180] this also works:

a = targetA - sourceA a += (a>180) ? -360 : (a<-180) ? 360 : 0 

In a more verbose way:

a = targetA - sourceA a -= 360 if a > 180 a += 360 if a < -180 

This gives a signed angle for any angles:

a = targetA - sourceA a = (a + 180) % 360 - 180 

Beware in many languages the modulo operation returns a value with the same sign as the dividend (like C, C++, C#, JavaScript, full list here). This requires a custom mod function like so:

mod = (a, n) -> (a % n + n) % n 

If angles are within [-180, 180] this also works:

a = targetA - sourceA a += (a>180) ? -360 : (a<-180) ? 360 : 0 

In a more verbose way:

a = targetA - sourceA a -= 360 if a > 180 a += 360 if a < -180 

This gives a signed angle for any angles:

a = targetA - sourceA a = (a + 180) % 360 - 180 

Beware in many languages the modulo operation returns a value with the same sign as the dividend (like C, C++, C#, JavaScript, full list here). This requires a custom mod function like so:

mod = (a, n) -> a - floor(a/n) * n 

Or so:

mod = (a, n) -> (a % n + n) % n 

If angles are within [-180, 180] this also works:

a = targetA - sourceA a += (a>180) ? -360 : (a<-180) ? 360 : 0 

In a more verbose way:

a = targetA - sourceA a -= 360 if a > 180 a += 360 if a < -180 
A proper modulo function for languages lacking this feature.
Source Link
bennedich
  • 12.4k
  • 6
  • 36
  • 42

This gives a signed angle for any angles:

a = targetA - sourceA a = (a + 180) % 360 - 180 

Beware in many languages the modulo operation returns a value with the same sign as the dividend (like C, C++, C#, JavaScript, full list here). This requires a custom mod function like so:

mod = (a, n) -> (a % n + n) % n 

If angles are within [-180, 180] this also works:

a = targetA - sourceA a += (a>180) ? -360 : (a<-180) ? 360 : 0 

In a more verbose way:

a = targetA - sourceA a -= 360 if a > 180 a += 360 if a < -180 

This gives a signed angle for any angles:

a = targetA - sourceA a = (a + 180) % 360 - 180 

If angles are within [-180, 180] this also works:

a = targetA - sourceA a += (a>180) ? -360 : (a<-180) ? 360 : 0 

In a more verbose way:

a = targetA - sourceA a -= 360 if a > 180 a += 360 if a < -180 

This gives a signed angle for any angles:

a = targetA - sourceA a = (a + 180) % 360 - 180 

Beware in many languages the modulo operation returns a value with the same sign as the dividend (like C, C++, C#, JavaScript, full list here). This requires a custom mod function like so:

mod = (a, n) -> (a % n + n) % n 

If angles are within [-180, 180] this also works:

a = targetA - sourceA a += (a>180) ? -360 : (a<-180) ? 360 : 0 

In a more verbose way:

a = targetA - sourceA a -= 360 if a > 180 a += 360 if a < -180 
Rollback to Revision 2
Source Link
bennedich
  • 12.4k
  • 6
  • 36
  • 42

This gives a signed angle for any angles:

anglea = targetAngletargetA - sourceAnglesourceA anglea = (anglea + 540180) % 360 - 180 

If angles are within [-180, 180] this also works:

anglea = targetAngletargetA - sourceAnglesourceA anglea += (angle > 180a>180) ? -360 : ((angle < a<-180) ? 360 : 0) 

In a more verbose way:

anglea = targetAngletargetA - sourceAnglesourceA anglea -= 360 if anglea > 180 anglea += 360 if anglea < -180 

This gives a signed angle for any angles:

angle = targetAngle - sourceAngle angle = (angle + 540) % 360 - 180 

If angles are within [-180, 180] this also works:

angle = targetAngle - sourceAngle angle += (angle > 180) ? -360 : ((angle < -180) ? 360 : 0) 

In a more verbose way:

angle = targetAngle - sourceAngle angle -= 360 if angle > 180 angle += 360 if angle < -180 

This gives a signed angle for any angles:

a = targetA - sourceA a = (a + 180) % 360 - 180 

If angles are within [-180, 180] this also works:

a = targetA - sourceA a += (a>180) ? -360 : (a<-180) ? 360 : 0 

In a more verbose way:

a = targetA - sourceA a -= 360 if a > 180 a += 360 if a < -180 
Rollback to Revision 3
Source Link
bennedich
  • 12.4k
  • 6
  • 36
  • 42
Loading
Noticed a fault in my previous edit when (targetAngle - sourceAngle + 540) is smaller than 0 (would return an angle < -180). Sorry.
Source Link
Loading
Fixed first example to work with any angle and both types of modulo, and always return an angle within [-180, 180]. Also prettified code because couldn't edit less than 6 characters.
Source Link
Loading
Better solution for arbitrary angles
Source Link
bennedich
  • 12.4k
  • 6
  • 36
  • 42
Loading
Source Link
bennedich
  • 12.4k
  • 6
  • 36
  • 42
Loading