Saturday, November 22, 2008

It all starts with the math...

Okay, maybe not, but I thought that for this project, this would be a major part of the functionality: calculating the distance between two points. How exactly do you calculate the distance between to coordinates? Well, you can use some basic math to figure it out... if the earth was flat. Then there is the spherical formula, which would be nice, if the earth was a perfect sphere. What we end up with is a spheroid (sphere flattened at the poles), and the best formula to use for this (as per my Google research) is the Haversine formula.

To use the Haversine formula, you have to make sure your coordinates are in radians, not degrees. That's simple: radians = PI / 180 * degrees. How do you go from 30° 13' 4.07" N to something the formula can use : 30.21779722222222? You parse the string. Everything is related to 1 hour. So 30 / 1 + 13 / 60 + 4.07 / 3600 gives you the 30.21779722222222 and the N gives you the sign (+). If it was South or West, it would have been negative. The formula returns miles, so if I want yards, I multiply the result by 1760 and if I want feet, I multiply by 5280.

Now that I had figured out the formulas, I set out to create a class that would handle this work. I create a class called Haversine (after the formula used) and create a Distance method that returns a double and takes in 3 parameters: Position 1, Position 2 & Distance Type. Positions are a structure containing 2 double values for latitude and longitude. I also create a method that converts degrees into radians and vice versa. The last method is parseReg which takes a string. Basically it converts a value (30° 13' 4.07" N) that is degrees, minutes, seconds & direction into a double (30.21779722222222).

To test this puppy out, I use coordinates from my favorite golf hole at NAS, Red Hole # 1. I take the coordinates of the center of the green and the coordinates of the 200 yard marker. So the center of the green's coordinates are 30 13 4.07N latitude by 81 40 58.55W longitude. The 200 yard marker's coordinates are 30 12 58.18N latitude by 81 40 57.89W longitude. Plug those in the class and I get a result of 199.951410648 yards. For golf application, I'm willing to take this as pretty accurate, especially since the coordinates for the hole were captured off of Google Earth's site, not physical reading on the course.

Well, I'm very happy with the progress made today. I have a class that can handle all the math behind calculating the distance between to points in either yards or feet. The next hard part will be to use actual satellite pictures (probably from Google Earth's site), get coordinates from all 4 corners of the picture and then be able to use those coordinates to be figure out the distance between a point on that picture and the current GPS device location. To explain, let's say I'm looking at a bird's eye view of the hole I'm on. I'm on the tee box and I want to know how far is it to reach that creek on the left. I click on the screen where I see the end of the creek on the left and based on my current coordinates and the coordinates of where I clicked, it tells me that it's 187 yards out. That's the next goal.

Until next time,

Sebastien Limoges

The code of the Haversine class is available for download below.

No comments:

Post a Comment