all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* distance from Easter Island to Chile
@ 2014-04-20  0:03 Emanuel Berg
  2014-04-20  3:55 ` Frank Stutzman
                   ` (3 more replies)
  0 siblings, 4 replies; 20+ messages in thread
From: Emanuel Berg @ 2014-04-20  0:03 UTC (permalink / raw)
  To: help-gnu-emacs

;; As you know, the Easter Bunny isn't the kind of guy
;; to go on vacation, neglecting his duty. Instead, he
;; dropped me this interesting piece of Elisp to
;; calculate the distance between Earth locations,
;; based on the "Haversine formula".
;;
;; According to the program, the Easter Island (the
;; world's most desolate place) is 4301 kilometers from
;; the capital of Chile. I Googled that selfsame
;; distance, and the result makes me think the Easter
;; Bunny pulled of a successful implementation, though
;; no one, of course, should be above criticism.
;;
;; Enjoy!

(defun sin2 (p)
  (let ((sin-p (sin p)))
    (* sin-p sin-p) ))

(defun distance (l1-r f1-r l2-r f2-r)
  (interactive)
  (let ((l1 (degrees-to-radians l1-r))
        (f1 (degrees-to-radians f1-r))
        (l2 (degrees-to-radians l2-r))
        (f2 (degrees-to-radians f2-r)) )
    (* 2 6378.1 ; Earth's radius
       (asin
        (sqrt
         (+ (sin2 (/ (- f2 f1) 2))
            (* (cos f2) (cos f1) (sin2 (/ (- l2 l1) 2))) ))))))

(distance 33.45    ; Santiago
          70.6667
          27.1167  ; Easter Island
          109.3667
          ) ; should be 3757.41 [1]

;; [1] http://www.mapcrow.info/Distance_between_Santiago_CI_and_Easter_Island_CI.html

-- 
underground experts united:
http://user.it.uu.se/~embe8573


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: distance from Easter Island to Chile
  2014-04-20  0:03 distance from Easter Island to Chile Emanuel Berg
@ 2014-04-20  3:55 ` Frank Stutzman
  2014-04-20 15:14   ` Emanuel Berg
  2014-04-20  4:44 ` Barry Margolin
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 20+ messages in thread
From: Frank Stutzman @ 2014-04-20  3:55 UTC (permalink / raw)
  To: help-gnu-emacs

Emanuel Berg <embe8573@student.uu.se> wrote:
> ;; As you know, the Easter Bunny isn't the kind of guy
> ;; to go on vacation, neglecting his duty. Instead, he
> ;; dropped me this interesting piece of Elisp to
> ;; calculate the distance between Earth locations,
> ;; based on the "Haversine formula".

<snip>

As a realative newie to doing fancy things in emacs,
I am amazed and amused that this 1) can be done at all,
and 2) can be done so elegantly.

Or at least it looks it looks like it should be that 
should be the case.  For whatever reason, though, when I 
evaluate:

(distance 33.45    ; Santiago
          70.6667
          27.1167  ; Easter Island
          109.3667
          ) 

I get an answer of 4301.199.  Have no idea why.
GNU Emacs 24.3.1 (x86_64-pc-linux-gnu, GTK+ Version 3.4.2)
on Unbuntu 12.04.


-- 
Frank Stutzman



^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: distance from Easter Island to Chile
  2014-04-20  0:03 distance from Easter Island to Chile Emanuel Berg
  2014-04-20  3:55 ` Frank Stutzman
@ 2014-04-20  4:44 ` Barry Margolin
  2014-04-20 15:06   ` Emanuel Berg
  2014-04-20  6:28 ` Eli Zaretskii
       [not found] ` <mailman.19905.1397975330.10748.help-gnu-emacs@gnu.org>
  3 siblings, 1 reply; 20+ messages in thread
From: Barry Margolin @ 2014-04-20  4:44 UTC (permalink / raw)
  To: help-gnu-emacs

In article <87mwfguasr.fsf@nl106-137-194.student.uu.se>,
 Emanuel Berg <embe8573@student.uu.se> wrote:

> (defun distance (l1-r f1-r l2-r f2-r)
>   (interactive)

If you declare this interactive, you need to provide a specification 
string for the arguments, so that it can prompt for them.

And your argument names are not very helpful. They're apparently 
latitude and longitude, so what does "f" mean? And what is the "-r" 
suffix on all of them?

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: distance from Easter Island to Chile
  2014-04-20  0:03 distance from Easter Island to Chile Emanuel Berg
  2014-04-20  3:55 ` Frank Stutzman
  2014-04-20  4:44 ` Barry Margolin
@ 2014-04-20  6:28 ` Eli Zaretskii
       [not found] ` <mailman.19905.1397975330.10748.help-gnu-emacs@gnu.org>
  3 siblings, 0 replies; 20+ messages in thread
From: Eli Zaretskii @ 2014-04-20  6:28 UTC (permalink / raw)
  To: help-gnu-emacs

> From: Emanuel Berg <embe8573@student.uu.se>
> Date: Sun, 20 Apr 2014 02:03:32 +0200
> 
> (defun distance (l1-r f1-r l2-r f2-r)
>   (interactive)
>   (let ((l1 (degrees-to-radians l1-r))
>         (f1 (degrees-to-radians f1-r))
>         (l2 (degrees-to-radians l2-r))
>         (f2 (degrees-to-radians f2-r)) )
>     (* 2 6378.1 ; Earth's radius
>        (asin
>         (sqrt
>          (+ (sin2 (/ (- f2 f1) 2))
>             (* (cos f2) (cos f1) (sin2 (/ (- l2 l1) 2))) ))))))

This is inaccurate (Earth is not a sphere).  See
http://en.wikipedia.org/wiki/Geodesics_on_an_ellipsoid for some theory
and more accurate formulas.  A very accurate method is described in
http://en.wikipedia.org/wiki/Vincenty%27s_formulae (you need the
"inverse problem").



^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: distance from Easter Island to Chile
       [not found] ` <mailman.19905.1397975330.10748.help-gnu-emacs@gnu.org>
@ 2014-04-20  7:01   ` Barry Margolin
  2014-04-20  7:48     ` Eli Zaretskii
  2014-04-20 15:22     ` Emanuel Berg
  0 siblings, 2 replies; 20+ messages in thread
From: Barry Margolin @ 2014-04-20  7:01 UTC (permalink / raw)
  To: help-gnu-emacs

In article <mailman.19905.1397975330.10748.help-gnu-emacs@gnu.org>,
 Eli Zaretskii <eliz@gnu.org> wrote:

> > From: Emanuel Berg <embe8573@student.uu.se>
> > Date: Sun, 20 Apr 2014 02:03:32 +0200
> > 
> > (defun distance (l1-r f1-r l2-r f2-r)
> >   (interactive)
> >   (let ((l1 (degrees-to-radians l1-r))
> >         (f1 (degrees-to-radians f1-r))
> >         (l2 (degrees-to-radians l2-r))
> >         (f2 (degrees-to-radians f2-r)) )
> >     (* 2 6378.1 ; Earth's radius
> >        (asin
> >         (sqrt
> >          (+ (sin2 (/ (- f2 f1) 2))
> >             (* (cos f2) (cos f1) (sin2 (/ (- l2 l1) 2))) ))))))
> 
> This is inaccurate (Earth is not a sphere).

How accurate does your input have to be for that to be a significant? 
I.e. if you're calculating the distance between two cities, and each 
city is 5 miles wide, the distance is +/- 10 miles depending on where in 
the two cities you decide to get the coordinates from. So if the error 
in the formula is 2 miles, it's less than the inaccuracy in the input, 
so the formula should be good enough.

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: distance from Easter Island to Chile
  2014-04-20  7:01   ` Barry Margolin
@ 2014-04-20  7:48     ` Eli Zaretskii
  2014-04-20 10:26       ` Thorsten Jolitz
  2014-04-20 15:22     ` Emanuel Berg
  1 sibling, 1 reply; 20+ messages in thread
From: Eli Zaretskii @ 2014-04-20  7:48 UTC (permalink / raw)
  To: help-gnu-emacs

> From: Barry Margolin <barmar@alum.mit.edu>
> Date: Sun, 20 Apr 2014 03:01:36 -0400
> 
> In article <mailman.19905.1397975330.10748.help-gnu-emacs@gnu.org>,
>  Eli Zaretskii <eliz@gnu.org> wrote:
> 
> > This is inaccurate (Earth is not a sphere).
> 
> How accurate does your input have to be for that to be a significant? 
> I.e. if you're calculating the distance between two cities, and each 
> city is 5 miles wide, the distance is +/- 10 miles depending on where in 
> the two cities you decide to get the coordinates from. So if the error 
> in the formula is 2 miles, it's less than the inaccuracy in the input, 
> so the formula should be good enough.

No one said that these calculations are only for distances between
large cities.

Anyway, the error induced by assuming spherical Earth can be up to 1%,
which is not insignificant when the distances are on the order of
magnitude of many hundreds of kilometers (3757 km and 4301 km values
were cited by the OP).



^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: distance from Easter Island to Chile
  2014-04-20  7:48     ` Eli Zaretskii
@ 2014-04-20 10:26       ` Thorsten Jolitz
  2014-04-20 11:03         ` Eli Zaretskii
  0 siblings, 1 reply; 20+ messages in thread
From: Thorsten Jolitz @ 2014-04-20 10:26 UTC (permalink / raw)
  To: help-gnu-emacs

Eli Zaretskii <eliz@gnu.org> writes:

>> From: Barry Margolin <barmar@alum.mit.edu>
>> Date: Sun, 20 Apr 2014 03:01:36 -0400
>> 
>> In article <mailman.19905.1397975330.10748.help-gnu-emacs@gnu.org>,
>>  Eli Zaretskii <eliz@gnu.org> wrote:
>> 
>> > This is inaccurate (Earth is not a sphere).
>> 
>> How accurate does your input have to be for that to be a significant? 
>> I.e. if you're calculating the distance between two cities, and each 
>> city is 5 miles wide, the distance is +/- 10 miles depending on where in 
>> the two cities you decide to get the coordinates from. So if the error 
>> in the formula is 2 miles, it's less than the inaccuracy in the input, 
>> so the formula should be good enough.
>
> No one said that these calculations are only for distances between
> large cities.
>
> Anyway, the error induced by assuming spherical Earth can be up to 1%,
> which is not insignificant when the distances are on the order of
> magnitude of many hundreds of kilometers (3757 km and 4301 km values
> were cited by the OP).

I think one of the two French guys who had the task to establish a
world-wide accepted meter unit as 

,-------------------------------------------------------------------------
| 1/10,000,000 part of the quarter of a meridian, measurement (1795) by   
| Delambre and Mechain (http://en.wikipedia.org/wiki/History_of_the_metre)
`-------------------------------------------------------------------------

figured this out the hard way and became a depressed alcoholic because
of an unexplainable 300m 'measurement-error' in his triangulations.

-- 
cheers,
Thorsten




^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: distance from Easter Island to Chile
  2014-04-20 10:26       ` Thorsten Jolitz
@ 2014-04-20 11:03         ` Eli Zaretskii
  0 siblings, 0 replies; 20+ messages in thread
From: Eli Zaretskii @ 2014-04-20 11:03 UTC (permalink / raw)
  To: help-gnu-emacs

> From: Thorsten Jolitz <tjolitz@gmail.com>
> Date: Sun, 20 Apr 2014 12:26:15 +0200
> 
> I think one of the two French guys who had the task to establish a
> world-wide accepted meter unit as 
> 
> ,-------------------------------------------------------------------------
> | 1/10,000,000 part of the quarter of a meridian, measurement (1795) by   
> | Delambre and Mechain (http://en.wikipedia.org/wiki/History_of_the_metre)
> `-------------------------------------------------------------------------
> 
> figured this out the hard way and became a depressed alcoholic because
> of an unexplainable 300m 'measurement-error' in his triangulations.

Well, I'm no French, although I do like French wines and cognac.



^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: distance from Easter Island to Chile
  2014-04-20  4:44 ` Barry Margolin
@ 2014-04-20 15:06   ` Emanuel Berg
  0 siblings, 0 replies; 20+ messages in thread
From: Emanuel Berg @ 2014-04-20 15:06 UTC (permalink / raw)
  To: help-gnu-emacs

Barry Margolin <barmar@alum.mit.edu> writes:

>> (defun distance (l1-r f1-r l2-r f2-r) (interactive)
>
> If you declare this interactive, you need to provide
> a specification string for the arguments, so that it
> can prompt for them.

Right, I forgot about that. It is not used
interactively in the demo, and it is not really suited
for that, because it is a pain (and error-prone) to
enter those digits in the minibuffer - better to put
them in a form and evaluate it (I think `C-x C-e' or
`C-M-x' are the standard keybindings).

Then the digits are "persistent" as well and you can
add more locations.

If you were to do that it would perhaps make sense to
redo the interface slightly to make it accept to
*lists* referring to locations, and tagged with metadata
to make the output more "human", as in:

("Easter Island" (and more ...) 27.1167 109.3667)

But a simple interactive version could look like this:

(defun distance (l1-d f1-d l2-d f2-d)
  (interactive "nLatitude 1: \nnLongitude 1: \nnLatitude 2: \nnLongitude 2: ")
  (let ((l1 (degrees-to-radians l1-d))
        (f1 (degrees-to-radians f1-d))
        (l2 (degrees-to-radians l2-d))
        (f2 (degrees-to-radians f2-d)) )
    (message "%s"
             (* 2 6378.1 ; Earth's radius
                (asin
                 (sqrt
                  (+ (sin2 (/ (- f2 f1) 2))
                     (* (cos f2) (cos f1) (sin2 (/ (- l2 l1) 2))) )))))))

> And your argument names are not very helpful. They're
> apparently latitude and longitude, so what does "f"
> mean?

I got the f's and l's from the Haversine formula
itself, as it was presented on Wikipedia, though in
that formula, they were actually the Greek letters
phi and lambda, respectively.

I agree that "latitude" and "longitude" are better
names, but then the math code would get "chatty", and
it would be difficult to see the formula behind it. I
don't know if the scientific community has some
short-forms (preferable single-lettered) for those
quantities? (It would be a no-brainer if they didn't
both start with "l"s...)

> And what is the "-r" suffix on all of them?

Right, that should be "-d" (for degrees), as that's
what you input - it gets converted to radians in the
`let' form.

-- 
underground experts united:
http://user.it.uu.se/~embe8573


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: distance from Easter Island to Chile
  2014-04-20  3:55 ` Frank Stutzman
@ 2014-04-20 15:14   ` Emanuel Berg
  2014-04-20 22:39     ` giacomo.boffi
       [not found]     ` <mailman.19957.1398033917.10748.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 20+ messages in thread
From: Emanuel Berg @ 2014-04-20 15:14 UTC (permalink / raw)
  To: help-gnu-emacs

Frank Stutzman <stutzman@cat2.kjsl.com> writes:

> As a realative newie to doing fancy things in emacs,
> I am amazed and amused that this 1) can be done at
> all, and 2) can be done so elegantly.

Thank you! Actually that is an extremely
straightforward implementation of the formula - but
nonetheless, yes, Lisp (and Elisp) is just *gorgeous*
from an aesthetic point of view, and it is very fast
for doing simple things.

Check out my update to the code in response to
Mr. Margolin's comments for some refinements.

> Or at least it looks it looks like it should be that
> should be the case.  For whatever reason, though,
> when I evaluate ...
>
> I get an answer of 4301.199

That's normal - this is a mathematical method to
calculate the distance between two spots on a
sphere. But the world of math isn't exactly the world
of - well, the world.

The distance figure I got from the web is the (much
more) accurate one, but I cannot say what methods are
behind it. Safe to say, it is less abstract/general,
and more practical.

-- 
underground experts united:
http://user.it.uu.se/~embe8573


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: distance from Easter Island to Chile
  2014-04-20  7:01   ` Barry Margolin
  2014-04-20  7:48     ` Eli Zaretskii
@ 2014-04-20 15:22     ` Emanuel Berg
  2014-04-21  6:04       ` Emanuel Berg
  1 sibling, 1 reply; 20+ messages in thread
From: Emanuel Berg @ 2014-04-20 15:22 UTC (permalink / raw)
  To: help-gnu-emacs

Barry Margolin <barmar@alum.mit.edu> writes:

> How accurate does your input have to be for that to
> be a significant?  I.e. if you're calculating the
> distance between two cities, and each city is 5 miles
> wide, the distance is +/- 10 miles depending on where
> in the two cities you decide to get the coordinates
> from. So if the error in the formula is 2 miles, it's
> less than the inaccuracy in the input, so the formula
> should be good enough.

Yes, let's make this crystal (ball) clear, this is an
implementation of a mathematical formula - if anyone
reading this is writing a school essay on geography,
*don't* use data from this tool :) The data is correct
but in a gross way.

I don't know what the professionals do to calculate
distances, but safe to say, they have more refined,
more specific, more empirical "down-to-earth" methods
(pardon the pun)!

I thought that was clear, but no harm, now it is.

-- 
underground experts united:
http://user.it.uu.se/~embe8573


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: distance from Easter Island to Chile
  2014-04-20 15:14   ` Emanuel Berg
@ 2014-04-20 22:39     ` giacomo.boffi
       [not found]     ` <mailman.19957.1398033917.10748.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 20+ messages in thread
From: giacomo.boffi @ 2014-04-20 22:39 UTC (permalink / raw)
  To: help-gnu-emacs

Emanuel Berg <embe8573@student.uu.se> writes:

> Frank Stutzman <stutzman@cat2.kjsl.com> writes:
>
>> I get an answer of 4301.199 

mee too

> That's normal

(progn 
  (defun d2r (x) (/ (* pi x) 180))
  (defun spherical-law-of-cosines (p1 p2)
    "http://en.wikipedia.org/wiki/Great-circle_distance#Formulas"
    (let ((lat1 (car p1)) (lon1 (cadr p1))
	  (lat2 (car p2)) (lon2 (cadr p2)))
      (acos (+ (* (sin lat1) (sin lat2))
	       (* (cos lat1) (cos lat2) (cos (- lon2 lon1)))))))
  
  (setq santiago (mapcar 'd2r '(33.4500  70.6667)))
  (setq easter_i (mapcar 'd2r '(27.1167 109.3667)))
  (insert (format "\nEaster Island to Santiago distance is%8.2f km."
                  (* (spherical-law-of-cosines santiago easter_i) 6378.1))))
                                                                            ^
put the cursor over the caret and type C-x C-e




^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: distance from Easter Island to Chile
       [not found]     ` <mailman.19957.1398033917.10748.help-gnu-emacs@gnu.org>
