Permalink
Browse files

Added a simple Circle class

  • Loading branch information...
1 parent 4c1afcd commit 698872d96b6b4e6a5a514e3a20fd27faae720ce5 @bfoz committed Apr 21, 2012
Showing with 48 additions and 0 deletions.
  1. +1 −0 lib/geometry.rb
  2. +24 −0 lib/geometry/circle.rb
  3. +23 −0 test/geometry/circle.rb
View
@@ -1,3 +1,4 @@
+require_relative 'geometry/circle'
require_relative 'geometry/point'
require_relative 'geometry/line'
require_relative 'geometry/polygon'
View
@@ -0,0 +1,24 @@
+require_relative 'point'
+
+module Geometry
+
+=begin rdoc
+Circles come in all shapes and sizes, but they're usually round.
+
+== Usage
+ circle = Geometry::Circle [1,1], 2
+=end
+
+ class Circle
+ attr_reader :center, :radius
+
+ # Construct a new {Circle} from a centerpoint and radius
+ # @param [Point] center The center point of the Circle
+ # @param [Number] radius The radius of the Circle
+ # @return [Circle] A new Circle object
+ def initialize(center, radius)
+ @center = center.is_a?(Point) ? center : Point[center]
+ @radius = radius
+ end
+ end
+end
View
@@ -0,0 +1,23 @@
+require_relative '../helper'
+require_relative '../../lib/geometry/circle'
+
+def Circle(*args)
+ Geometry::Circle.new(*args)
+end
+
+class CircleTest < Test::Unit::TestCase
+ must "create a Circle object from a Point and a radius" do
+ circle = Circle [1,2], 3
+ assert_kind_of(Geometry::Circle, circle)
+ end
+
+ must "have a center point accessor" do
+ circle = Circle [1,2], 3
+ assert_equal(circle.center, [1,2])
+ end
+
+ must "have a radius accessor" do
+ circle = Circle [1,2], 3
+ assert_equal(3, circle.radius)
+ end
+end

0 comments on commit 698872d

Please sign in to comment.