diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2016-11-27 22:58:35 +1300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-11-27 22:58:35 +1300 |
commit | 58db487ece3ac8644557145a07a75ce4bf63a344 (patch) | |
tree | 29f18d871486df89c202e32a82fe143acc3754f7 /src/org/traccar/helper | |
parent | f1b58491f6dc2a50312efb19f501d2121ed06b08 (diff) | |
parent | f933147c0cba53df96448a64a2ba58dda4da1659 (diff) | |
download | trackermap-server-58db487ece3ac8644557145a07a75ce4bf63a344.tar.gz trackermap-server-58db487ece3ac8644557145a07a75ce4bf63a344.tar.bz2 trackermap-server-58db487ece3ac8644557145a07a75ce4bf63a344.zip |
Merge pull request #2608 from Abyss777/polyline_geofence
Implemented polyline geofence
Diffstat (limited to 'src/org/traccar/helper')
-rw-r--r-- | src/org/traccar/helper/DistanceCalculator.java | 19 |
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; + } + } |