@ 2014-04-21  1:40       ` Emanuel Berg
  2014-04-21  1:50         ` Emanuel Berg
                           ` (2 more replies)
  2014-04-21  2:16       ` Emanuel Berg
  1 sibling, 3 replies; 20+ messages in thread
From: Emanuel Berg @ 2014-04-21  1:40 UTC (permalink / raw)
  To: help-gnu-emacs

giacomo.boffi@gmail.com writes:

>> I get an answer of 4301.199
>
> me too

I get that too. We all get the same result - it is a
function that maps input (in a deterministic way) to
output. Provided the implementation is correct that's
what you get with the Haversine method.

> (progn 
>   (defun d2r (x) (/ (* pi x) 180))
>   (defun spherical-law-of-cosines (p1 p2)
>     "http://en.wikipedia.org/wiki/Great-circle_distance#Formulas"
>     (let ((lat1 (car p1)) (lon1 (cadr p1))
> 	  (lat2 (car p2)) (lon2 (cadr p2)))
>       (acos (+ (* (sin lat1) (sin lat2))
> 	       (* (cos lat1) (cos lat2) (cos (- lon2 lon1)))))))
>   
>   (setq santiago (mapcar 'd2r '(33.4500  70.6667)))
>   (setq easter_i (mapcar 'd2r '(27.1167 109.3667)))
>   (insert (format "\nEaster Island to Santiago distance is%8.2f km."
>                   (* (spherical-law-of-cosines
>   santiago easter_i) 6378.1))))

