Well I found a way to do it about 50% faster but it's less accurate.
The margin of error can be up to 4 degrees, but if speed is important
and accuracy doesn't need to be pin point, this will work.
private static final float ATAN2_CF1 = (float) (3.1415927f / 4f);
private static final float ATAN2_CF2 = 3f * ATAN2_CF1;
/**
* This is 50% faster than Math.atan2 in the emulator, but can have
an error of up to 4 degrees.
*/
private static float fastatan2(float y, float x) {
float abs_y = Math.abs(y);
float angle;
if (x >= 0) {
float r = (x - abs_y) / (x + abs_y);
angle = ATAN2_CF1 - ATAN2_CF1 * r;
} else {
float r = (x + abs_y) / (abs_y - x);
angle = ATAN2_CF2 - ATAN2_CF1 * r;
}
return y < 0 ? -angle : angle;
}
On May 24, 9:57 am, Robert Green <[email protected]> wrote:
> Using arc tangents for interpreting accelerometers seems to be a
> pretty easy way to go to get a desired result. I just benchmarked
> (float)Math.atan2(x,y) and it's a very slow function. I checked
> FloatMath for Android 1.0 SDK and it has sin and cos but no tan or
> atan. Is there a faster way to do this?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---