-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from jonatasrancan/restruct
Restruct
- Loading branch information
Showing
8 changed files
with
173 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
1.0.0 | ||
1.1.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
module Navigate | ||
private | ||
|
||
class Bearing | ||
def self.calculate(*args) | ||
new(*args).calculate | ||
end | ||
|
||
def initialize(position1, position2) | ||
valid_args?(position1, position2) | ||
|
||
@latitude1 = position1.latitude.to_radians | ||
@longitude1 = position1.longitude.to_radians | ||
@latitude2 = position2.latitude.to_radians | ||
@longitude2 = position2.longitude.to_radians | ||
end | ||
|
||
def calculate | ||
dLon = longitude2 - longitude1 | ||
|
||
y = Math::sin(dLon) * Math::cos(latitude2) | ||
x = Math::cos(latitude1) * Math::sin(latitude2) - | ||
Math::sin(latitude1) * Math::cos(latitude2) * Math::cos(dLon) | ||
|
||
Math::atan2(y, x).to_bearing | ||
end | ||
|
||
private | ||
|
||
attr_reader :latitude1, :latitude2, :longitude1, :longitude2 | ||
|
||
def valid_args?(position1, position2) | ||
raise 'First arg, must be a Navigate::Position instance' unless position1.is_a?(Navigate::Position) | ||
raise 'Second arg, must be a Navigate::Position instance' unless position2.is_a?(Navigate::Position) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
module Navigate | ||
private | ||
|
||
class Destination | ||
def self.calculate(*args) | ||
new(*args).calculate | ||
end | ||
|
||
def initialize(position, options = {}) | ||
valid_args?(position, options[:bearing]) | ||
|
||
@latitude = position.latitude.to_radians | ||
@longitude = position.longitude.to_radians | ||
@radius = EARTH_RADIUS[options[:radius_unit] || 'KM'] | ||
@bearing = options[:bearing] || 90.0 | ||
@distance = options[:distance] || 100.0 | ||
end | ||
|
||
def calculate | ||
new_lat = Math::asin( | ||
Math::sin(latitude) * Math::cos(distance / radius) + | ||
Math::cos(latitude) * Math::sin(distance / radius) * | ||
Math::cos(bearing) | ||
) | ||
|
||
new_lon = longitude + Math::atan2( | ||
Math::sin(bearing) * Math::sin(distance / radius) * Math::cos(latitude), | ||
Math::cos(distance / radius) - Math::sin(latitude) * Math::sin(new_lat) | ||
) | ||
|
||
new_lon = (new_lon + Math::PI) % (2 * Math::PI) - Math::PI | ||
|
||
Position.new new_lat.to_degrees, new_lon.to_degrees | ||
end | ||
|
||
private | ||
|
||
attr_reader :latitude, :longitude, :radius, :bearing, :distance | ||
|
||
def valid_args?(position, bearing) | ||
raise 'First arg, must be a Navigate::Position instance' unless position.is_a?(Navigate::Position) | ||
raise 'Bearing must be between 0.0 and 360.0' if bearing && !(0.0..360.0).include?(bearing) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
module Navigate | ||
private | ||
|
||
class DistanceBetween | ||
def self.calculate(*args) | ||
new(*args).calculate | ||
end | ||
|
||
def initialize(position1, position2, radius_unit) | ||
valid_args?(position1, position2) | ||
|
||
@latitude1 = position1.latitude.to_radians | ||
@longitude1 = position1.longitude.to_radians | ||
@latitude2 = position2.latitude.to_radians | ||
@longitude2 = position2.longitude.to_radians | ||
@radius_unit = EARTH_RADIUS[radius_unit || 'KM'] | ||
end | ||
|
||
def calculate | ||
Math::acos( | ||
Math::sin(latitude1) * Math::sin(latitude2) + | ||
Math::cos(latitude1) * Math::cos(latitude2) * | ||
Math::cos(longitude2 - longitude1) | ||
) * radius_unit | ||
end | ||
|
||
private | ||
|
||
attr_reader :latitude1, :latitude2, :longitude1, :longitude2, :radius_unit | ||
|
||
def valid_args?(position1, position2) | ||
raise 'First arg, must be a Navigate::Position instance' unless position1.is_a?(Navigate::Position) | ||
raise 'Second arg, must be a Navigate::Position instance' unless position2.is_a?(Navigate::Position) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,8 +22,6 @@ def valid_longitude? | |
def valid? | ||
valid_latitude? | ||
valid_longitude? | ||
|
||
true | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters