Distance between points

I needed the following methods for a small software tool written in Smalltalk to manage GPS points …

Point>>fromHHMMSS
 "^
 this method expects a point holding geographical coordinates in
  HH.MMSSxx and returns a point with decimal notation.

  (09.235926@53.580164) fromHHMMSS -> 9.39979444444444 @ 53.9671222222222

  For Berlin:

  (13.2400@52.3100) fromHHMMSS   -> 13.4 @ 52.5166666666667

 "

 | newX newY |

 newX := self x integerPart + (( (self x fractionPart * 100.0) integerPart * 60 + ((self x fractionPart * 100.0) fractionPart * 100.0)) / 3600.0).
 newY := self y integerPart + (( (self y fractionPart * 100.0) integerPart * 60 + ((self y fractionPart * 100.0) fractionPart * 100.0)) / 3600.0).

 ^newX@newY

Point>>angleBetweenPoint: aPoint
 "^ returns the angle between two geographical coordinates in radian

  Berlin and Tokio (example from wikipedia)

  ((13.24@52.31) fromHHMMSS angleBetweenPoint: (139.46@35.42) fromHHMMSS) ->  1.39993138153655 (-> 80.2101597699631 degrees)

 "

 | l1r b1r l2r b2r pi |

 pi := Float pi.
 l1r := self x * pi / 180.
 b1r := self y *  pi / 180.

 l2r := aPoint x * pi / 180.
 b2r := aPoint y * pi / 180.

 ^( b1r sin * b2r sin + (b1r cos * b2r cos * (l2r -l1r) cos)) arcCos

distanceFromGPSPoint: aPoint

 "^ returns the distance between two geographical points in meter. the receiver and the parameter is assumed to be in decimal notation

  ((09.235926@53.580164) fromHHMMSS distanceFromGPSPoint: (09.240231@53.575976) fromHHMMSS) => 80.3583734076564
  ((09.235926@53.580164) fromHHMMSS distanceFromGPSPoint: (09.40632@53.96428) ) => 80.3583734076564
 "

 ^6378137 * (self angleBetweenPoint: aPoint)
This entry was posted in Smalltalk. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s