Class: Euclidean::SizedRectangle

Inherits:
Rectangle show all
Defined in:
lib/euclidean/rectangle.rb

Instance Attribute Summary collapse

Accessors collapse

Instance Method Summary collapse

Methods inherited from Rectangle

#bounds, #inset, #minmax, new

Methods included from ClusterFactory

included

Constructor Details

#new(width, height) ⇒ Object #new(size) ⇒ Object #new(origin, size) ⇒ Object

Returns a new instance of SizedRectangle.

Overloads:

  • #new(width, height) ⇒ Object

    Creates a Rectangle of the given width and height with its origin at [0,0]

    Parameters:

    • height (Number)

      Height

    • width (Number)

      Width

  • #new(size) ⇒ Object

    Creates a Rectangle of the given Euclidean::Size with its origin at [0,0]

    Parameters:

    • size (Size)

      Width and height

  • #new(origin, size) ⇒ Object

    Creates a Rectangle with the given origin point and size

    Parameters:



310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/euclidean/rectangle.rb', line 310

def initialize(*args)
  options, args = args.partition {|a| a.is_a? Hash}
  options = options.reduce({}, :merge)

  @origin = options[:origin] ? Point[options[:origin]] : PointZero.new

  if options.has_key?(:size)
    @size = Euclidean::Size[options[:size]]
  elsif options.has_key?(:height) and options.has_key?(:width)
    @size = Euclidean::Size[options[:width], options[:height]]
  else
    raise ArgumentError, "Bad arguments to SizeRectangle#new"
  end
end

Instance Attribute Details

#originPoint

Returns The Rectangle‘s origin.

Returns:



292
293
294
# File 'lib/euclidean/rectangle.rb', line 292

def origin
  @origin
end

#sizeSize

Returns The Euclidean::Size of the Rectangle.

Returns:



294
295
296
# File 'lib/euclidean/rectangle.rb', line 294

def size
  @size
end

Instance Method Details

#centerPoint

Returns The Rectangle‘s center.

Returns:



332
333
334
# File 'lib/euclidean/rectangle.rb', line 332

def center
  @origin + @size/2
end

#edgesArray<Edge>

Returns The Rectangle‘s four edges.

Returns:



337
338
339
340
341
342
343
344
345
346
# File 'lib/euclidean/rectangle.rb', line 337

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: ==

Returns:

  • (Boolean)


325
326
327
# File 'lib/euclidean/rectangle.rb', line 325

def eql?(other)
  (self.origin == other.origin) && (self.size == other.size)
end

#heightObject



348
349
350
# File 'lib/euclidean/rectangle.rb', line 348

def height
  @size.height
end

#maxPoint

Returns The upper right corner of the bounding Rectangle.

Returns:



353
354
355
# File 'lib/euclidean/rectangle.rb', line 353

def max
  @origin + @size
end

#minPoint

Returns The lower left corner of the bounding Rectangle.

Returns:



358
359
360
# File 'lib/euclidean/rectangle.rb', line 358

def min
  @origin
end

#pointsArray<Point>

Returns The Rectangle‘s four points (clockwise).

Returns:



363
364
365
366
367
368
369
# File 'lib/euclidean/rectangle.rb', line 363

def points
  point0 = @origin
  point2 = @origin + @size
  point1 = Point[point0.x,point2.y]
  point3 = Point[point2.x, point0.y]
  [point0, point1, point2, point3]
end

#widthObject



371
372
373
# File 'lib/euclidean/rectangle.rb', line 371

def width
  @size.width
end