Class: Laser::Cutter::Geometry::Line
- Defined in:
- lib/laser/cutter/geometry/line.rb
Overview
A straight line between two points. Two lines are equal whichever way they run.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#p1 ⇒ Point
Where the line starts.
-
#p2 ⇒ Point
Where the line ends.
Attributes inherited from Shape
Class Method Summary collapse
Instance Method Summary collapse
-
#<(other) ⇒ Boolean
Whether this line comes first, by p1 and then by p2.
-
#<=>(other) ⇒ Integer?
Orders lines by their normalized points.
-
#>(other) ⇒ Boolean
Whether this line comes last, by p1 and then by p2.
-
#center ⇒ Point
The middle of the line.
-
#clone ⇒ Line
A copy with copies of both points.
-
#eql?(other) ⇒ Boolean
Whether the lines join the same two points, either way round.
-
#hash ⇒ Integer
The same for a line and its reverse.
-
#initialize(point1, point2 = nil) ⇒ Line
constructor
A new instance of Line.
- #length ⇒ Float
-
#normalized ⇒ Line
The same line, running from the lesser point to the greater.
-
#overlaps?(another) ⇒ Boolean
Whether both lines lie on one horizontal or vertical line and share at least a point.
-
#relocate! ⇒ Line
Moves the line so that it starts at its position, keeping its length and direction.
-
#sorted_coords(another) ⇒ Array(Array, Array)
The x and then the y ranges of both lines, each range sorted.
-
#to_s ⇒ String
The class and both points, such as "line 0,0=>1,0".
-
#xor(another) ⇒ Array(Line, Line)?
The parts of two overlapping lines that only one of them covers.
Methods inherited from Shape
Constructor Details
#initialize(point1, point2 = nil) ⇒ Line
Returns a new instance of Line.
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/laser/cutter/geometry/line.rb', line 25 def initialize(point1, point2 = nil) if point1.is_a?(Hash) = point1 self.p1 = Point.new([:from]) self.p2 = Point.new([: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 ⇒ Point
Returns where the line starts.
19 20 21 |
# File 'lib/laser/cutter/geometry/line.rb', line 19 def p1 @p1 end |
#p2 ⇒ Point
Returns where the line ends.
19 |
# File 'lib/laser/cutter/geometry/line.rb', line 19 attr_accessor :p1, :p2 |
Class Method Details
.[](*array) ⇒ Line
11 12 13 |
# File 'lib/laser/cutter/geometry/line.rb', line 11 def self.[](*array) new(*array) end |
Instance Method Details
#<(other) ⇒ Boolean
Returns whether this line comes first, by p1 and then by p2.
126 127 128 |
# File 'lib/laser/cutter/geometry/line.rb', line 126 def <(other) p1 == other.p1 ? p2 < other.p2 : p1 < other.p1 end |
#<=>(other) ⇒ Integer?
Orders lines by their normalized points.
118 119 120 121 122 |
# File 'lib/laser/cutter/geometry/line.rb', line 118 def <=>(other) n1 = normalized n2 = other.normalized n1.p1.eql?(n2.p1) ? n1.p2 <=> n2.p2 : n1.p1 <=> n2.p1 end |
#>(other) ⇒ Boolean
Returns whether this line comes last, by p1 and then by p2.
132 133 134 |
# File 'lib/laser/cutter/geometry/line.rb', line 132 def >(other) p1 == other.p1 ? p2 > other.p2 : p1 > other.p1 end |
#center ⇒ Point
Returns the middle of the line.
88 89 90 |
# File 'lib/laser/cutter/geometry/line.rb', line 88 def center Point.new((p2.x + p1.x) / 2, (p2.y + p1.y) / 2) end |
#clone ⇒ Line
Returns a copy with copies of both points.
142 143 144 |
# File 'lib/laser/cutter/geometry/line.rb', line 142 def clone self.class.new(p1, p2) end |
#eql?(other) ⇒ Boolean
Returns whether the lines join the same two points, either way round.
104 105 106 107 |
# File 'lib/laser/cutter/geometry/line.rb', line 104 def eql?(other) (other.p1.eql?(p1) && other.p2.eql?(p2)) || (other.p2.eql?(p1) && other.p1.eql?(p2)) end |
#hash ⇒ Integer
Returns the same for a line and its reverse.
137 138 139 |
# File 'lib/laser/cutter/geometry/line.rb', line 137 def hash [p1.to_a, p2.to_a].sort.hash end |
#length ⇒ Float
93 94 95 |
# File 'lib/laser/cutter/geometry/line.rb', line 93 def length Math.sqrt(((p2.x - p1.x)**2) + ((p2.y - p1.y)**2)) end |
#normalized ⇒ Line
Returns the same line, running from the lesser point to the greater.
110 111 112 |
# File 'lib/laser/cutter/geometry/line.rb', line 110 def normalized p1 < p2 ? Line.new(p1, p2) : Line.new(p2, p1) end |
#overlaps?(another) ⇒ Boolean
Returns whether both lines lie on one horizontal or vertical line and share at least a point.
41 42 43 44 45 46 47 48 |
# File 'lib/laser/cutter/geometry/line.rb', line 41 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! ⇒ Line
Moves the line so that it starts at its position, keeping its length and direction.
78 79 80 81 82 83 84 85 |
# File 'lib/laser/cutter/geometry/line.rb', line 78 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) ⇒ Array(Array, Array)
Returns the x and then the y ranges of both lines, each range sorted.
53 54 55 56 57 |
# File 'lib/laser/cutter/geometry/line.rb', line 53 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] [xs, ys] end |
#to_s ⇒ String
Returns the class and both points, such as "line 0,0=>1,0".
98 99 100 |
# File 'lib/laser/cutter/geometry/line.rb', line 98 def to_s "#{self.class.name.gsub(/.*::/, '').downcase} #{p1}=>#{p2}" end |
#xor(another) ⇒ Array(Line, Line)?
The parts of two overlapping lines that only one of them covers.
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/laser/cutter/geometry/line.rb', line 63 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 |