From 492c9dbd52fac466e8c313382ba6cf74d6790467 Mon Sep 17 00:00:00 2001 From: Brandon Fosdick Date: Thu, 14 Apr 2016 16:42:59 -0700 Subject: [PATCH] Added end_angle, radius, and start_angle to ThreePointArc --- lib/geometry/arc.rb | 23 ++++++++++++++++++++++- test/geometry/arc.rb | 17 +++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/lib/geometry/arc.rb b/lib/geometry/arc.rb index b534dee..74c5fc0 100644 --- a/lib/geometry/arc.rb +++ b/lib/geometry/arc.rb @@ -17,8 +17,15 @@ class Arc include ClusterFactory attr_reader :center + + # @return [Number] the radius of the {Arc} attr_reader :radius - attr_reader :start_angle, :end_angle + + # @return [Number] the starting angle of the {Arc} as radians from the x-axis + attr_reader :start_angle + + # @return [Number] the ending angle of the {Arc} as radians from the x-axis + attr_reader :end_angle # @overload new(center, start, end) # Create a new {Arc} given center, start and end {Point}s @@ -90,5 +97,19 @@ def initialize(center_point, start_point, end_point) # The end point of the {Arc} # @return [Point] alias :last :end + + def end_angle + a = (self.end - self.center) + Math.atan2(a.y, a.x) + end + + def radius + (self.start - self.center).magnitude + end + + def start_angle + a = (self.start - self.center) + Math.atan2(a.y, a.x) + end end end diff --git a/test/geometry/arc.rb b/test/geometry/arc.rb index dde29f0..b7119a4 100644 --- a/test/geometry/arc.rb +++ b/test/geometry/arc.rb @@ -23,3 +23,20 @@ end end end + +describe Geometry::ThreePointArc do + it 'must have an ending angle' do + arc = Geometry::ThreePointArc.new([0,0], [1,0], [0,1]) + arc.end_angle.must_equal Math::PI/2 + end + + it 'must have a radius' do + arc = Geometry::ThreePointArc.new([0,0], [1,0], [0,1]) + arc.radius.must_equal 1 + end + + it 'must have an starting angle' do + arc = Geometry::ThreePointArc.new([0,0], [1,0], [0,1]) + arc.start_angle.must_equal 0 + end +end \ No newline at end of file