Class: Geometry::SizedRectangle
- Defined in:
- lib/geometry/rectangle.rb
Instance Attribute Summary collapse
-
#origin ⇒ Point
The Rectangle‘s origin.
- #size ⇒ Size
Attributes inherited from Rectangle
Accessors collapse
-
#center ⇒ Point
The Rectangle‘s center.
-
#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) ⇒ SizedRectangle
constructor
A new instance of SizedRectangle.
Methods inherited from Rectangle
Methods included from ClusterFactory
Constructor Details
#new(width, height) ⇒ Object #new(size) ⇒ Object #new(origin, size) ⇒ Object
Returns a new instance of SizedRectangle.
316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
# File 'lib/geometry/rectangle.rb', line 316 def initialize(*args) , args = args.partition {|a| a.is_a? Hash} = .reduce({}, :merge) @origin = [:origin] ? Point[[:origin]] : 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 SizeRectangle#new" end end |
Instance Attribute Details
#origin ⇒ Point
Returns The Rectangle‘s origin.
298 299 300 |
# File 'lib/geometry/rectangle.rb', line 298 def origin @origin end |
#size ⇒ Size
Returns The Geometry::Size of the Rectangle.
300 301 302 |
# File 'lib/geometry/rectangle.rb', line 300 def size @size end |
Instance Method Details
#center ⇒ Point
Returns The Rectangle‘s center.
338 339 340 |
# File 'lib/geometry/rectangle.rb', line 338 def center @origin + @size/2 end |
#edges ⇒ Array<Edge>
Returns The Rectangle‘s four edges.
343 344 345 346 347 348 349 350 351 352 |
# File 'lib/geometry/rectangle.rb', line 343 def edges point0 = @origin point2 = @origin + @size 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: ==
331 332 333 |
# File 'lib/geometry/rectangle.rb', line 331 def eql?(other) (self.origin == other.origin) && (self.size == other.size) end |
#height ⇒ Object
373 374 375 |
# File 'lib/geometry/rectangle.rb', line 373 def height @size.height end |
#max ⇒ Point
Returns The upper right corner of the bounding Rectangle.
355 356 357 |
# File 'lib/geometry/rectangle.rb', line 355 def max @origin + @size end |
#min ⇒ Point
Returns The lower left corner of the bounding Rectangle.
360 361 362 |
# File 'lib/geometry/rectangle.rb', line 360 def min @origin end |
#points ⇒ Array<Point>
Returns The Rectangle‘s four points (clockwise).
365 366 367 368 369 370 371 |
# File 'lib/geometry/rectangle.rb', line 365 def points point0 = @origin point2 = @origin + @size point1 = Point[point0.x,point2.y] point3 = Point[point2.x, point0.y] [point0, point1, point2, point3] end |
#width ⇒ Object
377 378 379 |
# File 'lib/geometry/rectangle.rb', line 377 def width @size.width end |