Class: Euclidean::Rectangle

Inherits:
Object
  • Object
show all
Includes:
ClusterFactory
Defined in:
lib/euclidean/rectangle.rb

Overview

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]

Direct Known Subclasses

CenteredRectangle, SizedRectangle

Accessors collapse

Instance Attribute Summary collapse

Accessors collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ClusterFactory

included

Constructor Details

#initialize(point0, point1) ⇒ Rectangle

Creates a Euclidean::Rectangle using the given Points

Parameters:

  • point0 (Point)

    A corner (ie. bottom-left)

  • point1 (Point)

    The other corner (ie. top-right)

Raises:

  • (ArgumentError)


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# 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

Instance Attribute Details

#centerPoint (readonly)

Returns The Euclidean::Rectangle‘s center.

Returns:



25
26
27
# File 'lib/euclidean/rectangle.rb', line 25

def center
  @center
end

#heightNumber (readonly)

Returns Height of the Euclidean::Rectangle.

Returns:



27
28
29
# File 'lib/euclidean/rectangle.rb', line 27

def height
  @height
end

#originPoint (readonly)

Returns The Euclidean::Rectangle‘s origin.

Returns:



29
30
31
# File 'lib/euclidean/rectangle.rb', line 29

def origin
  @origin
end

#sizeSize (readonly)

Returns The Size of the Euclidean::Rectangle.

Returns:



31
32
33
# File 'lib/euclidean/rectangle.rb', line 31

def size
  @size
end

#widthNumber (readonly)

Returns Width of the Euclidean::Rectangle.

Returns:



33
34
35
# File 'lib/euclidean/rectangle.rb', line 33

def width
  @width
end

Class Method Details

.new(width, height) ⇒ CenteredRectangle .new(size) ⇒ CenteredRectangle .new(point0, point1) ⇒ Object .new(origin, size) ⇒ SizedRectangle .new(left, bottom, right, top) ⇒ Object

Overloads:



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# 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

Instance Method Details

#boundsRectangle

Returns The smallest axis-aligned Euclidean::Rectangle that bounds the receiver.

Returns:



112
113
114
# File 'lib/euclidean/rectangle.rb', line 112

def bounds
  return Rectangle.new(self.min, self.max)
end

#edgesArray<Edge>

Returns The Euclidean::Rectangle‘s four edges (counterclockwise).

Returns:



123
124
125
126
127
128
129
130
131
# 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) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


104
105
106
# File 'lib/euclidean/rectangle.rb', line 104

def eql?(other)
  self.points == other.points
end

#inset(x, y) ⇒ Object #inset(top, left, bottom, right) ⇒ Object #inset(x, y) ⇒ Object #inset(top, left, bottom, right) ⇒ Object

Create a new Euclidean::Rectangle from the receiver that’s inset by the given amount

Overloads:

  • #inset(x, y) ⇒ Object
  • #inset(top, left, bottom, right) ⇒ Object

Raises:

  • (ArumentError)


184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# 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

#maxPoint

Returns The upper right corner of the bounding Euclidean::Rectangle.

Returns:



139
140
141
# File 'lib/euclidean/rectangle.rb', line 139

def max
  @points.last
end

#minPoint

Returns The lower left corner of the bounding Euclidean::Rectangle.

Returns:



144
145
146
# File 'lib/euclidean/rectangle.rb', line 144

def min
  @points.first
end

#minmaxArray<Point>

Returns The lower left and upper right corners of the bounding Euclidean::Rectangle.

Returns:



149
150
151
# File 'lib/euclidean/rectangle.rb', line 149

def minmax
  [self.min, self.max]
end

#pointsArray<Point>

Returns The Euclidean::Rectangle‘s four points (counterclockwise).

Returns:



160
161
162
163
164
165
# 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