That's uncanny close to the Googled distance! - that
means the Earth is very, very spherical, I take it?

This is so exact it *could* be used in those "school
essays" (in which I told not to use the Haversine
method) - one is even tempted to suspect that this
formula is what they used - possibly adding or
subtracting some (empirical) constant?

Awesome stuff!

Comment: There is already a d2r (degrees-to-radians).

I'll definitely add this to my distance.el - keep this
up, and we will soon have an Emacs GIS library...

-- 
underground experts united:
http://user.it.uu.se/~embe8573


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: distance from Easter Island to Chile
  2014-04-21  1:40       ` Emanuel Berg
@ 2014-04-21  1:50         ` Emanuel Berg
  2014-04-21  9:45         ` giacomo.boffi
       [not found]         ` <mailman.19981.1398073560.10748.help-gnu-emacs@gnu.org>
  2 siblings, 0 replies; 20+ messages in thread
From: Emanuel Berg @ 2014-04-21  1:50 UTC (permalink / raw)
  To: help-gnu-emacs

Emanuel Berg <embe8573@student.uu.se> writes:

> This is so exact it *could* be used in those "school
> essays" (in which I told not to use the Haversine
> method)

On the Wikipedia article on the Haversine, they mention
the other formula as well - though it is a bit hard to
make sense of... (What is "h"?)

"When using these formulae, ensure that h does not
 exceed 1 due to a floating point error (d is only real
 for h from 0 to 1). h only approaches 1 for antipodal
 points (on opposite sides of the sphere) — in this
 region, relatively large numerical errors tend to
 arise in the formula when finite precision is
 used. Because d is then large (approaching πR, half
 the circumference) a small error is often not a major
 concern in this unusual case (although there are other
 great-circle distance formulas that avoid this
 problem). (The formula above is sometimes written in
 terms of the arctangent function, but this suffers
 from similar numerical problems near h = 1.)

 As described below, a similar formula can be written
 using cosines (sometimes called the spherical law of
 cosines, not to be confused with the law of cosines
 for plane geometry) instead of haversines, but if the
 two points are close together (e.g. a kilometer apart,
 on the Earth) you might end up with cos (d/R) =
 0.99999999, leading to an inaccurate answer. Since the
 haversine formula uses sines it avoids that problem."

-- 
underground experts united:
http://user.it.uu.se/~embe8573


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: distance from Easter Island to Chile
       [not found]     ` <mailman.19957.1398033917.10748.help-gnu-emacs@gnu.org>
  2014-04-21  1:40       ` Emanuel Berg
