Velocity Reviews > Find smallest distance between numbers in Array

# Find smallest distance between numbers in Array

Sunny
Guest
Posts: n/a

 10-10-2008
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?

David Mark
Guest
Posts: n/a

 10-10-2008
On Oct 10, 10:21*am, Sunny <(E-Mail Removed)> wrote:
> 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.

Lasse Reichstein Nielsen
Guest
Posts: n/a

 10-10-2008
Sunny <(E-Mail Removed)> writes:

> 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
--
Lasse Reichstein Nielsen
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'

Evertjan.
Guest
Posts: n/a

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

> Sunny <(E-Mail Removed)> writes:
>
>> 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

<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) {

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(0,0,180,0)); // halve around the equator

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

--
Evertjan.
The Netherlands.

Thomas 'PointedEars' Lahn
Guest
Posts: n/a

 10-11-2008
Sunny wrote:
> 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
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>

RobG
Guest
Posts: n/a

 10-14-2008
On Oct 11, 6:29 am, "Evertjan." <(E-Mail Removed)> wrote:
[...]
> <http://www.movable-type.co.uk/scripts/latlong.html>

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?

--
Rob

Dr J R Stockton
Guest
Posts: n/a

 10-14-2008
In comp.lang.javascript message <847acf89-e72b-4803-8a18-daf70971713a@g1
7g2000prg.googlegroups.com>, Mon, 13 Oct 2008 17:52:16, RobG
<(E-Mail Removed)> posted:
>
>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.

--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 7.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
I find MiniTrue useful for viewing/searching/altering files, at a DOS prompt;
free, DOS/Win/UNIX, <URL:http://www.idiotsdelight.net/minitrue/> unsupported.

William James
Guest
Posts: n/a

 11-27-2008

Evertjan. wrote:
> Lasse Reichstein Nielsen wrote on 10 okt 2008 in comp.lang.javascript:
>
> > Sunny <(E-Mail Removed)> writes:
> >
> >> 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

>
> <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) {
>
> 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:

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)
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] )