如何確定給定點是否在邊界框內?確定點是否在邊界框內
我的觀點是48.847172,2.386597。
的BoundingBox:
"48.7998602295",
"48.8198640442",
"2.46138595581",
"2.48138619423"
如何確定給定點是否在邊界框內?確定點是否在邊界框內
我的觀點是48.847172,2.386597。
的BoundingBox:
"48.7998602295",
"48.8198640442",
"2.46138595581",
"2.48138619423"
做只是像往常一樣:
if(bb.ix <= p.x && p.x <= bb.ax && bb.iy <= p.y && p.y <= bb.ay) {
// Point is in bounding box
}
bb
是邊框,(ix,iy)
是其左上角的座標,(ax,ay)
其右下角的座標。其座標是p
,而其座標是(x,y)
。
CGRect和CGPoint有相當不錯的實用方法(假設你不介意使用CGFloat來存儲座標,並且看着你的值,你不需要:-))。
你可以那樣做:
// Create bounding box
CGRect area = CGRectMake(x, y, width, height);
// Define point
CGPoint point = CGPointMake(pX, pY);
/Check
BOOL isInside = CGRectContainsPoint(area, point);
該解決方案還需要考慮其中的UI發送箱橫跨經度180/-180(低縮放級別的地圖視圖的情況下,你可以看到整個世界,允許無限循環水平滾動,所以它可以例如,一個框的bottomLeft.lng = 170,而topRight.lng = -170(= 190),幷包括一個20度的範圍。
def inBoundingBox(bl/*bottom left*/: Coordinates, tr/*top right*/: Coordinates, p: Coordinates): Boolean = {
// in case longitude 180 is inside the box
val isLongInRange =
if (tr.long < bl.long) {
p.long >= bl.long || p.long <= tr.long
} else
p.long >= bl.long && p.long <= tr.long
p.lat >= bl.lat && p.lat <= tr.lat && isLongInRange
}
如果您使用傳單,您可以創建一個新的LatLngBounds
和利用其操作:
var bounds = new L.LatLngBounds(
new L.LatLng(gc.bbox['_northEast'].lat, gc.bbox['_northEast'].lng),
new L.LatLng(gc.bbox['_southWest'].lat, gc.bbox['_southWest'].lng));
return bounds.contains(new L.LatLng(pos.latitude, pos.longitude))
使用此功能對於C加上加一個點是否退出來檢查矩形內
struct Box{
Vec2 corner1;
Vec2 corner2;
Vec2 corner3;
Vec2 corner4;
};
bool boxContainsPoint(Vec2 point, Box box){
//Calculate distances from corner to corner
float abL = box.corner1.getDistance(box.corner2);////////////////////
float bcL = box.corner2.getDistance(box.corner3);////////////////////
float cdL = box.corner3.getDistance(box.corner4);////////////////////
float daL = box.corner4.getDistance(box.corner1);////////////////////
//Calculate corner touch distance//////////////////////////////////
float apL = box.corner1.getDistance(point);/////////////////////////
float bpL = box.corner2.getDistance(point);/////////////////////////
float cpL = box.corner3.getDistance(point);/////////////////////////
float dpL = box.corner4.getDistance(point);/////////////////////////
//Here we calculate the touch area
//Heron's Formula
///////////////////////////////////////////////////////////////////
float area1 = (abL + apL + bpL)/2;///////////////////////////////
float area2 = (bcL + bpL + cpL)/2;///////////////////////////////
float area3 = (cdL + cpL + dpL)/2;///////////////////////////////
float area4 = (daL + dpL + apL)/2;///////////////////////////////
float a1 = sqrtf(area1 * (area1 - abL)*(area1 - apL)*(area1 - bpL));
float a2 = sqrtf(area2 * (area2 - bcL)*(area2 - bpL)*(area2 - cpL));
float a3 = sqrtf(area3 * (area3 - cdL)*(area3 - cpL)*(area3 - dpL));
float a4 = sqrtf(area4 * (area4 - daL)*(area4 - dpL)*(area4 - apL));
//Calculate the rectangle area
float A = roundf(abL*bcL);
//Check to see if the point contains the polygon rect
if(A ==roundf(a1 + a2 + a3 + a4)) return true;
return false;
}
謝謝!這工作! – viniciusmo
這隻適用於軸對齊的邊界框! – mrueg
@mrueg沒有進一步限定,邊界框被理解爲軸對齊。請參閱mathopenref.com/coordbounds.html。此外,OP僅使用4個數字描述示例框。對於通用盒子,至少需要5個盒子。他顯然是指軸對齊的。 –