Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculates the location of a destination coord #12

Merged
merged 1 commit into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading