lib/geometry/rectangle.rb in geometry-5 vs lib/geometry/rectangle.rb in geometry-6
- old
+ new
@@ -1,24 +1,27 @@
require_relative 'cluster_factory'
require_relative 'edge'
require_relative 'point'
+require_relative 'point_zero'
require_relative 'size'
module Geometry
=begin
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.new [1,2], [2,3] # Using two corners
+ rect = Rectangle.new from:[1,2], to:[2,3] # Using two corners
- rect = Rectangle[10, 20] # origin = [0,0], size = [10, 20]
- rect = Rectangle[Size[10, 20]] # origin = [0,0], size = [10, 20]
+ rect = Rectangle.new center:[1,2], size:[1,1] # Using a center point and a size
+ rect = Rectangle.new origin:[1,2], size:[1,1] # Using an origin point and a size
+ rect = Rectangle.new size: [10, 20] # origin = [0,0], size = [10, 20]
+ rect = Rectangle.new size: Size[10, 20] # origin = [0,0], size = [10, 20]
+ rect = Rectangle.new width: 10, height: 20 # origin = [0,0], size = [10, 20]
=end
class Rectangle
include ClusterFactory
@@ -42,12 +45,12 @@
# Creates a {Rectangle} of the given {Size} centered on the origin
# @param [Size] size Width and height
# @return [CenteredRectangle]
# @overload new(point0, point1)
# Creates a {Rectangle} using the given {Point}s
- # @param [Point,Array] point0 A corner
- # @param [Point,Array] point1 The other corner
+ # @param [Point] point0 A corner
+ # @param [Point] point1 The other corner
# @overload new(origin, size)
# Creates a {Rectangle} from the given origin and size
# @param [Point] origin Lower-left corner
# @param [Size] size Width and height
# @return [SizedRectangle]
@@ -56,25 +59,29 @@
# @param [Number] left X-coordinate of the left side
# @param [Number] bottom Y-coordinate of the bottom edge
# @param [Number] right X-coordinate of the right side
# @param [Number] top Y-coordinate of the top edge
def self.new(*args)
- case args.size
- when 1
- CenteredRectangle.new(args[0])
- when 2
- if args.all? {|a| a.is_a?(Numeric) }
- CenteredRectangle.new(Size[*args])
- elsif args.all? {|a| a.is_a?(Array) || a.is_a?(Point) }
- original_new(*args)
- elsif args[0].is_a?(Point) and args[1].is_a?(Size)
- SizedRectangle.new(*args)
- end
- when 4
- raise ArgumentError unless args.all? {|a| a.is_a?(Numeric)}
- left, bottom, right, top = *args
- original_new(Point[left, bottom], Point[right, top])
+ options, args = args.partition {|a| a.is_a? Hash}
+ options = options.reduce({}, :merge)
+
+ if options.has_key?(:size)
+ if options.has_key?(:center)
+ CenteredRectangle.new(center: options[:center], size: options[:size])
+ elsif options.has_key?(:origin)
+ SizedRectangle.new(origin: options[:origin], size: options[:size])
+ else
+ SizedRectangle.new(size: options[:size])
+ end
+ elsif options.has_key?(:from) and options.has_key?(:to)
+ original_new(options[:from], options[:to])
+ elsif options.has_key?(:height) and options.has_key?(:width)
+ SizedRectangle.new(height: options[:height], width: options[:width])
+ elsif (2==args.count) and (args.all? {|a| a.is_a?(Array) || a.is_a?(Point) })
+ original_new(*args)
+ else
+ raise ArgumentError, "Bad Rectangle arguments: #{args}, #{options}"
end
end
# Creates a {Rectangle} using the given {Point}s
# @param [Point] point0 A corner (ie. bottom-left)
@@ -86,44 +93,67 @@
# Reorder the points to get lower-left and upper-right
if (point0.x > point1.x) && (point0.y > point1.y)
point0, point1 = point1, point0
else
- if point0.x > point1.x
- point0.x, point1.x = point1.x, point0.x
- end
- if point0.y > point1.y
- point0.y, point1.y = point1.y, point0.y
- end
+ p0x, p1x = [point0.x, point1.x].minmax
+ p0y, p1y = [point0.y, point1.y].minmax
+ point0 = Point[p0x, p0y]
+ point1 = Point[p1x, p1y]
end
@points = [point0, point1]
end
+ def eql?(other)
+ self.points == other.points
+ end
+ alias :== :eql?
+
# @group Accessors
+ # @return [Rectangle] The smallest axis-aligned {Rectangle} that bounds the receiver
+ def bounds
+ return Rectangle.new(self.min, self.max)
+ end
+
# @return [Point] The {Rectangle}'s center
def center
min, max = @points.minmax {|a,b| a.y <=> b.y}
- Point[(max.x+min.x)/2.0, (max.y+min.y)/2.0]
+ Point[(max.x+min.x)/2, (max.y+min.y)/2]
end
- # @return [Array<Edge>] The {Rectangle}'s four edges
+ # @return [Array<Edge>] The {Rectangle}'s four edges (counterclockwise)
def edges
point0, point2 = *@points
- point1 = Point[point0.x,point2.y]
- point3 = Point[point2.x, point0.y]
+ point1 = Point[point2.x, point0.y]
+ point3 = Point[point0.x, point2.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)
+ # @return [Point] The upper right corner of the bounding {Rectangle}
+ def max
+ @points.last
+ end
+
+ # @return [Point] The lower left corner of the bounding {Rectangle}
+ def min
+ @points.first
+ end
+
+ # @return [Array<Point>] The lower left and upper right corners of the bounding {Rectangle}
+ def minmax
+ [self.min, self.max]
+ end
+
+ # @return [Array<Point>] The {Rectangle}'s four points (counterclockwise)
def points
point0, point2 = *@points
- point1 = Point[point0.x,point2.y]
- point3 = Point[point2.x, point0.y]
+ point1 = Point[point2.x, point0.y]
+ point3 = Point[point0.x, point2.y]
[point0, point1, point2, point3]
end
def origin
minx = @points.min {|a,b| a.x <=> b.x}
@@ -144,11 +174,10 @@
end
class CenteredRectangle < Rectangle
# @return [Point] The {Rectangle}'s center
attr_accessor :center
- attr_reader :origin
# @return [Size] The {Size} of the {Rectangle}
attr_accessor :size
# @overload new(width, height)
# Creates a {Rectangle} of the given width and height, centered on the origin
@@ -162,21 +191,29 @@
# @overload new(center, size)
# Creates a {Rectangle} with the given center point and size
# @param [Point] center
# @param [Size] size
def initialize(*args)
- if args[0].is_a?(Size)
- @center = Point[0,0]
- @size = args[0]
- elsif args[0].is_a?(Geometry::Point) and args[1].is_a?(Geometry::Size)
- @center, @size = args[0,1]
- elsif (2 == args.size) and args.all? {|a| a.is_a?(Numeric)}
- @center = Point[0,0]
- @size = Geometry::Size[*args]
+ options, args = args.partition {|a| a.is_a? Hash}
+ options = options.reduce({}, :merge)
+
+ @center = options[:center] ? Point[options[:center]] : PointZero.new
+
+ if options.has_key?(:size)
+ @size = Geometry::Size[options[:size]]
+ elsif options.has_key?(:height) and options.has_key?(:width)
+ @size = Geometry::Size[options[:width], options[:height]]
+ else
+ raise ArgumentError, "Bad arguments to CenteredRectangle#new"
end
end
+ def eql?(other)
+ (self.center == other.center) && (self.size == other.size)
+ end
+ alias :== :eql?
+
# @group Accessors
# @return [Array<Edge>] The {Rectangle}'s four edges
def edges
point0 = @center - @size/2.0
point2 = @center + @size/2.0
@@ -186,10 +223,20 @@
Edge.new(point1, point2),
Edge.new(point2, point3),
Edge.new(point3, point0)]
end
+ # @return [Point] The upper right corner of the bounding {Rectangle}
+ def max
+ @center + @size/2.0
+ end
+
+ # @return [Point] The lower left corner of the bounding {Rectangle}
+ def min
+ @center - @size/2.0
+ 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]
@@ -206,12 +253,10 @@
end
# @endgroup
end
class SizedRectangle < Rectangle
- # @return [Point] The {Rectangle}'s center
- attr_reader :center
# @return [Point] The {Rectangle}'s origin
attr_accessor :origin
# @return [Size] The {Size} of the {Rectangle}
attr_accessor :size
@@ -228,23 +273,31 @@
# Creates a {Rectangle} with the given origin point and size
# @param [Point] origin
# @param [Size] size
# @return SizedRectangle
def initialize(*args)
- if args[0].is_a?(Size)
- @origin = Point[0,0]
- @size = args[0]
- elsif args[0].is_a?(Geometry::Point) and args[1].is_a?(Geometry::Size)
- @origin, @size = args[0], args[1]
- elsif (2 == args.size) and args.all? {|a| a.is_a?(Numeric)}
- @origin = Point[0,0]
- @size = Geometry::Size[*args]
+ options, args = args.partition {|a| a.is_a? Hash}
+ options = options.reduce({}, :merge)
+
+ @origin = options[:origin] ? Point[options[:origin]] : PointZero.new
+
+ if options.has_key?(:size)
+ @size = Geometry::Size[options[:size]]
+ elsif options.has_key?(:height) and options.has_key?(:width)
+ @size = Geometry::Size[options[:width], options[:height]]
+ else
+ raise ArgumentError, "Bad arguments to SizeRectangle#new"
end
end
+ def eql?(other)
+ (self.origin == other.origin) && (self.size == other.size)
+ end
+ alias :== :eql?
# @group Accessors
+ # @return [Point] The {Rectangle}'s center
def center
@origin + @size/2
end
# @return [Array<Edge>] The {Rectangle}'s four edges
@@ -255,9 +308,19 @@
point3 = Point[point2.x, point0.y]
[Edge.new(point0, point1),
Edge.new(point1, point2),
Edge.new(point2, point3),
Edge.new(point3, point0)]
+ end
+
+ # @return [Point] The upper right corner of the bounding {Rectangle}
+ def max
+ @origin + @size
+ end
+
+ # @return [Point] The lower left corner of the bounding {Rectangle}
+ def min
+ @origin
end
# @return [Array<Point>] The {Rectangle}'s four points (clockwise)
def points
point0 = @origin