Skip to content

Instantly share code, notes, and snippets.

@macuk
Created September 10, 2020 07:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save macuk/9b697845f0bdfb421c58869a32e882a6 to your computer and use it in GitHub Desktop.
Save macuk/9b697845f0bdfb421c58869a32e882a6 to your computer and use it in GitHub Desktop.
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