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

Inherits:
Shape
  • Object
show all
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

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.

Parameters:

  • point1 (Point, Hash) —

    where the line starts, or a Hash with :from and :to, each anything Point accepts

  • point2 (Point, nil) (defaults to: nil) —

    where the line ends, unless point1 is a Hash

Raises:

  • (RuntimeError) —

    when a point is missing



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)
    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 ⇒ Point

Returns where the line starts.

Returns:

  • (Point) —

    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.

Returns:

  • (Point) —

    where the line ends



19
# File 'lib/laser/cutter/geometry/line.rb', line 19

attr_accessor :p1, :p2

Class Method Details

.[](*array) ⇒ Line

Parameters:

Returns:



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.

Parameters:

Returns:

  • (Boolean) —

    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.

Parameters:

Returns:

  • (Integer, nil)


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.

Parameters:

Returns:

  • (Boolean) —

    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.

Returns:

  • (Point) —

    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.

Returns:

  • (Line) —

    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.

Parameters:

Returns:

  • (Boolean) —

    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.

Returns:

  • (Integer) —

    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

Returns:

  • (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.

Returns:

  • (Line) —

    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.

Parameters:

Returns:

  • (Boolean) —

    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.

Returns:



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.

Parameters:

Returns:

  • (Array(Array, Array)) —

    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".

Returns:

  • (String) —

    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.

Parameters:

Returns:

  • (Array(Line, Line), nil) —

    nil when the lines do not overlap



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