javascript 如何检测点是否在圆内?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14598426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to detect if a point is in a Circle?
提问by Michael Schade
How can I test if a LatLng point is within the bounds of a circle? (Google Maps JavaScript v3)
如何测试 LatLng 点是否在圆的边界内?(谷歌地图 JavaScript v3)
The getBounds() method returns the bounding box for the circle, which is a rectangle, so if a point falls outside the circle but within the bounding box, you'll get the wrong answer.
getBounds() 方法返回圆的边界框,它是一个矩形,所以如果一个点落在圆外但在边界框内,你会得到错误的答案。
回答by geocodezip
Use the spherical geometry library(be sure to include it with the API)
使用球面几何库(务必将其包含在 API 中)
function pointInCircle(point, radius, center)
{
return (google.maps.geometry.spherical.computeDistanceBetween(point, center) <= radius)
}
回答by sheu
You could just do the distance comparison manually, fairly trivially.
您可以相当简单地手动进行距离比较。
(x1 - x2)^2 + (y1 - y2)^2 <= D^2
回答by Riccardo Zorn
You might use the Circle object to show it;
您可以使用 Circle 对象来显示它;
new google.maps.Circle({
map : map,
center : new google.maps.LatLng(lat,lng),
strokeColor:'#00FFCC',
strokeWeight:2,
fillOpacity:0,
radius:radiusm
});
And apply the Pythagorean theorem to coordinates: but in this case to make it a "real" circle since the ration between 1° of lat and longitude varies across latitudes, you should at the very least adjust them like:
并将勾股定理应用于坐标:但在这种情况下,为了使其成为“真正的”圆,因为纬度和经度的 1° 之间的比率因纬度而异,您至少应该像这样调整它们:
var kmRadius = 100; //(radius of 100 km)
var lat_gap = kmRadius/111.1;
var lng_gap = lat_gap / Math.cos(lat / (Math.PI/180));
回答by lumiera
Something like this should do the trick (code not tested):
这样的事情应该可以解决问题(代码未经测试):
public boolean pointInCircle(Circle c, LatLng coord) {
Rectangle r = c.getBounds();
double rectX = r.getX();
double rectY = r.getY();
double rectWidth = r.getWidth();
double rectHeight = r.getHeight();
double circleCenterX = rectX + rectWidth/2;
double circleCenterY = rectY + rectHeight/2;
double lat = coord.getLatitude();
double lon = coord.getLongitude();
// Point in circle if (x?h)^2 + (y?k)^2 <= r^2
double rSquared = Math.pow(rectWidth/2, 2);
double point = Math.pow(lat - circleCenterX, 2) + Math.pow(lon - circleCenterY, 2);
return (point <= rSquared) ? true : false;
}
回答by rekire
Why don't you simple calculate this with Pythagorean theorem? You know a2+b2=c2. If c is lower than r (radius) you know it is inside.
你为什么不简单地用勾股定理计算这个?你知道a2+b2=c2。如果 c 小于 r(半径),您就知道它在里面。
var isInside=Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2) >= r*r;
回答by Nguyen Chi Thanh
Try this (Javascript):
试试这个(Javascript):
const toRadians = (val) => {
return val * Math.PI / 180;
}
const toDegrees = (val) => {
return val * 180 / Math.PI;
}
// Calculate a point winthin a circle
// circle ={center:LatLong, radius: number} // in metres
const pointInsideCircle = (point, circle) => {
let center = circle.center;
let distance = distanceBetween(point, center);
return distance < circle.radius; // Use '<=' if you want to get all points in the border
};
const distanceBetween = (point1, point2) => {
var R = 6371e3; // metres
var φ1 = toRadians(point1.latitude);
var φ2 = toRadians(point2.latitude);
var Δφ = toRadians(point2.latitude - point1.latitude);
var Δλ = toRadians(point2.longitude - point1.longitude);
var a = Math.sin(Δφ / 2) * Math.sin(Δφ / 2) +
Math.cos(φ1) * Math.cos(φ2) *
Math.sin(Δλ / 2) * Math.sin(Δλ / 2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return R * c;
}
References: http://www.movable-type.co.uk/scripts/latlong.html
参考资料:http: //www.movable-type.co.uk/scripts/latlong.html
This npm helper module does the same thing and returns a boolean as to whether the item is in the circle.
这个 npm helper 模块做同样的事情,并返回一个关于项目是否在圆圈中的布尔值。