Class: Sevgi::Geometry::Rect

Inherits:
Element
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/sevgi/geometry/elements/rect.rb

Overview

rubocop:disable Metrics/ClassLength

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Element

#at, #bh, #bw, #ignorable?, #position, #position!

Constructor Details

#initialize(position: nil, width:, height:) ⇒ Rect



18
19
20
21
22
23
24
25
26
# File 'lib/sevgi/geometry/elements/rect.rb', line 18

def initialize(position: nil, width:, height:)
  super()

  @position = position || Point.origin
  @width    = width.to_f
  @height   = height.to_f

  compute
end

Instance Attribute Details

#cornerObject (readonly)

Returns the value of attribute corner.



16
17
18
# File 'lib/sevgi/geometry/elements/rect.rb', line 16

def corner
  @corner
end

#cornersObject (readonly)

Returns the value of attribute corners.



16
17
18
# File 'lib/sevgi/geometry/elements/rect.rb', line 16

def corners
  @corners
end

#heightObject (readonly)

Returns the value of attribute height.



15
16
17
# File 'lib/sevgi/geometry/elements/rect.rb', line 15

def height
  @height
end

#sideObject (readonly)

Returns the value of attribute side.



16
17
18
# File 'lib/sevgi/geometry/elements/rect.rb', line 16

def side
  @side
end

#sidesObject (readonly)

Returns the value of attribute sides.



16
17
18
# File 'lib/sevgi/geometry/elements/rect.rb', line 16

def sides
  @sides
end

#widthObject (readonly)

Returns the value of attribute width.



15
16
17
# File 'lib/sevgi/geometry/elements/rect.rb', line 15

def width
  @width
end

Class Method Details

.[](width, height, position = nil) ⇒ Object



115
# File 'lib/sevgi/geometry/elements/rect.rb', line 115

def [](width, height, position = nil) = new(width:, height:, position:)

.with_points(p, q) ⇒ Object



117
118
119
120
121
122
123
124
125
126
# File 'lib/sevgi/geometry/elements/rect.rb', line 117

def with_points(p, q)
  top  = p.above?(q) ? p : q
  left = p.left?(q)  ? p : q

  new(
    position: Point[left.x, top.y],
    width:    F.width(p, q),
    height:   F.height(p, q)
  )
end

Instance Method Details

#boxObject



28
29
30
# File 'lib/sevgi/geometry/elements/rect.rb', line 28

def box
  self
end

#diagonalObject



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

def diagonal
  Segment.forward(top_left, bottom_right)
end

#eql?(other) ⇒ Boolean Also known as: ==



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

def eql?(other)
  self.class == other.class && [ position, width, height ] == [ other.position, other.width, other.height ]
end

#hashObject



40
41
42
# File 'lib/sevgi/geometry/elements/rect.rb', line 40

def hash
  [ self.class, position, width, height ].hash
end

#height!Object



44
45
46
# File 'lib/sevgi/geometry/elements/rect.rb', line 44

def height!(...)
  F.approx(height, ...)
end

#inside?(point) ⇒ Boolean



48
49
50
# File 'lib/sevgi/geometry/elements/rect.rb', line 48

def inside?(point)
  onto?(point) || (left.right?(point) && right.left?(point) && top.left?(point) && bottom.right?(point))
end

#intersection(line, precision: nil) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/sevgi/geometry/elements/rect.rb', line 53

def intersection(line, precision: nil)
  points = sides.map { _1.intersection(line).approx(precision) }.uniq.select { onto?(_1) }
  return if points.empty?

  Error.("Unexpected number of intersection points: #{points.size}") if points.size > 2

  Segment.forward(*points)
end

#onto?(point) ⇒ Boolean



63
64
65
# File 'lib/sevgi/geometry/elements/rect.rb', line 63

def onto?(point)
  left.onto?(point) || right.onto?(point) || top.onto?(point) || bottom.onto?(point)
end

#orient(new_orientation) ⇒ Object



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

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

  with(width: height, height: width)
end

#orientationObject



73
74
75
# File 'lib/sevgi/geometry/elements/rect.rb', line 73

def orientation
  F.gt?(width, height) ? :landscape : :portrait
end

#outside?(point) ⇒ Boolean



77
78
79
# File 'lib/sevgi/geometry/elements/rect.rb', line 77

def outside?(point)
  !inside?(point)
end

#to_sObject



81
82
83
84
85
86
87
88
# File 'lib/sevgi/geometry/elements/rect.rb', line 81

def to_s
  strings = []

  strings << "R[#{F.approx(width)}x#{F.approx(height)}]"
  strings << position.to_s if Point.eq?(position, Point.origin)

  strings.join("@")
end

#translate(dx: nil, dy: nil) ⇒ Object



90
91
92
# File 'lib/sevgi/geometry/elements/rect.rb', line 90

def translate(dx: nil, dy: nil)
  self.class.new(position: position.translate(dx:, dy:), width: width, height: height)
end

#width!Object



94
95
96
# File 'lib/sevgi/geometry/elements/rect.rb', line 94

def width!(...)
  F.approx(width, ...)
end

#zero?(precision = nil) ⇒ Boolean



98
99
100
# File 'lib/sevgi/geometry/elements/rect.rb', line 98

def zero?(precision = nil)
  Point.eq?(top_left, bottom_right, precision:)
end