Last active
July 2, 2020 02:01
-
-
Save johnd0e/98ea196c43b54e40713ff022425f3013 to your computer and use it in GitHub Desktop.
IITC plugin: Experimental geodesic precision / https://cdn.staticaly.com/gist/johnd0e/98ea196c43b54e40713ff022425f3013/raw/experimental-geodesic-precision.user.js?env=dev
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @id experimental-geodesic-precision | |
// @name IITC plugin: Experimental geodesic precision | |
// @category Custom | |
// @version 0.2.0-alpha | |
// @description Increase latitude geodesic precision (experimental way) | |
// Instead of geodesic distance formula uses some Pyphagorean-like approximation, | |
// where latitude has less contribution than longitude. | |
// @namespace https://github.com/IITC-CE/ingress-intel-total-conversion | |
// @include https://intel.ingress.com/* | |
// @grant none | |
// ==/UserScript== | |
if (typeof window.plugin !== 'function') window.plugin = function() {}; | |
(function wrapper (plugin_info) { | |
// constants | |
var d2r = Math.PI/180.0; | |
var r2d = 180.0/Math.PI; | |
var earthR = 6367000.0; // earth radius in meters (doesn't have to be exact) | |
function geodesicConvertLine (start, end, convertedPoints) { | |
var lng1 = start.lng * d2r; | |
var lng2 = end.lng * d2r; | |
var dLng = lng1-lng2; | |
var segments = 0; | |
if (dLng !== 0) { | |
var lat1 = start.lat * d2r; | |
var lat2 = end.lat * d2r; | |
var dLatLng = Math.sqrt(Math.pow(dLng, 2) + Math.abs(lat1-lat2)); | |
segments = Math.floor(dLatLng * earthR / this.options.segmentsCoeff); | |
} | |
if (segments > 1) { | |
// maths based on https://edwilliams.org/avform.htm#Int | |
// pre-calculate some constant values for the loop | |
var sinLat1 = Math.sin(lat1); | |
var sinLat2 = Math.sin(lat2); | |
var cosLat1 = Math.cos(lat1); | |
var cosLat2 = Math.cos(lat2); | |
var sinLat1CosLat2 = sinLat1*cosLat2; | |
var sinLat2CosLat1 = sinLat2*cosLat1; | |
var cosLat1CosLat2SinDLng = cosLat1*cosLat2*Math.sin(dLng); | |
for (var i=1; i < segments; i++) { | |
var iLng = lng1-dLng*(i/segments); | |
var iLat = Math.atan( | |
(sinLat1CosLat2 * Math.sin(iLng-lng2) - sinLat2CosLat1 * Math.sin(iLng-lng1)) | |
/ cosLat1CosLat2SinDLng | |
); | |
convertedPoints.push(L.latLng(iLat*r2d, iLng*r2d)); | |
} | |
} | |
convertedPoints.push(end); | |
} | |
function updatePoly(Class) { | |
Class.mergeOptions({segmentLength: 10000}); | |
Class.include({_geodesicConvertLine: geodesicConvertLine}); | |
} | |
function setup () { | |
updatePoly(L.GeodesicPolyline); | |
updatePoly(L.GeodesicPolygon); | |
} | |
setup.priority = 'high'; | |
setup.info = plugin_info; //add the script info data to the function as a property | |
if (!window.bootPlugins) window.bootPlugins = []; | |
window.bootPlugins.push(setup); | |
if (window.iitcLoaded && typeof setup === 'function') setup(); | |
})({ script: typeof GM_info !== 'undefined' && GM_info.script && { | |
version: GM_info.script.version, name: GM_info.script.name, description: GM_info.script.description | |
}})// wrapper end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment