Class: Geometry::CenteredRectangle
- Defined in:
- lib/geometry/rectangle.rb
Instance Attribute Summary collapse
-
#center ⇒ Point
The Rectangle‘s center.
- #size ⇒ Size
Attributes inherited from Rectangle
Accessors collapse
-
#edges ⇒ Array<Edge>
The Rectangle‘s four edges.
- #height ⇒ Object
-
#max ⇒ Point
The upper right corner of the bounding Rectangle.
-
#min ⇒ Point
The lower left corner of the bounding Rectangle.
-
#points ⇒ Array<Point>
The Rectangle‘s four points (clockwise).
- #width ⇒ Object
Instance Method Summary collapse
- #eql?(other) ⇒ Boolean (also: #==)
-
#initialize(*args) ⇒ CenteredRectangle
constructor
A new instance of CenteredRectangle.
Methods inherited from Rectangle
Methods included from ClusterFactory
Constructor Details
#new(width, height) ⇒ CenteredRectangle #new(size) ⇒ CenteredRectangle #new(center, size) ⇒ CenteredRectangle
Returns a new instance of CenteredRectangle.
230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/geometry/rectangle.rb', line 230 def initialize(*args) , args = args.partition {|a| a.is_a? Hash} = .reduce({}, :merge) @center = [:center] ? Point[[:center]] : PointZero.new if .has_key?(:size) @size = Geometry::Size[[:size]] elsif .has_key?(:height) and .has_key?(:width) @size = Geometry::Size[[:width], [:height]] else raise ArgumentError, "Bad arguments to CenteredRectangle#new" end end |
Instance Attribute Details
Instance Method Details
#edges ⇒ Array<Edge>
Returns The Rectangle‘s four edges.
252 253 254 255 256 257 258 259 260 261 |
# File 'lib/geometry/rectangle.rb', line 252 def edges 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 |
#eql?(other) ⇒ Boolean Also known as: ==
245 246 247 |
# File 'lib/geometry/rectangle.rb', line 245 def eql?(other) (self.center == other.center) && (self.size == other.size) end |
#height ⇒ Object
282 283 284 |
# File 'lib/geometry/rectangle.rb', line 282 def height @size.height end |
#max ⇒ Point
Returns The upper right corner of the bounding Rectangle.
264 265 266 |
# File 'lib/geometry/rectangle.rb', line 264 def max @center + @size/2.0 end |
#min ⇒ Point
Returns The lower left corner of the bounding Rectangle.
269 270 271 |
# File 'lib/geometry/rectangle.rb', line 269 def min @center - @size/2.0 end |
#points ⇒ Array<Point>
Returns The Rectangle‘s four points (clockwise).
274 275 276 277 278 279 280 |
# File 'lib/geometry/rectangle.rb', line 274 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 |
#width ⇒ Object
286 287 288 |
# File 'lib/geometry/rectangle.rb', line 286 def width @size.width end |