Class: Laser::Cutter::Geometry::Line

Inherits:
Shape
  • Object
show all
Defined in:
lib/laser-cutter/geometry/shape/line.rb

Direct Known Subclasses

Rect

Instance Attribute Summary collapse

Attributes inherited from Shape

#name, #position

Instance Method Summary collapse

Methods inherited from Shape

#move_to, #x, #x=, #y, #y=

Constructor Details

#initialize(point1, point2 = nil) ⇒ Line

Returns a new instance of Line.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/laser-cutter/geometry/shape/line.rb', line 7

def initialize(point1, point2 = nil)
  if point1.is_a?(Hash)
    options = point1
    self.p1 = Point.new(options[:from])
    self.p2 = Point.new(options[:to])
  else
    self.p1 = point1.clone
    self.p2 = point2.clone
  end
  self.position = p1.clone
  raise 'Both points are required for line definition' unless (p1 && p2)
end

Instance Attribute Details

#p1Object

Returns the value of attribute p1.



5
6
7
# File 'lib/laser-cutter/geometry/shape/line.rb', line 5

def p1
  @p1
end

#p2Object

Returns the value of attribute p2.



5
6
7
# File 'lib/laser-cutter/geometry/shape/line.rb', line 5

def p2
  @p2
end

Instance Method Details

#<=>(other) ⇒ Object



50
51
52
# File 'lib/laser-cutter/geometry/shape/line.rb', line 50

def <=>(other)
  self.normalized.to_s <=> other.normalized.to_s
end

#centerObject



29
30
31
# File 'lib/laser-cutter/geometry/shape/line.rb', line 29

def center
  Point.new((p2.x + p1.x) / 2, (p2.y + p1.y) / 2)
end

#cloneObject



58
59
60
# File 'lib/laser-cutter/geometry/shape/line.rb', line 58

def clone
  self.class.new(p1, p2)
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
# File 'lib/laser-cutter/geometry/shape/line.rb', line 41

def eql?(other)
  (other.p1.eql?(p1) && other.p2.eql?(p2)) ||
  (other.p2.eql?(p1) && other.p1.eql?(p2))
end

#hashObject



54
55
56
# File 'lib/laser-cutter/geometry/shape/line.rb', line 54

def hash
  [p1.to_a, p2.to_a].sort.hash
end

#lengthObject



33
34
35
# File 'lib/laser-cutter/geometry/shape/line.rb', line 33

def length
  Math.sqrt((p2.x - p1.x)**2 + (p2.y - p1.y)**2)
end

#normalizedObject



46
47
48
# File 'lib/laser-cutter/geometry/shape/line.rb', line 46

def normalized
  p1 < p2 ? Line.new(p1, p2) : Line.new(p2, p1)
end

#relocate!Object



20
21
22
23
24
25
26
27
# File 'lib/laser-cutter/geometry/shape/line.rb', line 20

def relocate!
  dx = p2.x - p1.x
  dy = p2.y - p1.y

  self.p1 = position.clone
  self.p2 = Point[p1.x + dx, p1.y + dy]
  self
end

#to_sObject



37
38
39
# File 'lib/laser-cutter/geometry/shape/line.rb', line 37

def to_s
  "#{self.class.name.gsub(/.*::/,'').downcase} #{p1}=>#{p2}"
end