@ 2014-04-21  2:16       ` Emanuel Berg
  1 sibling, 0 replies; 20+ messages in thread
From: Emanuel Berg @ 2014-04-21  2:16 UTC (permalink / raw)
  To: help-gnu-emacs

giacomo.boffi@gmail.com writes:

> (progn (defun d2r (x) (/ (* pi x) 180)) ...

Aha, degrees-to-radians is a macro, won't play with
mapcar (?). Did some improvements (I hope) to the
interface:

(defun d2r (d) (/ (* pi d) 180))

(defun spherical-law-of-cosines (p1 p2)
  (let ((lat1 (car p1)) (lon1 (cadr p1))
        (lat2 (car p2)) (lon2 (cadr p2)))
    (acos (+ (* (sin lat1) (sin lat2))
             (* (cos lat1) (cos lat2) (cos (- lon2 lon1)))))))

(defun distance-2 (p1 p2)
  (let*((from          (car p1))
        (from-coords-r (mapcar 'd2r (cadr p1)))
        (to            (car p2))
        (to-coords-r   (mapcar 'd2r (cadr p2)))
        (dist (* 6378.1
                 (spherical-law-of-cosines from-coords-r
                                           to-coords-r)) ))
    (message (format "%s -> %s: %8.2f km" from to dist)) ))

(setq p1 '("Santiago"      (33.4500  70.6667)))
(setq p2 '("Easter Island" (27.1167 109.3667)))

(distance-2 p1 p2)

Now we just need a database of locations, then we can
really go nuts...

-- 
underground experts united:
http://user.it.uu.se/~embe8573


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: distance from Easter Island to Chile
  2014-04-20 15:22     ` Emanuel Berg
@ 2014-04-21  6:04       ` Emanuel Berg
  0 siblings, 0 replies; 20+ messages in thread
From: Emanuel Berg @ 2014-04-21  6:04 UTC (permalink / raw)
  To: help-gnu-emacs

I thought the grossness of the first formula was due to
the Earth not being completely sphere. Instead, it
seems to be due to lack of computing precision. The
implication of that should be that the Earth is
probably more a sphere than a ping-pong ball straight
out of a Chinese factory... scary!

If this theory is correct, it could be interesting to
see if execution on a 64-bit machine would produce a
different result.

On Debian (at least), you can check your hardware with
'lscpu', and the OS (the Linux kernel) with

getconf LONG_BIT

If you have a 32-bit kernel, I say you are safely in
the the "32-bit camp", even though you have 64-bit
capable hardware (but sometimes 32-bit OSs can run
64-bit software in 64-bit mode). I have heard there are
even 128-bit machines among common people these
days... But my school's system (Solaris/SunOS) is
32-bit as well and it produces the same result as on my
32-bit desktop Debian.

-- 
underground experts united:
http://user.it.uu.se/~embe8573


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: distance from Easter Island to Chile
  2014-04-21  1:40       ` Emanuel Berg
  2014-04-21  1:50         ` Emanuel Berg
@ 2014-04-21  9:45         ` giacomo.boffi
       [not found]         ` <mailman.19981.1398073560.10748.help-gnu-emacs@gnu.org>
  2 siblings, 0 replies; 20+ messages in thread
From: giacomo.boffi @ 2014-04-21  9:45 UTC (permalink / raw)
  To: help-gnu-emacs

Emanuel Berg <embe8573@student.uu.se> writes:

> giacomo.boffi@gmail.com writes:
>
>>> I get an answer of 4301.199
>>
>> me too
>
> Provided the implementation is correct that's what you get with the
> Haversine method.

(defun d2r (x) 
  "degrees-to-radians is a macro, won't work with mapcar..."
  (/ (* x pi) 180))

; wolfram's alpha says
(setq average-earth-radius 6367.4447)

; there are sources that report different locations
; for Santiago and Easter Island
(setq santiago (mapcar 'd2r '(33.4500  70.6667)))
(setq easter_i (mapcar 'd2r '(27.1167 109.3667)))

(defun haversines (p1 p2)
  "Returns a list with sin^2(Delta_Lat/2) and sin^2(Delta_Lon/2).
http://en.wikipedia.org/wiki/Haversine" 
  (flet ((d2 (x) (/ x 2))
	 (p2 (x) (* x x)))
    (mapcar 'p2 (mapcar 'sin (mapcar 'd2 (mapcar* '- p1 p2))))))

(defun central-angle (p1 p2)
  "Returns the central angle between two locations on a sphere,
using the haversine formula.
   http://en.wikipedia.org/wiki/Great-circle_distance#Computational_formulas "
  (let* ( (hs (haversines p1 p2))
	  (hs-lat (car  hs))
	  (hs-lon (cadr hs))
	  (cos-lat1 (cos (car p1)))
	  (cos-lat2 (cos (car p2)))
	  )
    (* 2 (asin (sqrt (+ hs-lat (* cos-lat1 cos-lat2 hs-lon)))))))

(insert (format "\n%f" (* 6378.1 
			(central-angle santiago easter_i))))

(insert (format "\n%f" (* average-earth-radius 
			(central-angle santiago easter_i))))




^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: distance from Easter Island to Chile
       [not found]         ` <mailman.19981.1398073560.10748.help-gnu-emacs@gnu.org>
