Class: PerfectShape::Shape

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

Overview

Superclass of all shapes. Not meant to be used directly. Subclasses must implement/override methods as needed.

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Object

Subclasses must implement



98
99
# File 'lib/perfect_shape/shape.rb', line 98

def ==(other)
end

#bounding_boxObject

Rectangle with x = self.min_x, y = self.min_y, width = self.width, height = self.height



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

def bounding_box
  require 'perfect_shape/rectangle'
  Rectangle.new(x: min_x, y: min_y, width: width, height: height)
end

#center_pointObject

Center point is ‘[center_x, center_y]` Returns `nil` if either center_x or center_y are `nil`



56
57
58
# File 'lib/perfect_shape/shape.rb', line 56

def center_point
  [center_x, center_y] unless center_x.nil? || center_y.nil?
end

#center_xObject

center_x is min_x + width/2.0 by default Returns nil if min_x or width are nil



62
63
64
# File 'lib/perfect_shape/shape.rb', line 62

def center_x
  min_x + width / BigDecimal('2.0') if min_x && width
end

#center_yObject

center_y is min_y + height/2.0 by default Returns nil if min_y or height are nil



68
69
70
# File 'lib/perfect_shape/shape.rb', line 68

def center_y
  min_y + height / BigDecimal('2.0') if min_y && height
end

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

Subclasses must implement

Returns:

  • (Boolean)


94
95
# File 'lib/perfect_shape/shape.rb', line 94

def contain?(x_or_point, y = nil, outline: false, distance_tolerance: 0)
end

#heightObject

Default implementation is max_y - min_y Subclasses can override



50
51
52
# File 'lib/perfect_shape/shape.rb', line 50

def height
  max_y - min_y if max_y && min_y
end

#max_xObject

Subclasses must implement



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

def max_x
end

#max_yObject

Subclasses must implement



39
40
# File 'lib/perfect_shape/shape.rb', line 39

def max_y
end

#min_xObject

Subclasses must implement



27
28
# File 'lib/perfect_shape/shape.rb', line 27

def min_x
end

#min_yObject

Subclasses must implement



31
32
# File 'lib/perfect_shape/shape.rb', line 31

def min_y
end

#normalize_point(x_or_point, y = nil) ⇒ Object

Normalizes point args whether two-number Array or x, y args returning normalized point array of two BigDecimal’s

Parameters:

  • x_or_point

    The point or X coordinate of the point to test.

  • y (defaults to: nil)

    The Y coordinate of the point to test.

Returns:

  • Array of x and y BigDecimal’s representing point



85
86
87
88
89
90
91
# File 'lib/perfect_shape/shape.rb', line 85

def normalize_point(x_or_point, y = nil)
  x = x_or_point
  x, y = x if y.nil? && x_or_point.is_a?(Array) && x_or_point.size == 2
  x = x.is_a?(BigDecimal) ? x : BigDecimal(x.to_s)
  y = y.is_a?(BigDecimal) ? y : BigDecimal(y.to_s)
  [x, y]
end

#widthObject

Default implementation is max_x - min_x Subclasses can override



44
45
46
# File 'lib/perfect_shape/shape.rb', line 44

def width
  max_x - min_x if max_x && min_x
end