Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/153.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 确定点是否在边界框内_Javascript_Ios_Google Maps_Google Maps Api 3_Maps - Fatal编程技术网

Javascript 确定点是否在边界框内

Javascript 确定点是否在边界框内,javascript,ios,google-maps,google-maps-api-3,maps,Javascript,Ios,Google Maps,Google Maps Api 3,Maps,如何确定给定点是否在边界框内 我的观点是48.847172,2.386597 边界框: "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

如何确定给定点是否在边界框内

我的观点是48.847172,2.386597

边界框:

    "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
}

if(bb.ix对于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);

此解决方案还考虑了这样一种情况,即用户界面发送一个穿过经度180/-180的框(在低缩放级别上映射视图,您可以看到整个世界,允许无限循环水平滚动,因此,例如,框的左下角.lng=170而右上角.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
}
def-inBoundingBox(bl/*左下*/:Coordinates,tr/*右上*/:Coordinates,p:Coordinates):布尔={
//如果经度180在盒子里
瓦利斯隆金朗格酒店=
如果(tr.longp、 long>=bl.long | | p.long=bl.long&&p.long=bl.lat&&p.lat如果使用传单,可以创建新的
LatLngBounds
并使用其
contains()
操作:

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 plus使用此函数检查矩形内是否存在点

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;

}

这个答案与相同;但是,就我个人而言,我不明白那里发生了什么,因为它被压缩了很多。这里是同样的答案,并有详细的解释。另外请注意,答案是Kotlin,而不是原始帖子要求的JavaScript,但它足够可读,因此你的语言不应该太糟糕

首先定义坐标2D和边界框类:

data class Coordinate2D (val latitude: Double, val longitude: Double)

data class BoundingBox (val north: Double, val east: Double, val south: Double, val west: Double)
此函数用于确定点是否位于任意边界框中

fun isPointInBoundingBox(point: Coordinate2D, boundingBox: BoundingBox): Boolean {
    //given the bounding box is an imaginary rectangle in a coordinate system
    //bounding box has 4 sides - northLine, eastLine, southLine and westLine
    //initially assume the point is not in our bounding box of interest as such:
    var isPointEastOfWestLine = false
    var isPointWestOfEastLine = false
    var isPointSouthOfNorthLine = false
    var isPointNorthOfSouthLine = false

    if (boundingBox.east < boundingBox.west) {
        //if west coordinate has a bigger value than the east, 
        //the bounding box must be crossing the dateline
        //in other words, longitude 180 is inside the box
        //let's see what's happening with the point
        if (point.longitude >= boundingBox.west) {
            //imagine a bounding box where westernmost longitude is +170 and easternmost longitude is -170
            //if the point in question has a latitude of +171 as in the case expressed in the if
            //statement, then we can conclude that point lies east of the west line
            isPointEastOfWestLine = true

            //we can also infer that the point must lie west of east line because the point's longitude is positive
            //therefore, the point's position must be to the west of the easternmost longitude of the bounding box
            isPointWestOfEastLine = true
        }

        if (point.longitude <= boundingBox.east) {
            //imagine a bounding box where westernmost longitude is +170 and easternmost longitude is -170
            //if the point in question has a latitude of -171 as in the case expressed in the if
            //statement, then we can conclude that point lies west of the east line
            isPointWestOfEastLine = true

            //we can also infer that the point must lie east of the west line because the point's longitude is negative
            //therefore, the point's position must be to the east of the westernmost longitude of the bounding box
            isPointEastOfWestLine = true
        }
    } else {
        //in the else case, bounding box does not cross the dateline, so comparisons are more straightforward
        //longitudes range from -180 to +180; therefore, western side of a bounding box will always
        //have lower value than eastern side
        if (point.longitude >= boundingBox.west) {
            //in this case, point's longitude has a higher value than the west side of the bounding box
            //we can conclude that point lies to the east of the west line of the bounding box
            isPointEastOfWestLine = true
        }
        if (point.longitude <= boundingBox.east) {
            //in this case, point's longitude has a lower value than the east side of the bounding box
            //we can conclude that point lies to the east of the west line of the bounding box
            isPointWestOfEastLine = true
        }
    }

    //comparing latitudes are little bit easier. latitude values range from -90 to +90 where
    //-90 is the southern pole and +90 is the northern pole. The more north you go, higher the values.
    if (point.latitude >= boundingBox.south) {
        //point's latitude is higher, therefore, point must lie to the north of the south line
        isPointNorthOfSouthLine = true
    }

    if (point.latitude <= boundingBox.north) {
        //point's latitude is higher, therefore, point must lie to the north of the south line
        isPointSouthOfNorthLine = true
    }

    return isPointEastOfWestLine &&
            isPointWestOfEastLine &&
            isPointNorthOfSouthLine &&
            isPointSouthOfNorthLine
}
fun isPointInBoundingBox(点:坐标2d,边界框:边界框):布尔值{
//给定边界框是坐标系中的一个虚构矩形
//边界框有4条边-北线、东线、南线和西线
//首先假设该点不在我们感兴趣的边界框中:
var isPointEastOfWestLine=false
var isPointWestOfEastLine=false
var IsPointsSouthOfNorthline=错误
var isPointNorthOfSouthLine=false
if(boundingBox.east=boundingBox.west){
//想象一个边界框,其中最西边的经度为+170,最东边的经度为-170
//如果问题点的纬度为+171,如if中所示
//声明,那么我们可以得出结论,该点位于西线以东
isPointEastOfWestLine=真
//我们还可以推断该点必须位于东线以西,因为该点的经度为正
//因此,点的位置必须位于边界框最东端经度的西侧
isPointWestOfEastLine=真
}
if(point.longitude=boundingBox.west){
//在这种情况下,点的经度的值高于边界框的西侧
//我们可以得出结论,该点位于边界框的西线以东
isPointEastOfWestLine=真
}
如果(point.longitude这应该更快

function doesPointCollide(p,box) {
    return !(p.x < box.left || p.x > box.right || p.y > box.bottom || p.y < box.top)
}
函数doesPointCollide(p,box){

返回!(p.xbox.right | | p.y>box.bottom | | p.y

如果点在任何尺寸之外,我们知道它不在边界框中,否则它在边界框中,因此我们可以更快地忽略否定的情况。

这只适用于轴对齐的边界框!@mrueg无需进一步限定,边界框被理解为轴对齐。例如,请参阅mathopenref.com/coordbounds.html。此外,OP仅使用4个数字描述示例框。对于一般框,至少需要5个数字。他显然指的是轴对齐的框。还包括左下角位于正经度范围内的场景(0任何半体面的编译器都会生成与普通逻辑转换完全相同的代码。即使它没有生成(也许我们说的是一个没有按需编译的解释器),差异也会非常小,以至于每个人都应该选择最易维护的(即可读的)版本。一般来说,我同意你的观点。但是,想象一个场景,该函数以DOM创建mousemove事件的速率启动,每个事件循环一次,可能每秒数百或数千次;在这种情况下,这里的优化会有所帮助。“p.y>box.bottom | | p.ybox.top”