Let me start by saying math is NOT my strong point - by a long shot. I was asked to write a program calculating which locations from a given list (the co-ordinates given in degrees) are within 100km of a specific point. I was told to use the first formula on this page to calculate distance since this is more of a programming exercise than a mathematical one.
I calculated the central angle using the latitudes and longitudes in degress then converted to radians to calculate distance.
The problem is that my distances all seem very small. Here's the first few:
0.71099, 2.71675, 2.65812, 3.13579
I'm really confused now about units and what exactly the output I'm getting means. I think this is a maths issue rather than a programming one but if I'm wrong please let me know and I'll ask this on Stack Overflow instead.
Also, I hope I got the tag right!
update
I'm using ruby to write this program and here's my method
def distance
read_data_from_file
office_lat = 53.3381985
office_long = -6.2592576
# get and store all the central angles
@customers.each do |customer|
lat = (customer['latitude']).to_f
long = (customer['longitude']).to_f
@central_angles << Math.acos((Math.sin(office_lat) * Math.sin(lat)) +
(Math.cos(office_lat) * Math.cos(lat) * Math.cos(office_long - long)))
end
# now calculate distance using d = r * central_angle
# but we must convert central angles to radians first
@central_angles.each { |angle| @distances << 100 * to_rad(angle) }
puts @distances
end
read_data_from_file is just a method that pulls up all the customer information from a text file. The second to last line calculates distances and pushes them to an array. I used 100 for radius because we're looking for customers based within 100km of the office.
Be warned that
Math.acosgives its output in radians, and in the same wayMath.sinandMath.coswant their argument to be expressed in radians, not degrees. Moreover, in the $d=r \,\Delta\sigma$ formula, $r$ is the Earth radius.