class Laser::Cutter::Geometry::Line

Attributes

p1[RW]
p2[RW]

Public Class Methods

[](*array) click to toggle source
# File lib/laser-cutter/geometry/shape/line.rb, line 6
def self.[] *array
  self.new *array
end
new(point1, point2 = nil) click to toggle source
# 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

Public Instance Methods

<(other) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# File lib/laser-cutter/geometry/shape/line.rb, line 97
def clone
  self.class.new(p1, p2)
end
eql?(other) click to toggle source
# 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() click to toggle source
# File lib/laser-cutter/geometry/shape/line.rb, line 93
def hash
  [p1.to_a, p2.to_a].sort.hash
end
length() click to toggle source
# 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() click to toggle source
# 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) click to toggle source
# 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!() click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# File lib/laser-cutter/geometry/shape/line.rb, line 66
def to_s
  "#{self.class.name.gsub(/.*::/,'').downcase} #{p1}=>#{p2}"
end
xor(another) click to toggle source
# 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