class Euclidean::Rectangle

The {Rectangle} class cluster represents your typical arrangement of 4 corners and 4 sides.

Usage

Constructors

rect = Euclidean::Rectangle.new [1,2], [2,3]                    # Using two corners
rect = Euclidean::Rectangle.new from:[1,2], to:[2,3]            # Using two corners

rect = Euclidean::Rectangle.new center:[1,2], size:[1,1]   # Using a center point and a size
rect = Euclidean::Rectangle.new origin:[1,2], size:[1,1]   # Using an origin point and a size

rect = Euclidean::Rectangle.new size: [10, 20]         # origin = [0,0], size = [10, 20]
rect = Euclidean::Rectangle.new size: Size[10, 20]  # origin = [0,0], size = [10, 20]
rect = Euclidean::Rectangle.new width: 10, height: 20       # origin = [0,0], size = [10, 20]

Attributes

center[R]

@return [Point] The {Rectangle}‘s center

height[R]

@return [Number] Height of the {Rectangle}

origin[R]

@return [Point] The {Rectangle}‘s origin

size[R]

@return [Size] The {Size} of the {Rectangle}

width[R]

@return [Number] Width of the {Rectangle}

Public Class Methods

new(*args) click to toggle source

@overload new(width, height)

Creates a {Rectangle} of the given width and height, centered on the origin
@param [Number]   height  Height
@param [Number]   width   Width
@return [CenteredRectangle]

@overload new(size)

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]    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]

@overload new(left, bottom, right, top)

Creates a {Rectangle} from the locations of each side
@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
# File lib/euclidean/rectangle.rb, line 59
  def self.new(*args)
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)
elsif options.empty?
  raise ArgumentError, "#{self} arguments must be named, not: #{args}"
else
  raise ArgumentError, "Bad Rectangle arguments: #{args}, #{options}"
end
  end
new(point0, point1) click to toggle source

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)

# File lib/euclidean/rectangle.rb, line 87
def initialize(point0, 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
  else
    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

Public Instance Methods

==(other)
Alias for: eql?
bounds() click to toggle source

@return [Rectangle] The smallest axis-aligned {Rectangle} that bounds the receiver

# File lib/euclidean/rectangle.rb, line 112
def bounds
  return Rectangle.new(self.min, self.max)
end
edges() click to toggle source

@return [Array<Edge>] The {Rectangle}‘s four edges (counterclockwise)

# File lib/euclidean/rectangle.rb, line 123
def edges
  point0, point2 = *@points
  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
eql?(other) click to toggle source
# File lib/euclidean/rectangle.rb, line 104
def eql?(other)
  self.points == other.points
end
Also aliased as: ==
inset(*args) click to toggle source

Create a new {Rectangle} from the receiver that’s inset by the given amount @overload inset(x, y) @overload inset(top, left, bottom, right) @overload inset(x, y)

@option options [Number] :x          Inset from the left and right sides
@option options [Number] :y          Inset from the top and bottom

@overload inset(top, left, bottom, right)

@option options [Number] :bottom The inset from the bottom of the {Rectangle}
@option options [Number] :left   The inset from the left side of the {Rectangle}
@option options [Number] :right  The inset from the right side of the {Rectangle}
@option options [Number] :top    The inset from the top of the {Rectangle}
# File lib/euclidean/rectangle.rb, line 184
def inset(*args)
  options, args = args.partition {|a| a.is_a? Hash}
  options = options.reduce({}, :merge)
  raise ArumentError, "Can't specify both arguments and options" if !args.empty? && !options.empty?

  if 1 == args.size
    distance = args.shift
    Rectangle.new from:(min + distance), to:(max - distance)
  elsif 2 == args.size
    distance = Point[*args]
    Rectangle.new from:(min + distance), to:(max - distance)
  elsif 4 == args.size
    top, left, bottom, right = *args
    Rectangle.new from:(min + Point[left, bottom]), to:(max - Point[right, top])
  elsif options[:x] && options[:y]
    distance = Point[options[:x], options[:y]]
    Rectangle.new from:(min + distance), to:(max - distance)
  elsif options[:top] && options[:left] && options[:bottom] && options[:right]
    Rectangle.new from:(min + Point[options[:left], options[:bottom]]), to:(max - Point[options[:right], options[:top]])
  end
end
max() click to toggle source

@return [Point] The upper right corner of the bounding {Rectangle}

# File lib/euclidean/rectangle.rb, line 139
def max
  @points.last
end
min() click to toggle source

@return [Point] The lower left corner of the bounding {Rectangle}

# File lib/euclidean/rectangle.rb, line 144
def min
  @points.first
end
minmax() click to toggle source

@return [Array<Point>] The lower left and upper right corners of the bounding {Rectangle}

# File lib/euclidean/rectangle.rb, line 149
def minmax
  [self.min, self.max]
end
points() click to toggle source

@return [Array<Point>] The {Rectangle}‘s four points (counterclockwise)

# File lib/euclidean/rectangle.rb, line 160
def points
  point0, point2 = *@points
  point1 = Point[point2.x, point0.y]
  point3 = Point[point0.x, point2.y]
  [point0, point1, point2, point3]
end