Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function distance(from, to) {
- let R = 6371000;
- let dLat = (to.lat - from.lat).toRad();
- let dLon = (to.lon - from.lon).toRad();
- let lat1 = from.lat.toRad();
- let lat2 = to.lat.toRad();
- let a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2);
- let c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
- return R * c;
- }
- Number.prototype.toRad = function() {
- return this * Math.PI / 180;
- }
- function geo2polar(from, to) {
- let p = {};
- let x = to.lon - from.lon;
- let y = to.lat - from.lat;
- p.r = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
- p.theta = 0;
- if (x > 0 && y >= 0) {
- p.theta = Math.atan(y / x);
- } else if (x > 0 && y < 0) {
- p.theta = Math.atan(y / x) + 2 * Math.PI;
- } else if (x < 0) {
- p.theta = Math.atan(y / x) + Math.PI;
- } else if (x == 0 && y > 0) {
- p.theta = Math.PI / 2;
- } else if (x == 0 && y < 0) {
- p.theta = 3 * Math.PI / 2;
- } else if (x == 0 && y == 0) {
- p.theta = 0;
- }
- return p;
- }
Advertisement
Add Comment
Please, Sign In to add comment