Class: PerfectShape::CompositeShape

Inherits:
Shape
  • Object
show all
Defined in:
lib/perfect_shape/composite_shape.rb

Overview

A composite of multiple shapes

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Shape

#==, #bounding_box, #center_point, #center_x, #center_y, #height, #width

Constructor Details

#initialize(shapes: []) ⇒ CompositeShape

Constructs from multiple shapes



37
38
39
# File 'lib/perfect_shape/composite_shape.rb', line 37

def initialize(shapes: [])
  self.shapes = shapes
end

Instance Attribute Details

#shapesObject

Returns the value of attribute shapes.



34
35
36
# File 'lib/perfect_shape/composite_shape.rb', line 34

def shapes
  @shapes
end

Instance Method Details

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

Checks if composite shape contains point (two-number Array or x, y args) by comparing against all shapes it consists of

the composite shape or false if the point lies outside of the path’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:

  • (Boolean)

    true if the point lies within the bound of



66
67
68
69
70
71
# File 'lib/perfect_shape/composite_shape.rb', line 66

def contain?(x_or_point, y = nil, outline: false, distance_tolerance: 0)
  x, y = Point.normalize_point(x_or_point, y)
  return unless x && y
  
  shapes.any? { |shape| shape.contain?(x, y, outline: outline, distance_tolerance: distance_tolerance) }
end

#intersect?(rectangle) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/perfect_shape/composite_shape.rb', line 73

def intersect?(rectangle)
  shapes.any? { |shape| shape.intersect?(rectangle) }
end

#max_xObject



49
50
51
# File 'lib/perfect_shape/composite_shape.rb', line 49

def max_x
  shapes.map(&:max_x).max
end

#max_yObject



53
54
55
# File 'lib/perfect_shape/composite_shape.rb', line 53

def max_y
  shapes.map(&:max_y).max
end

#min_xObject



41
42
43
# File 'lib/perfect_shape/composite_shape.rb', line 41

def min_x
  shapes.map(&:min_x).min
end

#min_yObject



45
46
47
# File 'lib/perfect_shape/composite_shape.rb', line 45

def min_y
  shapes.map(&:min_y).min
end