简体   繁体   English

确定坐标是否在边界框内

[英]Determine if coordinates is within bounding box

I need to create a function that, given a , b and c returns a boolean indicating if c is within a and b . I need to create a function that, given a , b and c returns a boolean indicating if c is within a and b .

All variables have the following type:所有变量都具有以下类型:

type Coordinate = {
  lat: number;
  lon: number;
};

I've came up with a solution that initially I though was correctly, but after testing with Google Maps, I find out it's wrong.我想出了一个最初我认为是正确的解决方案,但在使用谷歌地图进行测试后,我发现它是错误的。

The function: function:

function inBoundingBox(
  bottomLeft: Coordinate,
  topRight: Coordinate,
  point: Coordinate
) {
  let isLongInRange: boolean;
  if (topRight.lon < bottomLeft.lon) {
    isLongInRange = point.lon >= bottomLeft.lon || point.lon <= topRight.lon;
  } else {
    isLongInRange = point.lon >= bottomLeft.lon && point.lon <= topRight.lon;
  }
  return (
    point.lat >= bottomLeft.lat && point.lat <= topRight.lat && isLongInRange
  );
}

One example that should work:一个应该起作用的例子:

const topRight: Coordinate = {
  lat: -23.5273,
  lon: -46.833881
};

const bottomLeft: Coordinate = {
  lat: -23.537519,
  lon: -46.840019
};

const point = {
  lat: -23.52785,
  lon: -46.840545
};

const result = inBoundingBox(bottomLeft, topRight, point);
console.log(result) // false, where should be true.

And a visual representation is here .这里有一个视觉表示。

I need help to find out where exactly the code is wrong, and how to fix it.我需要帮助来找出代码到底哪里错了,以及如何修复它。

I also tried to use Leaflet to see if it works, but the result is the same:我也试过用 Leaflet 看看是否有效,但结果是一样的:

function leafletContains(bottomLeft, topRight, pos) {
  var bounds = new L.LatLngBounds(
    new L.LatLng(bottomLeft.lat, bottomLeft.lon),
    new L.LatLng(topRight.lat, topRight.lon)
  );
  return bounds.contains(new L.LatLng(pos.lat, pos.lon));
}

leafLetContains({ lat: -23.537519, lon: -46.840019 }, { lat: -23.5273, lon: -46.833881 }, { lat: -23.527811, lon: -46.840201 }) // false, where should be true.

this line is wrong这条线是错误的

else {
  isLongInRange = point.lon >= bottomLeft.lon && point.lon <= topRight.lon;
}

Has to be ||必须是|| instead of && and one of the following: switch around the >= and <= or switch palces of bottomLeft.lon and topRight.lon而不是&&和以下之一:切换>=<=或切换bottomLeft.lontopRight.lon

else {
  isLongInRange = point.lon <= bottomLeft.lon || point.lon >= topRight.lon;
}

A bounding box test must check the four sides of the box.边界框测试必须检查框的四个边。

A box in the surface of a sphere is not a rectangle, so it's hard to work with x,y coordinates.球体表面中的框不是矩形,因此很难使用 x,y 坐标。 But with "polar" coordinates (lat, lon) it's a lot easy:但是使用“极”坐标(纬度,经度)很容易:

I'm not a javascript coder, so forgive my mistakes in this code:我不是 javascript 编码器,所以请原谅我在此代码中的错误:

function inBoundingBox(
  bottomLeft: Coordinate,
  topRight: Coordinate,
  point: Coordinate
) {
  let isLongInRange: boolean;
  let isLatiInRange: boolean;
  isLongInRange = point.lon >= bottomLeft.lon && point.lon <= topRight.lon;
  isLatiInRange = point.lat >= bottomLeft.lat && point.lat <= topRight.lat;
  return ( isLongInRange && isLatiInRange );
}

assuming bottomLeft.lon < topRight.lon and bottomLeft.lat < topRight.lat假设bottomLeft.lon < topRight.lonbottomLeft.lat < topRight.lat

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM