Calculating distance between 2 points - confused about radians

1.2k Views Asked by At

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.

2

There are 2 best solutions below

0
On BEST ANSWER

Be warned that Math.acos gives its output in radians, and in the same way Math.sin and Math.cos want their argument to be expressed in radians, not degrees. Moreover, in the $d=r \,\Delta\sigma$ formula, $r$ is the Earth radius.

1
On

Let there be any two arbitrary points on a sphere with center $O$ & a radius $R$ such that $\angle AOB=\alpha$ then draw a perpendicular say $ON$ from center $O$ to the straight line $AB$ to obtain a right $\triangle ONA$ thus we have $$\sin \frac{\angle AOB}{2}=\frac{\frac{\text{(great circle distance)}}{2}} {\text{radius}}$$ Now, the shortest distance between A & B $$\text{great circle distance between A & B}=R\sin \frac{\alpha}{2}$$

Where, the angle $\alpha$ can be taken in degree or in radians.