aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/helper
diff options
context:
space:
mode:
Diffstat (limited to 'src/org/traccar/helper')
-rw-r--r--src/org/traccar/helper/DistanceCalculator.java19
1 files changed, 18 insertions, 1 deletions
diff --git a/src/org/traccar/helper/DistanceCalculator.java b/src/org/traccar/helper/DistanceCalculator.java
index 3452597ab..88d4ef8a4 100644
--- a/src/org/traccar/helper/DistanceCalculator.java
+++ b/src/org/traccar/helper/DistanceCalculator.java
@@ -1,5 +1,6 @@
/*
- * Copyright 2014 Anton Tananaev (anton@traccar.org)
+ * Copyright 2014 - 2016 Anton Tananaev (anton@traccar.org)
+ * Copyright 2016 Andrey Kunitsyn (andrey@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -33,4 +34,20 @@ public final class DistanceCalculator {
return d * 1000;
}
+ public static double distanceToLine(
+ double pointLat, double pointLon, double lat1, double lon1, double lat2, double lon2) {
+ double d0 = distance(pointLat, pointLon, lat1, lon1);
+ double d1 = distance(lat1, lon1, lat2, lon2);
+ double d2 = distance(lat2, lon2, pointLat, pointLon);
+ if (Math.pow(d0, 2) > Math.pow(d1, 2) + Math.pow(d2, 2)) {
+ return d2;
+ }
+ if (Math.pow(d2, 2) > Math.pow(d1, 2) + Math.pow(d0, 2)) {
+ return d0;
+ }
+ double halfP = (d0 + d1 + d2) * 0.5;
+ double area = Math.sqrt(halfP * (halfP - d0) * (halfP - d1) * (halfP - d2));
+ return 2 * area / d1;
+ }
+
}