PDA

View Full Version : Cocoa 48-Hour Programming Challenge


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,

OneSadCookie
2004.01.28, 09:08 PM
eww, miles.

*looks for barfing face smiley*

Sohta
2004.01.28, 09:08 PM
:lol:

Damn you with your challenges.

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

diordna
2004.01.28, 09:12 PM
Er...not much warning, eh? Another "I-just-sprung-it-on-you" contest?

Carlos Camacho
2004.01.28, 09:29 PM
Think of it like a pop-quiz.

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

MattDiamond
2004.01.29, 12:22 AM
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. :-)

BeyondCloister
2004.01.29, 05:21 AM
Being professional about this, I thought I had better check the specs of this project before commencing it.

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

DoG
2004.01.29, 06:21 AM
You're on. It's been a long time since I've done any Cocoa hot-wiring.

geezusfreeek
2004.01.29, 09:47 AM
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!

MattDiamond
2004.01.29, 10:01 AM
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.)

geezusfreeek
2004.01.29, 10:08 AM
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?

DaFalcon
2004.01.29, 11:10 AM
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 :p

Of course if you do build in database support I'm sure you'd get all kinds of 'leet props :ninja: Or something :blush:

Carlos Camacho
2004.01.29, 06:39 PM
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 Camacho
2004.01.29, 06:41 PM
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. ;)

FreakSoftware
2004.01.29, 08:54 PM
10 bucks says Carlos needed an application to do this. :)

DoG
2004.01.29, 10:00 PM
Well, I got the basic distance algorithm worked out (great circle one), of course without the database part, as I could not find any database with cities and coordinates in it after some googling. But, I pulled a little Cocoa app out of my sleeve where you can enter 2 sets of lon/lat coordinates and it tells you the distance and a few useless things.

http://web.axelero.hu/g5d6/arculator.zip

geezusfreeek
2004.01.29, 10:26 PM
Would be useful if anyone here plans to go around the world and stay at the home of a forum member. ;)

Interesting idea..... Maybe.... I dunno about that.... Hmm.... Nah. I don't like any of you guys! :)

Iceman
2004.01.29, 11:56 PM
Suggest the next contest? April fools 48 hour Cocoa application contest. Ok sorry I just had to put that one in there :D.

Oather
2004.01.30, 01:43 AM
Hi everyone,
I've been lurking around on and off for a while, and saw this so I figured I'd give it a shot.

I got really into and threw in a quick 3D view :rolleyes:
Let me know what you think.

http://homepage.mac.com/edgare/.cv/edgare/Public/GlobeDistance.sit-link.sit

Carlos Camacho
2004.01.30, 07:46 AM
Actually, no I don't need such an app, since ArcGIS (a very very very expensive GIS) app I use does it. But I think some other geologist, people may bind it useful.

Two entries thus far?

WoW!

Playing with "GlobeDistance" now!

Great! 3D!

Why not make the two axis movement controls the same way? One is like a knob and one is a slider. Just thinking outloud. ;) Kilometers too! OSC will be happy! And low-and-behold, vectors for the locations, Bravo!

Now to look at "Arculator".
Looks like a Cocoa app. :D Ah, I see you formatted the values correctly (ie degrees symbols) Hours Flight as well?!? I'm impressed!

Between the two of these, I see some great programmers! Come, on who else is going to impress me?!?!? If you guys end up puttinf all your neat code together, you might have a really handy utility that can get 5 stars on even VersionTracker.com

IBethune
2004.01.30, 08:52 AM
GlobeDistance won't run on my machine - error log:

**********

Date/Time: 2004-01-30 13:51:15 +0000
OS Version: 10.2.8 (Build 6R73)
Host: Iain-Bethunes-G4.local.

Command: GlobeDistance
PID: 1903

Exception: EXC_BAD_ACCESS (0x0001)
Codes: KERN_PROTECTION_FAILURE (0x0002) at 0x0000000c

Thread 0 Crashed:
#0 0x909b3ccc in gluQuadricDrawStyle
#1 0x00008b14 in -[MyOpenGLView drawRect:]
#2 0x930809ec in -[NSView _drawRect:clip:]
#3 0x93096a34 in -[NSView _recursiveDisplayAllDirtyWithLockFocus:visRect:]
#4 0x93096bbc in -[NSView _recursiveDisplayAllDirtyWithLockFocus:visRect:]
#5 0x93096bbc in -[NSView _recursiveDisplayAllDirtyWithLockFocus:visRect:]
#6 0x930c8180 in -[NSFrameView _recursiveDisplayRectIfNeededIgnoringOpacity:isVis ibleRect:rectIsVisibleRectForView:topView:]
#7 0x930b2c14 in -[NSThemeFrame _recursiveDisplayRectIfNeededIgnoringOpacity:isVis ibleRect:rectIsVisibleRectForView:topView:]
#8 0x930963f4 in -[NSView _displayRectIgnoringOpacity:isVisibleRect:rectIsVi sibleRectForView:]
#9 0x930a5778 in -[NSView displayIfNeeded]
#10 0x930b5608 in -[NSWindow displayIfNeeded]
#11 0x930dc8b8 in -[NSWindow _reallyDoOrderWindow:relativeTo:findKey:forCounter :force:isModal:]
#12 0x930ad7b4 in -[NSIBObjectData nibInstantiateWithOwner:topLevelObjects:]
#13 0x931b32bc in loadNib
#14 0x930ea9b4 in +[NSBundle(NSNibLoading) _loadNibFile:nameTable:withZone:ownerBundle:]
#15 0x93149a5c in +[NSBundle(NSNibLoading) loadNibFile:externalNameTable:withZone:]
#16 0x9314981c in +[NSBundle(NSNibLoading) loadNibNamed:owner:]
#17 0x9315fbfc in NSApplicationMain
#18 0x0000823c in _start (crt.c:267)
#19 0x000080b0 in start

BeyondCloister
2004.01.30, 10:01 AM
Actually, no I don't need such an app, since ArcGIS (a very very very expensive GIS) app I use does it.

A very small world indeed as I too use that product.

I decided not to enter this contest for two reasons:

1. GIS stuff is what I do for a living so Mac is meant for playing and not work.
2. Someone decided to drive their car into the back of mine this morning so I'm not really in the mood for coding :(

geezusfreeek
2004.01.30, 10:46 AM
Carlos, next time you are going to start a short contest like this one, could you give it a couple days advance first? You don't have to say what objectives are in advance, just say when you will be posting them or something. That way everybody gets a heads up on when to be checking for the rules and objectives of the contest.

DoG
2004.01.30, 01:55 PM
Between the two of these, I see some great programmers! Come, on who else is going to impress me?!?!? If you guys end up puttinf all your neat code together, you might have a really handy utility that can get 5 stars on even VersionTracker.com

Well, after I saw GlobalDistance and the 3D stuff, and since I had some free time on my hands, AND because I needed to get my head clean, I fixed Arculator and added a nice OpenGL display. Arculator v2:

http://web.axelero.hu/g5d6/arculator2.zip

Don't push it or I will have to write a postmortem :wacko:

Oather
2004.01.30, 02:25 PM
Thanks for the feedback Camacho, I have to agree the GUI is pretty cumbersome. What I really need is one of those old Quickdraw3D views/controllers.

IBethune I'm guessing the crash is cause by 10.2. Unfortutaly I don't have the 10.2 sdk installed right now. I'm not doing error checking so it's quite possible it's something else.
You may be able to recomplie it with ProjectBuilder but it's not really worth the effort.

To bad I'm busy today or I'd try and fix things up and add some polish.

Love the degrees minutes seconds textboxes DoG, haven't got a chance to check out the 2nd version yet though.