Skip to content

Commit

Permalink
Calculates the location of a destination coord
Browse files Browse the repository at this point in the history
  • Loading branch information
mamantoha committed Apr 11, 2024
1 parent 2bbf46e commit a679024
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,19 @@ new_york.distance(london).to_kilometers
# => 5570.4744596620685
```

### Calculates the location of a destination coord

```crystal
require "geo"
require "geo/distance"
point = Geo::Coord.new(39, -75)
point.destination(5000, 90, :kilometers)
# Geo::Coord(@lat=26.440010707631124, @lng=-22.885355549364313)
```


## Contributing

1. Fork it (<https://github.com/geocrystal/geo/fork>)
Expand Down
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ crystal: ">= 1.0.0"
dependencies:
haversine:
github: geocrystal/haversine
version: ">= 0.4.0"
version: ">= 0.5.0"
convex_hull:
github: geocrystal/convex_hull
version: ">= 0.6.0"
Expand Down
7 changes: 7 additions & 0 deletions spec/geo/distance_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@ require "../../src/geo/distance"
describe Geo::Coord do
london = Geo::Coord.new(51.500153, -0.126236)
new_york = Geo::Coord.new(40.714268, -74.005974)
point = Geo::Coord.new(39, -75)

context "distance" do
context "calculates distance (by haversine formula)" do
it { london.distance(london).to_kilometers.should eq(0) }
it { new_york.distance(london).to_kilometers.should eq(5570.482153929098) }
end
end

context "destination" do
context "Calculates the location of a destination point (by haversine formula)" do
it { point.destination(5000, 90, :kilometers).should eq(Geo::Coord.new(26.440010707631124, -22.885355549364313)) }
end
end
end
7 changes: 7 additions & 0 deletions src/geo/distance.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,12 @@ module Geo
def distance(other : Geo::Coord) : Haversine::Distance
Haversine.distance(self.ll, other.ll)
end

# Calculates the location of a destination point
def destination(distance : Number, bearing : Number, unit : Symbol = :kilometers) : Geo::Coord
point = Haversine.destination(self.ll, distance, bearing, unit)

Geo::Coord.new(point[0], point[1])
end
end
end

0 comments on commit a679024

Please sign in to comment.