Carlos Camacho
2004.01.28, 08:53 PM
I'll like to see how many of you are savvy with Cocoa. So here is a challenge for you to be completed in 48-hours.
* Place the link for your app
* We vote for the best
* You show your source code
* No prizes, just glory
* Only ObjectiveC entries
* The time is based on the time of this post.
Challenge: Calculate the Distance Using Latitude and Longitude between two locations.
Anything extra will help your chances, and should give you extra "ohhs and ahhhs" (ie, showing 3d vectors to show the distance, a 3d globe, entering of city names, etc)
Here is the info you need:
Calculating Distance Using Latitude and Longitude
In this database, the location of ZIP codes is defined in terms of degrees of
north latitude and degrees of west longitude. Because of the spherical shape
of the Earth, calculating the exact distance between two ZIP codes requires
the use of spherical geometry and trigonometric math functions. However, you can calculate an approximate distance using much simpler math functions. For many applications the approximate distance calculation provides sufficient accuracy with much less complexity.
The following approximate distance calculations are relatively simple, but can
produce distance errors of 10 percent of more. These approximate calculations are performed using latitude and longitude values in degrees, as defined in this database. The first approximation requires only simple math functions:
Approximate distance in miles = sqrt(x * x + y * y)
where x = 69.1 * (zip2.lat - zip1.lat)
and y = 53 * (zip2.lon - zip1.lon)
You can improve the accuracy of this approximate distance calculation by
adding the cosine math function:
Approximate distance in miles = sqrt(x * x + y * y)
where x = 69.1 * (zip2.lat - zip1.lat)
and y = 69.1 * (zip2.lon - zip1.lon) * cos(zip1.lat/57.3)
If you need greater accuracy, you must use the exact distance calculation. The
exact distance calculation requires use of spherical geometry, since the Earth
is a sphere. The exact distance calculation also requires a high level of
floating point mathematical accuracy - about 15 digits of accuracy (sometimes called "double-precision"). Many computer languages do not provide sufficient accuracy for this calculation. In addition, the trig math functions used in the exact calculation require conversion of the latitude and longitude values from degrees to radians. To convert latitude or longitude from degrees to radians, divide the latitude and longitude values in this database by 180/pi, or 57.2958. The radius of the Earth is assumed to be 6,371 kilometers, or 3,958.75 miles.
If you convert all latitude and longitude values in the database to radians
before the calulation, use this equation:
Exact distance in miles = 3958.75 * arccos[sin(zip1.lat) *
sin(zip2.lat) + cos(zip1.lat) *
cos(zip2.lat) * cos(zip2.lon - zip1.lon)]
If you do NOT first convert the latitude and longitude values in the database
to radians, you must include the degrees-to-radians conversion in the
calculation. Substituting degrees for radians, the calculation becomes:
Exact distance in miles = 3958.75 * arccos[sin(zip1.lat/57.2958) *
sin(zip2.lat/57.2958) +
cos(zip1.lat/57.2958) *
cos(zip2.lat/57.2958) *
cos(zip2.lon/57.2958 - zip1.lon/57.2958)]
If the computer language you are using has no arccosine function, you can
calculate the same result using the arctangent function, which most computer
languages do support. Use the following equation:
Exact distance in miles = 3958.75 * arctan[sqrt(1-x^2))/x]
where x = [sin(zip1.lat/57.2958) * sin(zip2.lat/57.2958)] +
[cos(zip1.lat/57.2958) *
cos(zip2.lat/57.2958) *
cos(zip2.lon/57.2958 - zip1.lon/57.2958)]
Using the latitude and longitude values provided by this database, you should
be able to obtain distance accuracy of approximately +/- 36 feet.
If your distance calculations produce wildly incorrect results, check for
these possible problems:
1. Did you convert the latitude and longitude values from degrees to
radians? Trigonometric math functions such as sine and cosine normally
require conversion of degrees to radians, as described above.
2. Are the equations implemented correctly with necessary parentheses?
Remember the old math precedence rule: MDAS - multiply, divide, add,
subtract.
3. Does your computer language provide sufficient mathematical accuracy?
Many languages simply do not provide the required floating point
precision. For best results, you need about 15 digits of accuracy.
Older versions of Basic, for example, often provide much less accuracy
than required for the exact distance calculation.
4. Did you retain decimal points in the latitude and longitude values?
When you imported the data into your database program, you may have
lost the decimal point during the importation of latitude and longitude
values.
Good Luck,
* Place the link for your app
* We vote for the best
* You show your source code
* No prizes, just glory
* Only ObjectiveC entries
* The time is based on the time of this post.
Challenge: Calculate the Distance Using Latitude and Longitude between two locations.
Anything extra will help your chances, and should give you extra "ohhs and ahhhs" (ie, showing 3d vectors to show the distance, a 3d globe, entering of city names, etc)
Here is the info you need:
Calculating Distance Using Latitude and Longitude
In this database, the location of ZIP codes is defined in terms of degrees of
north latitude and degrees of west longitude. Because of the spherical shape
of the Earth, calculating the exact distance between two ZIP codes requires
the use of spherical geometry and trigonometric math functions. However, you can calculate an approximate distance using much simpler math functions. For many applications the approximate distance calculation provides sufficient accuracy with much less complexity.
The following approximate distance calculations are relatively simple, but can
produce distance errors of 10 percent of more. These approximate calculations are performed using latitude and longitude values in degrees, as defined in this database. The first approximation requires only simple math functions:
Approximate distance in miles = sqrt(x * x + y * y)
where x = 69.1 * (zip2.lat - zip1.lat)
and y = 53 * (zip2.lon - zip1.lon)
You can improve the accuracy of this approximate distance calculation by
adding the cosine math function:
Approximate distance in miles = sqrt(x * x + y * y)
where x = 69.1 * (zip2.lat - zip1.lat)
and y = 69.1 * (zip2.lon - zip1.lon) * cos(zip1.lat/57.3)
If you need greater accuracy, you must use the exact distance calculation. The
exact distance calculation requires use of spherical geometry, since the Earth
is a sphere. The exact distance calculation also requires a high level of
floating point mathematical accuracy - about 15 digits of accuracy (sometimes called "double-precision"). Many computer languages do not provide sufficient accuracy for this calculation. In addition, the trig math functions used in the exact calculation require conversion of the latitude and longitude values from degrees to radians. To convert latitude or longitude from degrees to radians, divide the latitude and longitude values in this database by 180/pi, or 57.2958. The radius of the Earth is assumed to be 6,371 kilometers, or 3,958.75 miles.
If you convert all latitude and longitude values in the database to radians
before the calulation, use this equation:
Exact distance in miles = 3958.75 * arccos[sin(zip1.lat) *
sin(zip2.lat) + cos(zip1.lat) *
cos(zip2.lat) * cos(zip2.lon - zip1.lon)]
If you do NOT first convert the latitude and longitude values in the database
to radians, you must include the degrees-to-radians conversion in the
calculation. Substituting degrees for radians, the calculation becomes:
Exact distance in miles = 3958.75 * arccos[sin(zip1.lat/57.2958) *
sin(zip2.lat/57.2958) +
cos(zip1.lat/57.2958) *
cos(zip2.lat/57.2958) *
cos(zip2.lon/57.2958 - zip1.lon/57.2958)]
If the computer language you are using has no arccosine function, you can
calculate the same result using the arctangent function, which most computer
languages do support. Use the following equation:
Exact distance in miles = 3958.75 * arctan[sqrt(1-x^2))/x]
where x = [sin(zip1.lat/57.2958) * sin(zip2.lat/57.2958)] +
[cos(zip1.lat/57.2958) *
cos(zip2.lat/57.2958) *
cos(zip2.lon/57.2958 - zip1.lon/57.2958)]
Using the latitude and longitude values provided by this database, you should
be able to obtain distance accuracy of approximately +/- 36 feet.
If your distance calculations produce wildly incorrect results, check for
these possible problems:
1. Did you convert the latitude and longitude values from degrees to
radians? Trigonometric math functions such as sine and cosine normally
require conversion of degrees to radians, as described above.
2. Are the equations implemented correctly with necessary parentheses?
Remember the old math precedence rule: MDAS - multiply, divide, add,
subtract.
3. Does your computer language provide sufficient mathematical accuracy?
Many languages simply do not provide the required floating point
precision. For best results, you need about 15 digits of accuracy.
Older versions of Basic, for example, often provide much less accuracy
than required for the exact distance calculation.
4. Did you retain decimal points in the latitude and longitude values?
When you imported the data into your database program, you may have
lost the decimal point during the importation of latitude and longitude
values.
Good Luck,