Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Compass heading from deviceorientation
Message
From
24/04/2014 09:47:47
 
 
General information
Forum:
HTML5
Category:
Browser issues
Miscellaneous
Thread ID:
01598938
Message ID:
01598966
Views:
26
>>>Just varying either the x or y axis gives hugely different heading results (including at 'steep' angles Nan)
>>>
>>
>>
>>
>>  // Calculate compass heading
>>    var compassHeading = Math.atan(Vx / Vy);
>>
>>
>>Have you tried replacing the Math.atan()) by the Math.atan2() ?
>
>No, but I will. My trig's too rusty to guess what the effect might be :-}
>
>>http://www.w3schools.com/jsref/jsref_atan2.asp


Haven't tested this in javascript but

You know that the sign of the tangent is positive in quadrants I and III, negative in quandrants II and IV

In the example below for 225 degrees ( = 180 + 45)

The angle returned by atan() is 45 degrees
The angle returned by atan2() is 225 degrees

Atan does not know the exact quadrant whilst atan2 does since you pass both sine and cosine (here both are negative)
class Test_atan
	{
		internal static void Go()
		{
			// 180 + 45= 225 degrees
			double rads =  Math.PI + Math.PI / 4.0; 

			double sin = Math.Sin(rads);
			double cos = Math.Cos(rads);

			double atanDegrees = RadianToDegree(Math.Atan(sin / cos));
			double atan2Degrees = RadianToDegree(Math.Atan2(sin, cos));

			Console.WriteLine("{0}  {1} ", atanDegrees, atan2Degrees);

		}
		static double DegreeToRadian(double degrees)
		{
			return Math.PI * Modulo(degrees, 360.00) / 180.0;

		}
		static double RadianToDegree(double rads)
		{
			return Modulo(rads, Math.PI * 2) * (180.0 / Math.PI);
		}
		static double Modulo(double dividend, double divisor)
		{
			return (dividend % divisor)
				+ (Math.Sign(dividend) * Math.Sign(divisor) >= 0 ? 0 : divisor);
		}
	}
Gregory
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform