最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Check if point is inside circle - Stack Overflow

programmeradmin8浏览0评论

I have a GPS Coordinate of a spot called spotCoordinates and a center the a circle coordinates this.state.center and a radius in kilometers this.state.radius

I'm trying to make a method to check if the spotCoordinates is inside a circle but I don't know how I can add the radius to the coordinates and how to check if it's actually inside or not. It would be easier if it was a square.

 calculateParkingSpotsInTheArea = () => {
      this.state.parkingSpots.map(spot => {
          let spotCoordinates = spot.coordinates;
          console.log(spotCoordinates, this.state.center, this.state.radius);
          // Calculate if the parking spot is inside the circle
      });
  }

E.g Values printed in the console spotCoordinates = [41.5408446218337, -8.612296123028727] center = {lat: 41.536558, lng: -8.627487} radius = 25

Any help?

I have a GPS Coordinate of a spot called spotCoordinates and a center the a circle coordinates this.state.center and a radius in kilometers this.state.radius

I'm trying to make a method to check if the spotCoordinates is inside a circle but I don't know how I can add the radius to the coordinates and how to check if it's actually inside or not. It would be easier if it was a square.

 calculateParkingSpotsInTheArea = () => {
      this.state.parkingSpots.map(spot => {
          let spotCoordinates = spot.coordinates;
          console.log(spotCoordinates, this.state.center, this.state.radius);
          // Calculate if the parking spot is inside the circle
      });
  }

E.g Values printed in the console spotCoordinates = [41.5408446218337, -8.612296123028727] center = {lat: 41.536558, lng: -8.627487} radius = 25

Any help?

Share Improve this question asked Jan 7, 2020 at 18:09 user12361681user12361681 1073 silver badges9 bronze badges 7
  • This thread might be useful for you stackoverflow./questions/14560999/… – user3783243 Commented Jan 7, 2020 at 18:10
  • Use the Pythagorean Theorem to find the distance from the circle's midpoint. If it is less than the diameter of the circle, the point is inside. – Mike Robinson Commented Jan 7, 2020 at 18:12
  • @user3783243 it's different. I don't have 2 coordinates. I need to calculate the circle "edges" from the center and radius – user12361681 Commented Jan 7, 2020 at 18:14
  • 2 @MikeRobinson You mean "if it is less than the radius ..." – Teemu Commented Jan 7, 2020 at 18:16
  • 41.5408446218337, -8.612296123028727 is one point and 41.536558, lng: -8.627487 is the other, no? – user3783243 Commented Jan 7, 2020 at 18:20
 |  Show 2 more ments

3 Answers 3

Reset to default 4

Credit to this SO link and Great-circle distance

let spotCoordinates1 = [41.5408446218337, -8.612296123028727];
let spotCoordinates2 = [38.817459, -9.282218]

let center = {lat: 41.536558, lng: -8.627487};
let radius = 25

checkIfInside(spotCoordinates1);
checkIfInside(spotCoordinates2);

function checkIfInside(spotCoordinates) {

    let newRadius = distanceInKmBetweenEarthCoordinates(spotCoordinates[0], spotCoordinates[1], center.lat, center.lng);
    console.log(newRadius)

    if( newRadius < radius ) {
        //point is inside the circle
        console.log('inside')
    }
    else if(newRadius > radius) {
        //point is outside the circle
        console.log('outside')
    }
    else {
        //point is on the circle
        console.log('on the circle')
    }

}

function distanceInKmBetweenEarthCoordinates(lat1, lon1, lat2, lon2) {
  var earthRadiusKm = 6371;

  var dLat = degreesToRadians(lat2-lat1);
  var dLon = degreesToRadians(lon2-lon1);

  lat1 = degreesToRadians(lat1);
  lat2 = degreesToRadians(lat2);

  var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
          Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2); 
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
  return earthRadiusKm * c;
}

function degreesToRadians(degrees) {
  return degrees * Math.PI / 180;
}

An expression for this would be:

(center.x - pt.x)^2 + (center.y - pt.y)^2 < radius^2

You can adjust the parison operator to check if the pt is on the circle (==) or if it is outside the circle boundary (>).

Here is a JavaScript function that will return -1 if the pt is inside the circle, 0 if it is on the circle and 1 if it is outside the circle.

/**
 * @description Check if a pt is in, on or outside of a circle.
 * @param {[float]} pt The point to test. An array of two floats - x and y coordinates.
 * @param {[float]} center The circle center. An array of two floats - x and y coordinates.
 * @param {float} r The circle radius.
 * @returns {-1 | 0 | 1} -1 if the point is inside, 0 if it is on and 1 if it is outside the circle.
 */
function ptInCircle(pt, center, r) {

    const lhs = Math.pow(center[0] - pt[0], 2) + Math.pow(center[1] - pt[1], 2);
    const rhs = Math.pow(r, 2);

    return lhs < rhs ? -1 : (lhs === rhs ? 0 : 1);
}

If you don't like the shorthand nested ternary operator, you can swap the return line for this:

if (lhs < rhs) {
    return -1
} else if (lhs === rhs) {
    return 0
} else {
    return 1
}

Usage:

ptInCircle([0, 1], [1, 0], 1)
// Returns 1

You can calculate the point's distance from the center and see if that is smaller then the radius, here is an example:

const spotCoordinates = {
  lat: 41.5408446218337,
  lng: -8.612296123028727
};
const center = {
  lat: 41.536558,
  lng: -8.627487
};
const radius = 25;


function degreesToRadians(degrees) {
  return degrees * Math.PI / 180;
}

function distanceInKmBetweenEarthCoordinates(lat1, lon1, lat2, lon2) {
  var earthRadiusKm = 6371;

  var dLat = degreesToRadians(lat2-lat1);
  var dLon = degreesToRadians(lon2-lon1);

  lat1 = degreesToRadians(lat1);
  lat2 = degreesToRadians(lat2);

  var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
          Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2); 
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
  return earthRadiusKm * c;
}


function isInCircle(latLng) {
  const distance = distanceInKmBetweenEarthCoordinates(latLng.lat, latLng.lng, center.lat, center.lng);

  const calculationResult = distance <= radius;
  console.log("Is inside", calculationResult);
}

isInCircle(spotCoordinates);

And here is an example of this formula in action using Google Maps: https://jsfiddle/dpr9shz3/ to use the demo, just click anywhere on the map and the alert will specify the result of the calculation and whether the circle shape was clicked or not.

发布评论

评论列表(0)

  1. 暂无评论