Determine if point is within bounding box

后端 未结 7 1926
没有蜡笔的小新
没有蜡笔的小新 2020-12-08 11:02

How would you determine if a given point is within the bounding box?

My point is 48.847172 , 2.386597.

Boundingbox:

    \"48.7998602295\",
           


        
7条回答
  •  情歌与酒
    2020-12-08 11:55

    This answer is identical to kumetix's answer above; however, for the life of me, I didn't understand what was happening in there since it was condensed quite a bit. Here is the same answer with detailed explanation. Also please note that the answer is in Kotlin as opposed to JavaScript as the original post requested but it is sufficiently readable so that converting to your language shouldn't be too bad.

    First define Coordinate2D and BoundingBox classes:

    data class Coordinate2D (val latitude: Double, val longitude: Double)
    
    data class BoundingBox (val north: Double, val east: Double, val south: Double, val west: Double)
    

    Here is the function that determines whether a point is in an arbitrary bounding box

    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
    }
    

提交回复
热议问题