From a679024f55afdec1bee450502939274d38790567 Mon Sep 17 00:00:00 2001 From: Anton Maminov Date: Thu, 11 Apr 2024 11:07:26 +0300 Subject: [PATCH] Calculates the location of a destination coord --- README.md | 13 +++++++++++++ shard.yml | 2 +- spec/geo/distance_spec.cr | 7 +++++++ src/geo/distance.cr | 7 +++++++ 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d6fc590..126bde0 100644 --- a/README.md +++ b/README.md @@ -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 () diff --git a/shard.yml b/shard.yml index 19556b5..8f6e5ef 100644 --- a/shard.yml +++ b/shard.yml @@ -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" diff --git a/spec/geo/distance_spec.cr b/spec/geo/distance_spec.cr index 142ab54..b98a2a3 100644 --- a/spec/geo/distance_spec.cr +++ b/spec/geo/distance_spec.cr @@ -4,6 +4,7 @@ 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 @@ -11,4 +12,10 @@ describe Geo::Coord do 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 diff --git a/src/geo/distance.cr b/src/geo/distance.cr index 55a960a..932924f 100644 --- a/src/geo/distance.cr +++ b/src/geo/distance.cr @@ -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