MnGCA Home MnGCA
Minnesota Geocaching Association
 
 FAQFAQ   SearchSearch   User listUser list   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

calculating distance

 
Post new topic   Reply to topic    MnGCA Forum Index -> Tech Talk
View previous topic :: View next topic  
Author Message
SirPoonga
Geocacher


Joined: 07 Feb 2003

Posts: 144

PostPosted: Wed Jun 09, 2004 10:23 pm    Post subject: calculating distance Reply with quote

I need to know how to calculate the distance between two waypoints, mathematically. I want to program something and need to know the mathematics behind it. I know it isn't simple because the somewhat spherical shape of the earth. I am having a hard time finding resources.
Back to top
View user's profile Send private message
rickrich
Geocacher


Joined: 06 Jul 2003

Posts: 673

PostPosted: Thu Jun 10, 2004 5:32 am    Post subject: Reply with quote

From my "geo-dist" program at http://geo.rkkda.com. This should be good enough for government work. If you want a more accurate answer, check out Gazza's geodetics calculator.

BTW, around Mpls .001 minute is about 1.8m (5.9ft) N-S, and 1.3m (4.26 feet) E-W.

Code:
#
# Rough calculation of distance on an ellipsoid
#
calc_dist_meters() {
    awk \
    -v LATmin=$1 \
    -v LONmin=$2 \
    -v LATmax=$3 \
    -v LONmax=$4 \
    '
    function calcR(lat)
    {
        lat = lat * M_PI_180
        sc = sin(lat)
        x = A * (1.0 - E2)
        z = 1.0 - E2 * sc * sc
        y = z ^ 1.5
        r = x / y
        return r * 1000.0
    }

    function asin(x)    { return atan2(x,(1.-x^2)^0.5) }

    function dist(lat1, lon1, lat2, lon2,
                    radiant, dlon, dlat, a1, a2, a, sa, c)
    {
        radiant = M_PI_180
        dlon = radiant * (lon1 - lon2);
        dlat = radiant * (lat1 - lat2);
        a1 = sin(dlat / 2.0)
        a2 = sin (dlon / 2.0)
        a = (a1 * a1) + cos(lat1 * radiant) * cos(lat2 * radiant) * a2 * a2
        sa = sqrt(a)
        if (sa <= 1.0)
            c = 2 * asin(sa)
        else
            c = 2 * asin(1.0)
        return (Ra[int(100 + lat2)] + Ra[int(100 + lat1)]) * c / 2.0;
    }

    BEGIN {
        A = 6378.137
        E2 = 0.081082 * 0.081082
        M_PI = 3.14159265358979323846
        M_PI_180 = M_PI / 180.0
        # Build array for earth radii
        for (i = -100; i <= 100; i++)
            Ra[i+100] = calcR(i)

        d = dist(LATmin, LONmin, LATmax, LONmax)
        printf "%d\n", d
    }
    '
}
Back to top
View user's profile Send private message Send e-mail
sui generis
Past MnGCA Board


Joined: 17 Apr 2004

Posts: 608

PostPosted: Thu Jun 10, 2004 5:44 am    Post subject: Reply with quote

....did you remember to carry the 1?
_________________
I am amazed by how many people harp on the need to speak and write English in this country while exhibiting a fundamental lack of skills in the areas of spelling and sentence composition. Would this be irony, hypocrisy, or both?
Back to top
View user's profile Send private message AIM Address
SirPoonga
Geocacher


Joined: 07 Feb 2003

Posts: 144

PostPosted: Sun Jun 13, 2004 10:07 pm    Post subject: Reply with quote

rick, I ggotta find the link to your site again, I know if I look around here enough I will find it. Do you have the source available for your programs? I have a tbug idea and I am working on a way to track how the tbugs spread using svg like gps visualizer. But I to know/figure out the algorithms used to figure out what size of map to use, etc.....
Back to top
View user's profile Send private message
Marsha and Silent Bob
Past MnGCA President


Joined: 02 Sep 2003

Posts: 6261

PostPosted: Mon Jun 14, 2004 5:30 am    Post subject: Reply with quote

SirPoonga wrote:
rick, I ggotta find the link to your site again, I know if I look around here enough I will find it. Do you have the source available for your programs? I have a tbug idea and I am working on a way to track how the tbugs spread using svg like gps visualizer. But I to know/figure out the algorithms used to figure out what size of map to use, etc.....

http://mngca.rkkda.com/

Yeah the source is there too.
_________________
Sad state of affairs.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    MnGCA Forum Index -> Tech Talk All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2002 phpBB Group
Geocaching Cache Icons, Copyright 2009, Groundspeak Inc. All rights reserved. Used with Permission.