Class: Dieses::Geometry::Line

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

Instance Attribute Summary collapse

Attributes inherited from Element

#attributes, #hash

Instance Method Summary collapse

Methods inherited from Element

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

Constructor Details

#initialize(starting, ending) ⇒ Line

Returns a new instance of Line.



14
15
16
17
18
19
# File 'lib/dieses/geometry/line.rb', line 14

def initialize(starting, ending)
  @starting, @ending = Point.cast(starting), Point.cast(ending)
  @equation = Equation.from_line(self)

  super()
end

Instance Attribute Details

#endingObject (readonly)

Returns the value of attribute ending.



12
13
14
# File 'lib/dieses/geometry/line.rb', line 12

def ending
  @ending
end

#equationObject (readonly)

Returns the value of attribute equation.



12
13
14
# File 'lib/dieses/geometry/line.rb', line 12

def equation
  @equation
end

#startingObject (readonly)

Returns the value of attribute starting.



12
13
14
# File 'lib/dieses/geometry/line.rb', line 12

def starting
  @starting
end

Instance Method Details

#bboxObject



62
63
64
# File 'lib/dieses/geometry/line.rb', line 62

def bbox
  BoundingBox.new([starting, ending].min, [starting, ending].max)
end

#duplicate(x: nil, y: nil, count: 1) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/dieses/geometry/line.rb', line 26

def duplicate(x: nil, y: nil, count: 1)
  lines, line = [], self

  count.times do
    lines << line
    line = line.translate(x: x, y: y)
  end

  [lines, line]
end

#duplicates(x: nil, y: nil, count: 1) ⇒ Object



37
38
39
40
# File 'lib/dieses/geometry/line.rb', line 37

def duplicates(x: nil, y: nil, count: 1)
  lines, = duplicate(x: x, y: y, count: count)
  lines
end

#onto?(point) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/dieses/geometry/line.rb', line 42

def onto?(point)
  equation.onto?(point) && point >= starting && point <= ending
end

#to_hObject



58
59
60
# File 'lib/dieses/geometry/line.rb', line 58

def to_h
  { starting: starting, ending: ending }
end

#to_sObject



54
55
56
# File 'lib/dieses/geometry/line.rb', line 54

def to_s
  "L(#{starting}, #{ending})"
end

#to_svgfObject



46
47
48
49
50
51
52
# File 'lib/dieses/geometry/line.rb', line 46

def to_svgf
  x1, y1, x2, y2 = [*starting.to_a, *ending.to_a].map { |value| Support.approx(value) }

  <<~SVG
    <line x1="#{x1}" y1="#{y1}" x2="#{x2}" y2="#{y2}" %{attributes}/>
  SVG
end

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



21
22
23
24
# File 'lib/dieses/geometry/line.rb', line 21

def translate(x: nil, y: nil)
  starting, ending = [self.starting, self.ending].map { |point| point.translate(x: x, y: y) }
  self.class.new starting, ending
end