Skip to content

Instantly share code, notes, and snippets.

@vikaskanani
Created March 24, 2021 10:00
Show Gist options
  • Save vikaskanani/af76caac7fbd84670741e64fc4700301 to your computer and use it in GitHub Desktop.
Save vikaskanani/af76caac7fbd84670741e64fc4700301 to your computer and use it in GitHub Desktop.
JavaScript program to check circle overlapping
const MILES2METER = 1609.34;
const METER2MILE = 0.000621371;
// JavaScript program to check circles overlapping
function circle(x1, y1, x2, y2, r1, r2) {
//let distSq = Math.sqrt(((x1 - x2) * (x1 - x2)) + ((y1 - y2) * (y1 - y2)));
let distSq = getDistanceFromLatLonInMile(x1, y1, x2, y2) * MILES2METER;
if ( r1 < r2 ) {
[r1,r2]=[r2,r1];
}
if (distSq + r2 <= r1) {
return 'Complete overlap';
} else if ( distSq > r2 + r1) {
return 'Not overlapping';
} else {
return 'Overlapping';
}
}
function getDistanceFromLatLonInMile(lat1, lon1, lat2, lon2) {
var R = 3959; // Radius of the earth in mile
var dLat = deg2rad(lat2 - lat1); // deg2rad below
var dLon = deg2rad(lon2 - lon1);
var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.sin(dLon / 2) * Math.sin(dLon / 2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
var d = R * c; // Distance in mile
return d;
}
function deg2rad(deg) {
return deg * (Math.PI / 180)
}
console.log( circle(43.047461250774695,-108.03926157053738,43.0453134,-108.1903059,34700,16094));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment