Class: PerfectShape::Rectangle

Inherits:
Shape
  • Object
show all
Includes:
RectangularShape
Defined in:
lib/perfect_shape/rectangle.rb

Overview

Direct Known Subclasses

Square

Instance Attribute Summary

Attributes included from RectangularShape

#height, #width

Attributes included from PointLocation

#x, #y

Instance Method Summary collapse

Methods included from RectangularShape

#initialize, #max_x, #max_y

Methods included from PointLocation

#initialize, #min_x, #min_y

Methods inherited from Shape

#==, #bounding_box, #center_point, #center_x, #center_y, #height, #max_x, #max_y, #min_x, #min_y, #normalize_point, #width

Instance Method Details

#contain?(x_or_point, y = nil, outline: false, distance_tolerance: 0) ⇒ @code true

Checks if rectangle contains point (two-number Array or x, y args)

the rectangle, false if the point lies outside of the rectangle’s bounds.

Parameters:

  • x

    The X coordinate of the point to test.

  • y (defaults to: nil)

    The Y coordinate of the point to test.

Returns:

  • (@code true)

    if the point lies within the bound of



40
41
42
43
44
45
46
47
48
# File 'lib/perfect_shape/rectangle.rb', line 40

def contain?(x_or_point, y = nil, outline: false, distance_tolerance: 0)
  x, y = normalize_point(x_or_point, y)
  return unless x && y
  if outline
    edges.any? { |edge| edge.contain?(x, y, distance_tolerance: distance_tolerance) }
  else
    x.between?(self.x, self.x + width) && y.between?(self.y, self.y + height)
  end
end

#edgesObject



50
51
52
53
54
55
56
57
# File 'lib/perfect_shape/rectangle.rb', line 50

def edges
  [
    Line.new(points: [[self.x, self.y], [self.x + width, self.y]]),
    Line.new(points: [[self.x + width, self.y], [self.x + width, self.y + height]]),
    Line.new(points: [[self.x + width, self.y + height], [self.x, self.y + height]]),
    Line.new(points: [[self.x, self.y + height], [self.x, self.y]])
  ]
end