Permalink
Browse files

shape update

  • Loading branch information...
1 parent 3f7ffff commit 6cd270950175b3d826fa89512cf182391c69fc66 @kristianmandrup committed May 5, 2011
Showing with 26 additions and 4 deletions.
  1. +2 −0 lib/geo_magic/shape.rb
  2. +8 −1 lib/geo_magic/shape/rectangle.rb
  3. +16 −3 lib/geo_magic/shape/square.rb
View
2 lib/geo_magic/shape.rb
@@ -1,5 +1,7 @@
module GeoMagic
class Shape
+ attr_accessor :point_a
+
# is point within the square
def within? point
raise "Subclass should implement this"
View
9 lib/geo_magic/shape/rectangle.rb
@@ -2,9 +2,10 @@
module GeoMagic
class Rectangle < Shape
- attr_accessor :point_a, :point_b
+ attr_accessor :point_b
def initialize point_a, point_b
+ arg_check!(point_a, point_b)
@point_a = point_a
@point_b = point_b
end
@@ -102,6 +103,12 @@ def inside_top_left? point
def inside_bottom_right? point
bottom_right_point.latitude > point.latitude && bottom_right_point.longitude > point.longitude
end
+
+ private
+
+ def arg_check! p1, p2
+ raise ArgumentError, "Rectangle must be created from two Points or a Point and a Vector" if !([p1, p2].only_kinds_of?(GeoMagic::Point) || (p1.kind_of?(GeoMagic::Point) && p2.kind_of?(GeoMagic::Vector))
+ end
end
end
View
19 lib/geo_magic/shape/square.rb
@@ -4,10 +4,17 @@ module GeoMagic
class Square < Rectangle
attr_reader :distance
- def initialize point, distance
+ def initialize point, size
+ raise ArgumentError, "First argument must be a GeoMagic::Point" if !point.kind_of?(GeoMagic::Point)
+ raise ArgumentError, "Second argument must be a GeoMagic::Distance or a GeoMagic::Vector" if !size.any_kind_of?(GeoMagic::Distance, GeoMagic::Vector)
@point_a = point
- @distance = distance
- calculate_b
+
+ if size.kind_of?(GeoMagic::Distance)
+ @distance = size
+ calculate_b
+ else
+ raise "Square shape initializer must be fixed to alternatively accept a Vector arg"
+ end
end
def point
@@ -29,5 +36,11 @@ def distance= distance
def calculate_b
@point_b = @point_a.clone.move_diagonal distance
end
+
+ private
+
+ def arg_check! p1, p2
+ raise ArgumentError, "Rectangle must be created from two Points or a Point and a Vector" if !([p1, p2].only_kinds_of?(GeoMagic::Point) || (p1.kind_of?(GeoMagic::Point) && p2.kind_of?(GeoMagic::Vector))
+ end
end
end

0 comments on commit 6cd2709

Please sign in to comment.