UNPKG

random-location

Version:

Random coordinates within a circle (or on a circumference) given a center point and radius.

1 lines 2.16 kB
!function(t,e){if("object"==typeof exports&&"object"==typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var n=e();for(var r in n)("object"==typeof exports?exports:t)[r]=n[r]}}(global,(function(){return function(t){var e={};function n(r){if(e[r])return e[r].exports;var o=e[r]={i:r,l:!1,exports:{}};return t[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=t,n.c=e,n.d=function(t,e,r){n.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:r})},n.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},n.t=function(t,e){if(1&e&&(t=n(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var o in t)n.d(r,o,function(e){return t[e]}.bind(null,o));return r},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p="",n(n.s=0)}([function(t,e){const n=Math.PI/180,r=3*Math.PI,o=2*Math.PI,a=t=>t*n,i=t=>t/n,u=(t,e,n=Math.random)=>{const u=Math.sin(a(t.latitude)),l=Math.cos(a(t.latitude)),d=n()*o,s=Math.sin(d),c=Math.cos(d),f=e/6371e3,h=Math.sin(f),M=Math.cos(f);let p,b;return p=Math.asin(u*M+l*h*c),b=a(t.longitude)+Math.atan2(s*h*l,M-u*Math.sin(p)),b=(b+r)%o-Math.PI,{latitude:i(p),longitude:i(b)}},l=(t,e)=>{const n={latitude:a(t.latitude),longitude:a(t.longitude)},r={latitude:a(e.latitude),longitude:a(e.longitude)},o=Math.sin((r.latitude-n.latitude)/2),i=Math.sin((r.longitude-n.longitude)/2),u=o*o+i*i*Math.cos(n.latitude)*Math.cos(r.latitude);return 6371e3*(2*Math.atan2(Math.sqrt(u),Math.sqrt(1-u)))},d=l;t.exports={distance:l,haversine:d,randomCircumferencePoint:u,randomCirclePoint:(t,e,n=Math.random)=>u(t,Math.sqrt(n())*e,n),randomAnnulusPoint:(t,e,n,r=Math.random)=>{if(e>=n)throw new Error(`innerRadius (${e}) should be smaller than outerRadius (${n})`);const o=n-e;return u(t,e+Math.sqrt(r())*o,r)}}}])}));