Class: Dieses::Geometry::Rect

Inherits:
Element
  • Object
show all
Extended by:
Forwardable
Includes:
Predicate
Defined in:
lib/dieses/geometry/rect.rb

Overview

Defined Under Namespace

Modules: Predicate

Constant Summary collapse

Orientation =
Support::Enum.of(:portrait, :landscape)

Instance Attribute Summary collapse

Attributes inherited from Element

#attributes, #hash

Instance Method Summary collapse

Methods included from Predicate

#cover?, #inside?, #onto?, #outside?

Methods inherited from Element

#attr, #classify, #eql?, #to_svg

Constructor Details

#initialize(width, height, position: Point.origin) ⇒ Rect

Returns a new instance of Rect.



22
23
24
25
26
27
28
29
30
# File 'lib/dieses/geometry/rect.rb', line 22

def initialize(width, height, position: Point.origin)
  @width    = width.to_f
  @height   = height.to_f
  @position = Point.cast(position)
  @corner   = calculate_corner
  @side     = calculate_side

  super()
end

Instance Attribute Details

#heightObject (readonly)

Returns the value of attribute height.



20
21
22
# File 'lib/dieses/geometry/rect.rb', line 20

def height
  @height
end

#positionObject (readonly)

Returns the value of attribute position.



20
21
22
# File 'lib/dieses/geometry/rect.rb', line 20

def position
  @position
end

#widthObject (readonly)

Returns the value of attribute width.



20
21
22
# File 'lib/dieses/geometry/rect.rb', line 20

def width
  @width
end

Instance Method Details

#align(other, alignment = :center) ⇒ Object

Raises:

  • (ArgumentError)


67
68
69
70
71
# File 'lib/dieses/geometry/rect.rb', line 67

def align(other, alignment = :center)
  raise ArgumentError, "No such alignment type: #{alignment}" unless Align.respond_to? alignment

  self.class.new other.width, other.height, position: Align.public_send(alignment, self, other).approx
end

#angleObject



130
131
132
# File 'lib/dieses/geometry/rect.rb', line 130

def angle
  Math.atan(height / width)
end

#bboxObject



134
135
136
# File 'lib/dieses/geometry/rect.rb', line 134

def bbox
  BoundingBox.new(@corner.values.min, @corner.values.max)
end

#intersect(equation, precision: nil) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/dieses/geometry/rect.rb', line 115

def intersect(equation, precision: nil)
  points = Side.members
    .map { |line| public_send(line).intersect(equation).approx(precision) }
    .uniq
    .select { |intersect| onto?(intersect) }
    .sort

  return if points.empty?

  raise "Unexpected number of intersection points: #{points.size}" unless points.size <= 2

  Line.new((starting = points.shift), (points.shift || starting))
end

#orient(new_orientation) ⇒ Object



84
85
86
87
88
89
# File 'lib/dieses/geometry/rect.rb', line 84

def orient(new_orientation)
  new_orientation = Orientation.(new_orientation)
  return self if orientation == new_orientation

  self.class.new height, width, position: position
end

#orientationObject



75
76
77
78
79
80
81
82
# File 'lib/dieses/geometry/rect.rb', line 75

def orientation
  value = if Support.almost_greater_than(width, height)
            :landscape
          else
            :portrait
          end
  Orientation.(value)
end

#shrink(width: 0, height: 0) ⇒ Object



36
37
38
# File 'lib/dieses/geometry/rect.rb', line 36

def shrink(width: 0, height: 0)
  self.class.new self.width - width, self.height - height, position: position
end

#to_hObject



145
146
147
# File 'lib/dieses/geometry/rect.rb', line 145

def to_h
  { width: width, height: height, **position.to_h }
end

#to_sObject



138
139
140
141
142
143
# File 'lib/dieses/geometry/rect.rb', line 138

def to_s
  repr = "R(#{width}, #{height})"
  return repr if position == Point.origin

  "#{repr}@#{position}"
end

#to_svgfObject



150
151
152
153
154
# File 'lib/dieses/geometry/rect.rb', line 150

def to_svgf
  <<~SVG
    <rect width="#{Support.approx(width)}" height="#{Support.approx(height)}" x="#{Support.approx(position.x)}" y="#{Support.approx(position.y)}" %{attributes}/>
  SVG
end

#translate(x: 0, y: 0) ⇒ Object



32
33
34
# File 'lib/dieses/geometry/rect.rb', line 32

def translate(x: 0, y: 0)
  self.class.new width, height, position: position.translate(x: x, y: y)
end