diff --git a/java/src/com/android/inputmethod/keyboard/internal/GestureStroke.java b/java/src/com/android/inputmethod/keyboard/internal/GestureStroke.java index fc813a3d9..27a8e14f0 100644 --- a/java/src/com/android/inputmethod/keyboard/internal/GestureStroke.java +++ b/java/src/com/android/inputmethod/keyboard/internal/GestureStroke.java @@ -142,20 +142,21 @@ public class GestureStroke { mLastIncrementalBatchSize = size; } - private static float getDistance(final int p1x, final int p1y, - final int p2x, final int p2y) { - final float dx = p1x - p2x; - final float dy = p1y - p2y; + private static float getDistance(final int x1, final int y1, final int x2, final int y2) { + final float dx = x1 - x2; + final float dy = y1 - y2; // Note that, in recent versions of Android, FloatMath is actually slower than // java.lang.Math due to the way the JIT optimizes java.lang.Math. return (float)Math.sqrt(dx * dx + dy * dy); } - private static float getAngle(final int p1x, final int p1y, final int p2x, final int p2y) { - final int dx = p1x - p2x; - final int dy = p1y - p2y; + private static float getAngle(final int x1, final int y1, final int x2, final int y2) { + final int dx = x1 - x2; + final int dy = y1 - y2; if (dx == 0 && dy == 0) return 0; - return (float)Math.atan2(dy, dx); + // Would it be faster to call atan2f() directly via JNI? Not sure about what the JIT + // does with Math.atan2(). + return (float)Math.atan2((double)dy, (double)dx); } private static float getAngleDiff(final float a1, final float a2) { diff --git a/native/jni/src/geometry_utils.h b/native/jni/src/geometry_utils.h index 168542e45..deb042525 100644 --- a/native/jni/src/geometry_utils.h +++ b/native/jni/src/geometry_utils.h @@ -28,38 +28,36 @@ namespace latinime { -static inline float sqrf(float x) { +static inline float squareFloat(float x) { return x * x; } -static inline float getNormalizedSqrDistanceFloat(int x1, int y1, int x2, int y2, int scale) { - return sqrf(static_cast(x1 - x2) / static_cast(scale)) - + sqrf(static_cast(y1 - y2) / static_cast(scale)); +static inline float getNormalizedSquaredDistanceFloat(float x1, float y1, float x2, float y2, + float scale) { + return squareFloat((x1 - x2) / scale) + squareFloat((y1 - y2) / scale); } -static inline float getDistanceSqrFloat(float x1, float y1, float x2, float y2) { - return sqrf(x2 - x1) + sqrf(y2 - y1); +static inline float getSquaredDistanceFloat(float x1, float y1, float x2, float y2) { + return squareFloat(x1 - x2) + squareFloat(y1 - y2); +} + +static inline float getDistanceFloat(float x1, float y1, float x2, float y2) { + return hypotf(x1 - x2, y1 - y2); } static inline int getDistanceInt(int x1, int y1, int x2, int y2) { - return static_cast( - sqrtf(getDistanceSqrFloat( - static_cast(x1), static_cast(y1), - static_cast(x2), static_cast(y2)))); + return static_cast(getDistanceFloat(static_cast(x1), static_cast(y1), + static_cast(x2), static_cast(y2))); } static inline float getAngle(int x1, int y1, int x2, int y2) { const int dx = x1 - x2; const int dy = y1 - y2; - if (dx == 0 && dy == 0) { - return 0; - } - const float dxf = static_cast(dx); - const float dyf = static_cast(dy); - return atan2f(dyf, dxf); + if (dx == 0 && dy == 0) return 0; + return atan2f(static_cast(dy), static_cast(dx)); } -static inline float angleDiff(float a1, float a2) { +static inline float getAngleDiff(float a1, float a2) { const float diff = fabsf(a1 - a2); if (diff > M_PI_F) { return 2.0f * M_PI_F - diff; @@ -67,7 +65,7 @@ static inline float angleDiff(float a1, float a2) { return diff; } -// static float pointToLineDistanceSqrFloat( +// static float pointToLineSegSquaredDistanceFloat( // float x, float y, float x1, float y1, float x2, float y2) { // float A = x - x1; // float B = y - y1; @@ -76,7 +74,7 @@ static inline float angleDiff(float a1, float a2) { // return fabsf(A * D - C * B) / sqrtf(C * C + D * D); // } -static inline float pointToLineSegDistanceSqrFloat( +static inline float pointToLineSegSquaredDistanceFloat( float x, float y, float x1, float y1, float x2, float y2) { const float ray1x = x - x1; const float ray1y = y - y1; @@ -84,7 +82,7 @@ static inline float pointToLineSegDistanceSqrFloat( const float ray2y = y2 - y1; const float dotProduct = ray1x * ray2x + ray1y * ray2y; - const float lineLengthSqr = sqrf(ray2x) + sqrf(ray2y); + const float lineLengthSqr = squareFloat(ray2x) + squareFloat(ray2y); const float projectionLengthSqr = dotProduct / lineLengthSqr; float projectionX; @@ -99,7 +97,7 @@ static inline float pointToLineSegDistanceSqrFloat( projectionX = x1 + projectionLengthSqr * ray2x; projectionY = y1 + projectionLengthSqr * ray2y; } - return getDistanceSqrFloat(x, y, projectionX, projectionY); + return getSquaredDistanceFloat(x, y, projectionX, projectionY); } } // namespace latinime #endif // LATINIME_GEOMETRY_UTILS_H