@ 2014-04-21 10:35           ` Emanuel Berg
  2014-04-21 10:50             ` Emanuel Berg
  0 siblings, 1 reply; 20+ messages in thread
From: Emanuel Berg @ 2014-04-21 10:35 UTC (permalink / raw)
  To: help-gnu-emacs

giacomo.boffi@gmail.com writes:

> Emanuel Berg <embe8573@student.uu.se> writes:
>
>> giacomo.boffi@gmail.com writes:
>>
>>>> I get an answer of 4301.199
>>>
>>> me too
>> Provided the implementation is correct that's what
>> you get with the Haversine method.
>
> (defun d2r (x) "degrees-to-radians is a macro, won't
> work with mapcar..."  (/ (* x pi) 180))
>
> ; wolfram's alpha says (setq average-earth-radius
> 6367.4447)
>
> ; there are sources that report different locations ;
> for Santiago and Easter Island (setq santiago (mapcar
> d2r '(33.4500 70.6667))) (setq easter_i (mapcar 'd2r
> (27.1167 109.3667)))
>
> (defun haversines (p1 p2) "Returns a list with
> sin^2(Delta_Lat/2) and sin^2(Delta_Lon/2).
> http://en.wikipedia.org/wiki/Haversine" (flet ((d2 (x)
> (/ x 2)) (p2 (x) (* x x))) (mapcar 'p2 (mapcar 'sin
> (mapcar 'd2 (mapcar* '- p1 p2))))))
>
> (defun central-angle (p1 p2) "Returns the central angle
> between two locations on a sphere, using the haversine
> formula.
> http://en.wikipedia.org/wiki/Great-circle_distance#Computational_formulas
> " (let* ( (hs (haversines p1 p2)) (hs-lat (car hs))
> (hs-lon (cadr hs)) (cos-lat1 (cos (car p1))) (cos-lat2
> (cos (car p2))) ) (* 2 (asin (sqrt (+ hs-lat (*
> cos-lat1 cos-lat2 hs-lon)))))))
>
> (insert (format "\n%f" (* 6378.1 (central-angle
> santiago easter_i))))
>
> (insert (format "\n%f" (* average-earth-radius
> (central-angle santiago easter_i))))

Can you see where the first attempt fails? That looks
like the way I perceived the formula but I get a bit
dizzy by all those mapcars - I have the subtraction in
the opposite order but that might be how we supply the
arguments. Get back to you - never ending story,
this...

-- 
underground experts united:
http://user.it.uu.se/~embe8573


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: distance from Easter Island to Chile
  2014-04-21 10:35           ` Emanuel Berg
@ 2014-04-21 10:50             ` Emanuel Berg
  2014-04-23  2:49               ` Emanuel Berg
  0 siblings, 1 reply; 20+ messages in thread
From: Emanuel Berg @ 2014-04-21 10:50 UTC (permalink / raw)
  To: help-gnu-emacs

Emanuel Berg <embe8573@student.uu.se> writes:

> Can you see where the first attempt fails? That looks
> like the way I perceived the formula but I get a bit
> dizzy by all those mapcars - I have the subtraction
> in the opposite order but that might be how we supply
> the arguments. Get back to you - never ending story,
> this...

Found it! The f's and l's were in the opposite (wrong)
places. Now this is 3758 as well. Everything in time...

Be sure to save your math functions. Might come in
handy some day (and not just for you).

(defun distance (l1-d f1-d l2-d f2-d)
  (interactive "nLatitude 1: \nnLongitude 1: \nnLatitude 2: \nnLongitude 2: ")
  (let ((l1 (degrees-to-radians l1-d))
        (f1 (degrees-to-radians f1-d))
        (l2 (degrees-to-radians l2-d))
        (f2 (degrees-to-radians f2-d)) )
    (message "%s"
             (* 2 6367.4447 ; Earth's radius
                (asin
                 (sqrt
                  (+ (sin2 (/ (- l2 l1) 2))
                     (* (cos l2) (cos l1) (sin2 (/ (- f2 f1) 2))) )))))))

(distance 33.4500    ; Santiago
          70.6667
          27.1167    ; Easter Island
          109.3667
          )          ; phew!

-- 
underground experts united:
http://user.it.uu.se/~embe8573


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: distance from Easter Island to Chile
  2014-04-21 10:50             ` Emanuel Berg
