问题描述
我有一个线串"(带有初始化点和结束点)和一个点"(两个坐标).
I have a "linestring" (with init and end points) and a single "point" (two coordinates).
并且我实现了以下ActionSctipt
代码,以使用"haversine公式"来计算两个点之间的距离(每个点具有x和y坐标);此函数可以以"kms","meters","feets"或"miles"返回距离":
And I have implemented the following ActionSctipt
code to use "haversine formula" to calculate the distance between two points (each point has x & y coordinates); this function can return the "distance" in "kms", "meters", "feets" or "miles":
private function distanceBetweenCoordinates(lat1:Number, lon1:Number, lat2:Number, lon2:Number, units:String = "miles"):Number {
var R:int = RADIUS_OF_EARTH_IN_MILES;
if (units == "km") {
R = RADIUS_OF_EARTH_IN_KM;
}
if (units == "meters") {
R = RADIUS_OF_EARTH_IN_M;
}
if (units == "feet") {
R = RADIUS_OF_EARTH_IN_FEET;
}
var dLat:Number = (lat2 - lat1) * Math.PI / 180;
var dLon:Number = (lon2 - lon1) * Math.PI / 180;
var lat1inRadians:Number = lat1 * Math.PI / 180;
var lat2inRadians:Number = lat2 * Math.PI / 180;
var a:Number = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1inRadians) * Math.cos(lat2inRadians);
var c:Number = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
var d:Number = R * c;
return d;
}
此代码运行良好.但是我需要改进此代码,以允许计算单点"和一个线串"(有2个点)之间的最小距离.我该怎么办?
This code is functioning well. But I need to improving this code to allow calculate the minimum distance between a "single point" and one "linestring" (with 2 points).How can I do?
我认为这种解决方案:*对每个点(初始和结束)除以划线" ...,并为每个点计算到单点"的距离...在得到两个距离"后,返回最小距离.下图说明了该解决方案的更好之处:
I thought this solution:* Divide the "linesting" for each point (Init and end)... and for each of these calculate the distance to the "single point"... and after I getting both "distances" return the minimum distance.This solution is not the better, this is explained in the following image:
"d1"和"d2"距离无效...因为只有"d0"是有效距离.
"d1" and "d2" distances are invalid... because only "d0" is the valid distance.
请!帮我!!!如何改进Haversine公式以计算线与单点之间的距离(以公里为单位)?
Please! help me!!! How can I improve the haversine formula to calculate the distance between a line and a single point in kilometres?
谢谢!!!!
推荐答案
在您的情况下,d0
距离是三角形的高度.是Hb=2*A/b
,其中A
-区域& b
-基本边的长度(您的线串).
In your case d0
distance is a height of triangle. It's Hb=2*A/b
where A
- Area & b
-length of the base side (your linestring).
如果给定3个点,则可以计算它们之间的距离(三角形的边a
,b
,c
).它将允许您计算三角形面积:A=sqrt(p*(p-a)*(p-b)*(p-c))
,其中p
是半周长:p=(a+b+c)/2
.因此,现在您已经拥有计算距离Hb
(您的"d0")所需的所有变量.
If given 3 points you can calculate the the distances between them (sides a
, b
, c
of triangle). It will allow you to calculate triangle Area: A=sqrt(p*(p-a)*(p-b)*(p-c))
where p
is half perimeter: p=(a+b+c)/2
. So, now u have all variables u need to calculate the distance Hb
(your "d0").
这篇关于计算“线"与“线"之间的最小距离.和一个“点"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!