## Cocoa 48-Hour Programming Challenge

Founder
Posts: 1,139
Joined: 2002.04
Post: #1
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.

* 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:
Quote: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

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
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,

Carlos A. Camacho,
Founder
iDevGames
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
eww, miles.

*looks for barfing face smiley*
Member
Posts: 72
Joined: 2006.10
Post: #3

I'm gonna ruin my session 'cause of this... But.. I.. can't... resist...

- Sohta
Moderator
Posts: 683
Joined: 2002.11
Post: #4
Er...not much warning, eh? Another "I-just-sprung-it-on-you" contest?

My web site - Games, music, Python stuff
Founder
Posts: 1,139
Joined: 2002.04
Post: #5
Think of it like a pop-quiz.

OSC... extra ohhs for people who know kilometers and miles.

Carlos A. Camacho,
Founder
iDevGames
Moderator
Posts: 439
Joined: 2002.09
Post: #6
Carlos, next time you should give them 48 hours to write a program that, given a bunch of wishlists of winning developers, will calculate who gets what. And then contacts each of the sponsors automatically. :-)

Measure twice, cut once, curse three or four times.
Moderator
Posts: 592
Joined: 2002.12
Post: #7

The app you are after takes 2 points as input and returns the distance between the two.

The talk of databases is not relevant, just it happens to be in the reference document you provided?

(Now I just wonder if this is going to be so realistic that the client does not respond until the deadline is 1 hour away )
Moderator
Posts: 869
Joined: 2003.01
Post: #8
You're on. It's been a long time since I've done any Cocoa hot-wiring.
Member
Posts: 201
Joined: 2002.06
Post: #9
No can do this time. But... I love things like this. I think these are exactly the sorts of things we need to see more often here. Do it more!
Moderator
Posts: 439
Joined: 2002.09
Post: #10
geezusfreeek Wrote:No can do this time. But... I love things like this. I think these are exactly the sorts of things we need to see more often here. Do it more!
How about: winner of this challenge gets to help pick the next 48-hour challenge?

(Caveats: Carlos gets final say over the appropriateness of the challenge. And people who set the challenge have to sit it out.)

Measure twice, cut once, curse three or four times.
Member
Posts: 201
Joined: 2002.06
Post: #11
How about each time somebody wins one he is handicapped a couple of hours for later ones (he has to send it in sooner)? Keep the competition going a little more? Let people other than the super-elite have a chance?
Member
Posts: 328
Joined: 2002.04
Post: #12
BeyondCloister Wrote:The app you are after takes 2 points as input and returns the distance between the two.

The talk of databases is not relevant, just it happens to be in the reference document you provided?

Everything seems to imply that the program will just take coordinates and return a distance. Carlos doesn't provide you with zip codes and corresponding coordinates so I don't think he expects you to use a database, and that it is indeed just part of the reference document. But listening to me would be taking advice from someone who knows no more than you do, so you can still cross your fingers and hope Carlos replies before the deadline

Of course if you do build in database support I'm sure you'd get all kinds of 'leet props Or something
Founder
Posts: 1,139
Joined: 2002.04
Post: #13
Use what you can, or what you want. I'm very flexible. As for voting, place your link to download and after we all try them, we can all vote on them.

I admit that this idea came to me in about 30 seconds. I was searching for GIS data for Mexico, since I have to make various GIS maps of Mexico for my company's next project. And so I came across this, and thought, "that would make for an interesting programming challenge."

I'm keen to run more of them in the future, and will think them through in advance much more. Having people suggest the next contest is good. I have a long list of stuff that I would like to see worked on. Little stuff that can perhaps be useful to learn from, for game programming.

Anyhow, good luck. If you have a question on something that is not clear, like the database, simply state what you think, and if everyone agrees, then we make it "so."

Cheers,

Carlos A. Camacho,
Founder
iDevGames
Founder
Posts: 1,139
Joined: 2002.04
Post: #14
I was just thinking -- wouldn't it be cool to use such a little app, anjd then enter everyone's lat/long. and then we all can see who is nearest to us, and who is furthest from us. Would be useful if anyone here plans to go around the world and stay at the home of a forum member.

Carlos A. Camacho,
Founder
iDevGames
⌘-R in Chief
Posts: 1,265
Joined: 2002.05
Post: #15
10 bucks says Carlos needed an application to do this.