Forum: Ruby Re: Trouble migrating formula from JavaScript to Ruby

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6087a044557d6b59ab52e7dd20f94da8?d=identicon&s=25 Peña, Botp (Guest)
on 2006-04-27 12:23
(Received via mailing list)
Ingo Weiss:

# //JavaScript:
# dlng = p2_lng - p1_lng
# dlat = p2_lat - p1_lat
# a = (sin(dlat/2))^2 + cos(p1_lat) * cos(p2_lat) * (sin(dlon/2))^2
# c = 2 * atan2( sqrt(a), sqrt(1-a) )
# distance = R * c (where R is the radius of the Earth)
#
#
# #Ruby:
# dlng = p2_lng - p1_lng
# dlat = p2_lat - p1_lat
# a = (Math.sin(dlat/2))**2 + Math.cos(p1_lat) * Math.cos(p2_lat) *
# (Math.sin(dlng/2))**2
# c = 2 * Math.atan2( Math.sqrt(a), Math.sqrt(1-a) )
# distance = 6373 * c #(radius of the Earth: 6373km
#
#
# Any idea why the Ruby one returns incorrect numbers?

1) can you post sample running codes on both? (eg what are the values of
p2_lang, etc)

2) do you have test cases? (so we know what to expect)

kind regards -botp
91e1fb8bd265b7629491ab64c42f0906?d=identicon&s=25 Reid Thompson (Guest)
on 2006-04-27 12:45
(Received via mailing list)
Peña wrote:
> # #Ruby:
> # dlng = p2_lng - p1_lng
> # dlat = p2_lat - p1_lat
> # a = (Math.sin(dlat/2))**2 + Math.cos(p1_lat) * Math.cos(p2_lat) *
>
is dlng a typo, the first formula uses dlon?
3504745159d79c585e4f7df8df76aa10?d=identicon&s=25 Ingo Weiss (ingoweiss)
on 2006-04-27 17:35
Turned out that I needed to convert the numbers to radians. Here is the
code that worked:



#Calculate distance between 2 coordinate points (decimal lat/lng),
expressed in km
#This is an approximation since it assumes the earth to be a perfect
sphere
def calculate_distance(p1, p2)

  # radius_of_earth = 3963.0 #miles
  radius_of_earth = 6373.0 #kilometers

  #convert position to decimal and radians:
  to_rad = 180/Math::PI
  p1_lat = degmin_to_dec(p1.lat) / to_rad
  p1_lng = degmin_to_dec(p1.lng) / to_rad
  p2_lat = degmin_to_dec(p2.lat) / to_rad
  p2_lng = degmin_to_dec(p2.lng) / to_rad

  #calculate distance:
  distance = radius_of_earth * Math.acos(Math.sin(p1_lat) *
Math.sin(p2_lat) + Math.cos(p1_lat) * Math.cos(p2_lat) * Math.cos(p2_lng
- p1_lng))

end

Ingo
This topic is locked and can not be replied to.