summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/utils/MathUtils.java
diff options
context:
space:
mode:
Diffstat (limited to 'platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/utils/MathUtils.java')
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/utils/MathUtils.java59
1 files changed, 59 insertions, 0 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/utils/MathUtils.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/utils/MathUtils.java
index 0c90e4b244..fbcf9afb28 100644
--- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/utils/MathUtils.java
+++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/utils/MathUtils.java
@@ -1,5 +1,8 @@
package com.mapbox.mapboxsdk.utils;
+import android.graphics.PointF;
+import android.support.annotation.Nullable;
+
// TODO Remove this class if we finally include it within MAS 3.x (GeoJSON)
public class MathUtils {
@@ -46,4 +49,60 @@ public class MathUtils {
return secondMod + min;
}
+
+ /**
+ * Look for the intersection point of two segments, if exist.
+ *
+ * @param point0 first point of the first segment
+ * @param point1 second point of the first segment
+ * @param point2 first point of the second segment
+ * @param point3 second point of the second segment
+ * @return intersection point, if exists
+ */
+ @Nullable
+ public static PointF findSegmentsIntersection(PointF point0, PointF point1, PointF point2, PointF point3) {
+ // Java port of one of the algorithms discussed in https://stackoverflow.com/questions/563198/
+ float dx1 = point1.x - point0.x;
+ float dy1 = point1.y - point0.y;
+ float dx2 = point3.x - point2.x;
+ float dy2 = point3.y - point2.y;
+ float dx3 = point0.x - point2.x;
+ float dy3 = point0.y - point2.y;
+
+ float d = dx1 * dy2 - dx2 * dy1;
+
+ if (d != 0) {
+ float s = dx1 * dy3 - dx3 * dy1;
+ if ((s <= 0 && d < 0 && s >= d) || (s >= 0 && d > 0 && s <= d)) {
+ float t = dx2 * dy3 - dx3 * dy2;
+ if ((t <= 0 && d < 0 && t > d) || (t >= 0 && d > 0 && t < d)) {
+ t = t / d;
+ return new PointF(point0.x + t * dx1, point0.y + t * dy1);
+ }
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Returns a distance between two points.
+ *
+ * @param point0 first point
+ * @param point1 second point
+ * @return distance in pixels
+ */
+ public static float distance(PointF point0, PointF point1) {
+ return (float) Math.sqrt(Math.pow(point1.x - point0.x, 2) + Math.pow(point1.y - point0.y, 2));
+ }
+
+ /**
+ * Rounds a number.
+ * @param value original value
+ * @param precision expected precision
+ * @return rounded number
+ */
+ public static double round(double value, int precision) {
+ int scale = (int) Math.pow(10, precision);
+ return (double) Math.round(value * scale) / scale;
+ }
}