Class: Geometry::Rectangle
- Inherits:
-
Object
- Object
- Geometry::Rectangle
- Includes:
- ClusterFactory
- Defined in:
- lib/geometry/rectangle.rb
Overview
The Rectangle class cluster represents your typical arrangement of 4 corners and 4 sides.
Usage
Constructors
rect = Rectangle.new [1,2], [2,3] # Using two corners
rect = Rectangle.new from:[1,2], to:[2,3] # Using two corners
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]
Direct Known Subclasses
Accessors collapse
Instance Attribute Summary collapse
- #size ⇒ Size readonly
Accessors collapse
-
#bounds ⇒ Rectangle
The smallest axis-aligned Rectangle that bounds the receiver.
-
#center ⇒ Point
The Rectangle‘s center.
-
#edges ⇒ Array<Edge>
The Rectangle‘s four edges (counterclockwise).
-
#height ⇒ Number
Height of the Rectangle.
-
#max ⇒ Point
The upper right corner of the bounding Rectangle.
-
#min ⇒ Point
The lower left corner of the bounding Rectangle.
-
#minmax ⇒ Array<Point>
The lower left and upper right corners of the bounding Rectangle.
-
#origin ⇒ Point
The Rectangle‘s origin.
-
#points ⇒ Array<Point>
The Rectangle‘s four points (clockwise).
-
#width ⇒ Number
Width of the Rectangle.
Class Method Summary collapse
Instance Method Summary collapse
- #eql?(other) ⇒ Boolean (also: #==)
- #initialize(point0, point1) ⇒ Rectangle constructor
-
#inset(*args) ⇒ Object
Create a new Rectangle from the receiver that’s inset by the given amount.
- #path ⇒ Path
Methods included from ClusterFactory
Constructor Details
#initialize(point0, point1) ⇒ Rectangle
Creates a Geometry::Rectangle using the given Points
@param [Point] point0 A corner (ie. bottom-left)
@param [Point] point1 The other corner (ie. top-right)
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/geometry/rectangle.rb', line 83 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
#closed? ⇒ Boolean
120 121 122 |
# File 'lib/geometry/rectangle.rb', line 120 def closed? true end |
#size ⇒ Size (readonly)
29 30 31 |
# File 'lib/geometry/rectangle.rb', line 29 def size @size 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
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/geometry/rectangle.rb', line 55 def self.new(*args) , args = args.partition {|a| a.is_a? Hash} = .reduce({}, :merge) if .has_key?(:size) if .has_key?(:center) CenteredRectangle.new(center: [:center], size: [:size]) elsif .has_key?(:origin) SizedRectangle.new(origin: [:origin], size: [:size]) else SizedRectangle.new(size: [:size]) end elsif .has_key?(:from) and .has_key?(:to) original_new([:from], [:to]) elsif .has_key?(:height) and .has_key?(:width) SizedRectangle.new(height: [:height], width: [:width]) elsif (2==args.count) and (args.all? {|a| a.is_a?(Array) || a.is_a?(Point) }) original_new(*args) elsif .empty? raise ArgumentError, "#{self} arguments must be named, not: #{args}" else raise ArgumentError, "Bad Rectangle arguments: #{args}, #{options}" end end |
Instance Method Details
#bounds ⇒ Rectangle
108 109 110 |
# File 'lib/geometry/rectangle.rb', line 108 def bounds return Rectangle.new(self.min, self.max) end |
#center ⇒ Point
113 114 115 116 |
# File 'lib/geometry/rectangle.rb', line 113 def center min, max = @points.minmax {|a,b| a.y <=> b.y} Point[(max.x+min.x).to_r/2, (max.y+min.y).to_r/2] end |
#edges ⇒ Array<Edge>
125 126 127 128 129 130 131 132 133 |
# File 'lib/geometry/rectangle.rb', line 125 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: ==
100 101 102 |
# File 'lib/geometry/rectangle.rb', line 100 def eql?(other) self.points == other.points end |
#height ⇒ Number
166 167 168 169 |
# File 'lib/geometry/rectangle.rb', line 166 def height min, max = @points.minmax {|a,b| a.y <=> b.y} max.y - min.y end |
#inset(x, y) ⇒ Object #inset(top, left, bottom, right) ⇒ Object #inset(x, y) ⇒ Object #inset(top, left, bottom, right) ⇒ Object
Create a new Geometry::Rectangle from the receiver that’s inset by the given amount
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/geometry/rectangle.rb', line 189 def inset(*args) , args = args.partition {|a| a.is_a? Hash} = .reduce({}, :merge) raise ArumentError, "Can't specify both arguments and options" if !args.empty? && !.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 [:x] && [:y] distance = Point[[:x], [:y]] Rectangle.new from:(min + distance), to:(max - distance) elsif [:top] && [:left] && [:bottom] && [:right] Rectangle.new from:(min + Point[[:left], [:bottom]]), to:(max - Point[[:right], [:top]]) end end |
#max ⇒ Point
136 137 138 |
# File 'lib/geometry/rectangle.rb', line 136 def max @points.last end |
#min ⇒ Point
141 142 143 |
# File 'lib/geometry/rectangle.rb', line 141 def min @points.first end |
#minmax ⇒ Array<Point>
146 147 148 |
# File 'lib/geometry/rectangle.rb', line 146 def minmax [self.min, self.max] end |
#origin ⇒ Point
159 160 161 162 163 |
# File 'lib/geometry/rectangle.rb', line 159 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 |
#path ⇒ Path
212 213 214 |
# File 'lib/geometry/rectangle.rb', line 212 def path Path.new(*self.points, self.points.first) end |
#points ⇒ Array<Point>
151 152 153 154 155 156 |
# File 'lib/geometry/rectangle.rb', line 151 def points point0, point2 = *@points point1 = Point[point0.x, point2.y] point3 = Point[point2.x, point0.y] [point0, point1, point2, point3] end |
#width ⇒ Number
172 173 174 175 |
# File 'lib/geometry/rectangle.rb', line 172 def width min, max = @points.minmax {|a,b| a.x <=> b.x} max.x - min.x end |