Find smallest distance between numbers in Array

S

Sunny

Hi,

do someone know, How we can find the smallest distance between a bunch
of lat 7 long?
Like I have 10 Latitude & Longitude.
-73.924598,40.879010
-73.924506,40.878978
-73.924506,40.878978
-73.921406,40.878178
-73.921406,40.878178
-73.920806,40.878578
-73.920206,40.878978
-73.920206,40.878978
-73.918706,40.876578
-73.918706,40.876578

If I want to see, which one is closer to the first point.
How should I do?
 
D

David Mark

Hi,

do someone know, How we can find the smallest distance between a bunch
of lat 7 long?

Shift key on the fritz?
Like I have 10 Latitude & Longitude.

Problem seems to be intermittent.
-73.924598,40.879010
-73.924506,40.878978
-73.924506,40.878978
-73.921406,40.878178
-73.921406,40.878178
-73.920806,40.878578
-73.920206,40.878978
-73.920206,40.878978
-73.918706,40.876578
-73.918706,40.876578

If I want to see, which one is closer to the first point.
How should I do?

Loop through the other 9 and compare the distances. The smallest
distance is the indicator.
 
L

Lasse Reichstein Nielsen

Sunny said:
do someone know, How we can find the smallest distance between a bunch
of lat 7 long?
Like I have 10 Latitude & Longitude.

.... some coordinates on the earth ...
If I want to see, which one is closer to the first point.
How should I do?

Run through them, one by one, calculate the distance to the first
point, and remember only the closest one.
You might need http://en.wikipedia.org/wiki/Great_circle_distance
/L
 
E

Evertjan.

Lasse Reichstein Nielsen wrote on 10 okt 2008 in comp.lang.javascript:
... some coordinates on the earth ...


Run through them, one by one, calculate the distance to the first
point, and remember only the closest one.
You might need http://en.wikipedia.org/wiki/Great_circle_distance

<http://williams.best.vwh.net/avform.htm#LL>

<http://www.movable-type.co.uk/scripts/latlong.html>

I tried it this way:

=========================================
<script type='text/javascript'>

function GreatCircleDistance(lat1,lon1,lat2,lon2) {

// convert to radians
lat1 = lat1 * Math.PI / 180;
lon1 = lon1 * Math.PI / 180;
lat2 = lat2 * Math.PI / 180;
lon2 = lon2 * Math.PI / 180;

// based on earth radius of 6371 km
// Expects the earth to be a perfect sphere.
// returns km
return 6371 *
Math.acos(
Math.sin(lat1) * Math.sin(lat2) +
Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon2-lon1)
);
};

// test
alert(GreatCircleDistance(52,4,52,6)); // local distance
alert(GreatCircleDistance(0,0,180,0)); // halve around the equator
alert(GreatCircleDistance(0,0,0,1)/60); // 1 nautical mile
alert(GreatCircleDistance(0,0,0,1/60)); // 1 nautical mile

</script>
=========================================
 
T

Thomas 'PointedEars' Lahn

Sunny said:
do someone know, How we can find the smallest distance between a bunch
of lat 7 long?

Didn't you post that homework question a few days ago already?

You better don't do that again. And please try to improve your language skills.


PointedEars
 
R

RobG


The author of that article seems to believe there is a direct
correlation between the number of significant digits and accuracy,
however that is not a reasonable conclusion. The accuracy of Math
functions is dependent on the algorithm used to implement them -
calculating pi as 22/7 to 1,000 decimal places is less accurate than
using 3.1416.

ECMA-262 does not specify how Math functions are to be implemented,
although it suggests using fdlibm, therefore their accuracy (and
anything computed using them) is likely implementation dependent.

Are there any known issues with the accuracy of particular
implementations?
 
D

Dr J R Stockton

In comp.lang.javascript message <847acf89-e72b-4803-8a18-daf70971713a@g1
7g2000prg.googlegroups.com>, Mon, 13 Oct 2008 17:52:16, RobG
ECMA-262 does not specify how Math functions are to be implemented,
although it suggests using fdlibm, therefore their accuracy (and
anything computed using them) is likely implementation dependent.

It suggests using the algorithms in fdlibm, not necessarily fdlibm
(which I take to be a document of some form) itself.

Most, at least, of the standard JavaScript maths functions map directly
to CPU or FPU instructions on the PC; and I'd hope that they do so on
any recent general purpose processor chip. All of those instructions
should give an exact result if possible, and _IIRC_ should be good to
about one LSB otherwise. Therefore the accuracy IMHO should not vary
much with implementation, although the results need not match exactly.

Of course, results are liable to be worse on ill-conditioned problems.
For example, one should generally not solve a quadratic by evaluating
the well-known formula containing +/- twice, instead using whichever
sign gives the bigger result and obtaining the other by a readily-found
simple expression. It's a pity that there have been IIRC no questions
which would justify discussing such, briefly, in the FAQ.
 
W

William James

Evertjan. said:
Lasse Reichstein Nielsen wrote on 10 okt 2008 in comp.lang.javascript:


<http://williams.best.vwh.net/avform.htm#LL>

<http://www.movable-type.co.uk/scripts/latlong.html>

I tried it this way:

=========================================
<script type='text/javascript'>

function GreatCircleDistance(lat1,lon1,lat2,lon2) {

// convert to radians
lat1 = lat1 * Math.PI / 180;
lon1 = lon1 * Math.PI / 180;
lat2 = lat2 * Math.PI / 180;
lon2 = lon2 * Math.PI / 180;

// based on earth radius of 6371 km
// Expects the earth to be a perfect sphere.
// returns km
return 6371 *
Math.acos(
Math.sin(lat1) * Math.sin(lat2) +
Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon2-lon1)
);
};

Using Spidermonkey and jslibs:

LoadModule('jsstd') // Gives us Print().

var points = "\
-73.924598,40.879010 \
-73.924506,40.878978 \
-73.924506,40.878978 \
-73.921406,40.878178 \
-73.921406,40.878178 \
-73.920806,40.878578 \
-73.920206,40.878978 \
-73.920206,40.878978 \
-73.918706,40.876578 \
-73.918706,40.876578".split( " " )

var here = points.shift()

function calc_distance(lat1,lon1, lat2,lon2)
{ // convert to radians
lat1 = lat1 * Math.PI / 180
lon1 = lon1 * Math.PI / 180
lat2 = lat2 * Math.PI / 180
lon2 = lon2 * Math.PI / 180

// based on earth radius of 6371 km
// Expects the earth to be a perfect sphere.
// returns km
return 6371 * Math.acos(
Math.sin(lat1) * Math.sin(lat2) +
Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon2-lon1) )
}
function str_calc_dist( str1, str2 )
{ return calc_distance.
apply(this, [str1,str2].join(",").split(","))
}

Print( points.sort( function(a,b){
return str_calc_dist(a,here) - str_calc_dist(b,here) }
)[0] )
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top