Determine if point is within bounding box

后端 未结 7 1945
没有蜡笔的小新
没有蜡笔的小新 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:49

    This solution also takes in consideration a case in which the UI sends a box which crosses longitude 180/-180 (maps views on low zoom level where you can see the whole world, allow infinite cyclic horizontal scrolling, so it is possible for example that a box's bottomLeft.lng=170 while topRight.lng=-170(=190) and by that including a range of 20 degrees.

    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
    }
    

提交回复
热议问题