Created
September 10, 2020 07:44
-
-
Save macuk/9b697845f0bdfb421c58869a32e882a6 to your computer and use it in GitHub Desktop.
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
class Arc | |
def initialize(from, to) | |
raise ArgumentError if invalid?(from) || invalid?(to) | |
@from = from | |
@to = to | |
end | |
def to_s | |
"#{arc} degrees from #{@from} to #{@to}" | |
end | |
def arc | |
return 360 if all_round? | |
result = @to - @from | |
result.negative? ? result + 360 : result | |
end | |
def visible?(angle) | |
raise ArgumentError if invalid?(angle) | |
return true if all_round? | |
if @from < @to | |
angle >= @from && angle <= @to | |
else | |
angle >= @from && angle < 360 || angle >= 0 && angle <= @to | |
end | |
end | |
private | |
def invalid?(angle) | |
angle.negative? || angle > 359.9 | |
end | |
def all_round? | |
@from == @to | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment