lib/geometry/rectangle.rb in geometry-3 vs lib/geometry/rectangle.rb in geometry-4
- old
+ new
@@ -7,16 +7,16 @@
The {Rectangle} class cluster represents your typical arrangement of 4 corners and 4 sides.
== Usage
=== Constructors
- rect = Rectangle[[1,2], [2,3]] # Using two corners
- rect = Rectangle[[1,2], Size[1,1]] # Using origin and size
- rect = Rectangle[1,2,2,3] # Using four sides
+ rect = Rectangle[[1,2], [2,3]] # Using two corners
+ rect = Rectangle[[1,2], Size[1,1]] # Using origin and size
+ rect = Rectangle[1,2,2,3] # Using four sides
- rect = Rectangle[10, 20] # origin = [0,0], size = [10, 20]
- rect = Rectangle[Size[10, 20]] # origin = [0,0], size = [10, 20]
+ rect = Rectangle[10, 20] # origin = [0,0], size = [10, 20]
+ rect = Rectangle[Size[10, 20]] # origin = [0,0], size = [10, 20]
=end
class Rectangle
# @return [Point] The {Rectangle}'s center
@@ -79,12 +79,12 @@
# Creates a {Rectangle} using the given {Point}s
# @param [Point] point0 A corner (ie. bottom-left)
# @param [Point] point1 The other corner (ie. top-right)
def initialize(point0, point1)
- point0 = point0.is_a?(Point) ? point0 : Point[point0]
- point1 = point1.is_a?(Point) ? point1 : Point[point1]
+ point0 = Point[point0]
+ point1 = Point[point1]
raise(ArgumentError, "Point sizes must match") unless point0.size == point1.size
# Reorder the points to get lower-left and upper-right
if (point0.x > point1.x) && (point0.y > point1.y)
point0, point1 = point1, point0
@@ -98,13 +98,15 @@
end
@points = [point0, point1]
end
# @group Accessors
+
+ # @return [Point] The {Rectangle}'s center
def center
min, max = @points.minmax {|a,b| a.y <=> b.y}
- Point[(max.x+min.x)/2, (max.y+min.y)/2]
+ Point[(max.x+min.x)/2.0, (max.y+min.y)/2.0]
end
# @return [Array<Edge>] The {Rectangle}'s four edges
def edges
point0, point2 = *@points
@@ -114,10 +116,18 @@
Edge.new(point1, point2),
Edge.new(point2, point3),
Edge.new(point3, point0)]
end
+ # @return [Array<Point>] The {Rectangle}'s four points (clockwise)
+ def points
+ point0, point2 = *@points
+ point1 = Point[point0.x,point2.y]
+ point3 = Point[point2.x, point0.y]
+ [point0, point1, point2, point3]
+ end
+
def origin
minx = @points.min {|a,b| a.x <=> b.x}
miny = @points.min {|a,b| a.y <=> b.y}
Point[minx.x, miny.y]
end
@@ -171,20 +181,29 @@
end
# @group Accessors
# @return [Array<Edge>] The {Rectangle}'s four edges
def edges
- point0 = @center - @size/2
- point2 = @center + @size/2
+ point0 = @center - @size/2.0
+ point2 = @center + @size/2.0
point1 = Point[point0.x,point2.y]
point3 = Point[point2.x, point0.y]
[Edge.new(point0, point1),
Edge.new(point1, point2),
Edge.new(point2, point3),
Edge.new(point3, point0)]
end
+ # @return [Array<Point>] The {Rectangle}'s four points (clockwise)
+ def points
+ point0 = @center - @size/2.0
+ point2 = @center + @size/2.0
+ point1 = Point[point0.x,point2.y]
+ point3 = Point[point2.x, point0.y]
+ [point0, point1, point2, point3]
+ end
+
def height
@size.height
end
def width
@@ -245,9 +264,18 @@
point3 = Point[point2.x, point0.y]
[Edge.new(point0, point1),
Edge.new(point1, point2),
Edge.new(point2, point3),
Edge.new(point3, point0)]
+ end
+
+ # @return [Array<Point>] The {Rectangle}'s four points (clockwise)
+ def points
+ point0 = @origin
+ point2 = @origin + @size
+ point1 = Point[point0.x,point2.y]
+ point3 = Point[point2.x, point0.y]
+ [point0, point1, point2, point3]
end
def height
@size.height
end