Class: Laser::Cutter::Geometry::Line
- Inherits:
-
Shape
- Object
- Shape
- Laser::Cutter::Geometry::Line
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
Class Method Summary
collapse
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.
12
13
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/laser-cutter/geometry/shape/line.rb', line 12
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
#p1 ⇒ Object
Returns the value of attribute p1.
10
11
12
|
# File 'lib/laser-cutter/geometry/shape/line.rb', line 10
def p1
@p1
end
|
#p2 ⇒ Object
Returns the value of attribute p2.
10
11
12
|
# File 'lib/laser-cutter/geometry/shape/line.rb', line 10
def p2
@p2
end
|
Class Method Details
.[](*array) ⇒ Object
6
7
8
|
# File 'lib/laser-cutter/geometry/shape/line.rb', line 6
def self.[] *array
self.new *array
end
|
Instance Method Details
#<(other) ⇒ Object
85
86
87
|
# File 'lib/laser-cutter/geometry/shape/line.rb', line 85
def < (other)
self.p1 == other.p1 ? self.p2 < other.p2 : self.p1 < other.p1
end
|
#<=>(other) ⇒ Object
79
80
81
82
83
|
# File 'lib/laser-cutter/geometry/shape/line.rb', line 79
def <=>(other)
n1 = self.normalized
n2 = other.normalized
n1.p1.eql?(n2.p1) ? n1.p2 <=> n2.p2 : n1.p1 <=> n2.p1
end
|
#>(other) ⇒ Object
89
90
91
|
# File 'lib/laser-cutter/geometry/shape/line.rb', line 89
def > (other)
self.p1 == other.p1 ? self.p2 > other.p2 : self.p1 > other.p1
end
|
#center ⇒ Object
58
59
60
|
# File 'lib/laser-cutter/geometry/shape/line.rb', line 58
def center
Point.new((p2.x + p1.x) / 2, (p2.y + p1.y) / 2)
end
|
#clone ⇒ Object
97
98
99
|
# File 'lib/laser-cutter/geometry/shape/line.rb', line 97
def clone
self.class.new(p1, p2)
end
|
#eql?(other) ⇒ Boolean
70
71
72
73
|
# File 'lib/laser-cutter/geometry/shape/line.rb', line 70
def eql?(other)
(other.p1.eql?(p1) && other.p2.eql?(p2)) ||
(other.p2.eql?(p1) && other.p1.eql?(p2))
end
|
#hash ⇒ Object
93
94
95
|
# File 'lib/laser-cutter/geometry/shape/line.rb', line 93
def hash
[p1.to_a, p2.to_a].sort.hash
end
|
#length ⇒ Object
62
63
64
|
# File 'lib/laser-cutter/geometry/shape/line.rb', line 62
def length
Math.sqrt((p2.x - p1.x)**2 + (p2.y - p1.y)**2)
end
|
#normalized ⇒ Object
75
76
77
|
# File 'lib/laser-cutter/geometry/shape/line.rb', line 75
def normalized
p1 < p2 ? Line.new(p1, p2) : Line.new(p2, p1)
end
|
#overlaps?(another) ⇒ Boolean
25
26
27
28
29
30
31
|
# File 'lib/laser-cutter/geometry/shape/line.rb', line 25
def overlaps?(another)
xs, ys = sorted_coords(another)
return false unless xs.all?{|x| x == xs[0] } || ys.all?{|y| y == ys[0] }
return false if xs.first[1] < xs.last[0] || xs.first[0] > xs.last[1]
return false if ys.first[1] < ys.last[0] || ys.first[0] > ys.last[1]
true
end
|
#relocate! ⇒ Object
49
50
51
52
53
54
55
56
|
# File 'lib/laser-cutter/geometry/shape/line.rb', line 49
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
|
#sorted_coords(another) ⇒ Object
33
34
35
36
37
|
# File 'lib/laser-cutter/geometry/shape/line.rb', line 33
def sorted_coords(another)
xs = [[p1.x, p2.x].sort, [another.p1.x, another.p2.x].sort]
ys = [[p1.y, p2.y].sort, [another.p1.y, another.p2.y].sort]
return xs, ys
end
|
#to_s ⇒ Object
66
67
68
|
# File 'lib/laser-cutter/geometry/shape/line.rb', line 66
def to_s
"#{self.class.name.gsub(/.*::/,'').downcase} #{p1}=>#{p2}"
end
|
#xor(another) ⇒ Object
39
40
41
42
43
44
45
46
47
|
# File 'lib/laser-cutter/geometry/shape/line.rb', line 39
def xor(another)
return nil unless overlaps?(another)
xs, ys = sorted_coords(another)
xs.flatten!.sort!
ys.flatten!.sort!
[ Line.new(Point[xs[0], ys[0]], Point[xs[1], ys[1]]),
Line.new(Point[xs[2], ys[2]], Point[xs[3], ys[3]])]
end
|