@ 2014-04-23  2:49               ` Emanuel Berg
  0 siblings, 0 replies; 20+ messages in thread
From: Emanuel Berg @ 2014-04-23  2:49 UTC (permalink / raw)
  To: help-gnu-emacs

Made this into a CLI tool that fetches data (with curl)
from Wikipedia.

Even if you don't care for the CLI, the Emacs code is
improved, too.

It is sort of *slow* because of the fetch, and the
start of Emacs, but perhaps the parsing is
heavy-handed, as well.

Try:

distance 'Easter Island' 'Santiago de Chile'
distance Peking Paris
# etc.

Pretty cool, huh?

I put all the files here:

http://user.it.uu.se/~embe8573/distance/

1) loc

#!/bin/zsh

args=$(echo $* | sed 's/ /%20/g')
url=https://en.wikipedia.org/w/index.php\?title=$args\&printable=yes
coords=$(curl -s $url | grep -o '"geo">[-.[:digit:]]*; [-.[:digit:]]*<' | head -n 1 | sed 's/\("geo">\|-\|;\|<\)//g')

2) distance

#!/bin/zsh

coords1=`loc $1`
coords2=`loc $2`
lat1=$(echo $coords1 | cut -d" " -f1)
long1=$(echo $coords1 | cut -d" " -f2)
lat2=$(echo $coords2 | cut -d" " -f1)
long2=$(echo $coords2 | cut -d" " -f2)
emacs -Q -script distance.el $1 $lat1 $long1 $2 $lat2 $long2

3) distance.el

(require 'cl)

(defun sin2 (p)
  (let ((sin-p (sin p)))
    (* sin-p sin-p) ))

(defun haversine (a b)
  (sin2 (/ (- a b) 2) ))

(setq earth-radius 6367.4447)

(defun distance (p1 p2)
  (let ((from  (car p1))
        (to    (car p2))
        (lat1  (degrees-to-radians (caadr  p1)))
        (long1 (degrees-to-radians (cadadr p1)))
        (lat2  (degrees-to-radians (caadr  p2)))
        (long2 (degrees-to-radians (cadadr p2))) )
    (message "%s -> %s: %.2f km" from to
             (* 2 earth-radius
                (asin
                 (sqrt
                  (+ (haversine lat2 lat1)
                     (* (cos lat2)
                        (cos lat1)
                        (haversine long2 long1) ))))))))

(let ((from  (elt argv 0))
      (lat1  (string-to-number (elt argv 1)))
      (long1 (string-to-number (elt argv 2)))
      (to    (elt argv 3))
      (lat2  (string-to-number (elt argv 4)))
      (long2 (string-to-number (elt argv 5))) )
  (distance (list from (list lat1 long1))
            (list to   (list lat2 long2)) ))

-- 
underground experts united:
http://user.it.uu.se/~embe8573


^ permalink raw reply	[flat|nested] 20+ messages in thread

end of thread, other threads:[~2014-04-23  2:49 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-04-20  0:03 distance from Easter Island to Chile Emanuel Berg
2014-04-20  3:55 ` Frank Stutzman
2014-04-20 15:14   ` Emanuel Berg
2014-04-20 22:39     ` giacomo.boffi
     [not found]     ` <mailman.19957.1398033917.10748.help-gnu-emacs@gnu.org>
2014-04-21  1:40       ` Emanuel Berg
2014-04-21  1:50         ` Emanuel Berg
2014-04-21  9:45         ` giacomo.boffi
     [not found]         ` <mailman.19981.1398073560.10748.help-gnu-emacs@gnu.org>
2014-04-21 10:35           ` Emanuel Berg
2014-04-21 10:50             ` Emanuel Berg
2014-04-23  2:49               ` Emanuel Berg
2014-04-21  2:16       ` Emanuel Berg
2014-04-20  4:44 ` Barry Margolin
2014-04-20 15:06   ` Emanuel Berg
2014-04-20  6:28 ` Eli Zaretskii
     [not found] ` <mailman.19905.1397975330.10748.help-gnu-emacs@gnu.org>
2014-04-20  7:01   ` Barry Margolin
2014-04-20  7:48     ` Eli Zaretskii
2014-04-20 10:26       ` Thorsten Jolitz
2014-04-20 11:03         ` Eli Zaretskii
2014-04-20 15:22     ` Emanuel Berg
2014-04-21  6:04       ` Emanuel Berg

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.