class Draught::Line

Constants

DEGREES_180
DEGREES_270
DEGREES_360
DEGREES_90

Attributes

end_point[R]
length[R]
radians[R]
start_point[R]

Public Class Methods

build(args = {}) click to toggle source
# File lib/draught/line.rb, line 26
def build(args = {})
  builder_class = args.has_key?(:end_point) ? LineBuilderFromPoint : LineBuilderFromAngles
  line_args = builder_class.new(args).line_args
  new(line_args)
end
from_path(path) click to toggle source
# File lib/draught/line.rb, line 32
def from_path(path)
  if path.number_of_points != 2
    raise ArgumentError, "path must contain exactly 2 points, this contained #{path.number_of_points}"
  end
  build(start_point: path.first, end_point: path.last)
end
horizontal(width) click to toggle source
# File lib/draught/line.rb, line 18
def horizontal(width)
  build(end_point: Point.new(width, 0))
end
new(args) click to toggle source
# File lib/draught/line.rb, line 42
def initialize(args)
  @start_point = args.fetch(:start_point, Point::ZERO)
  @end_point = args.fetch(:end_point)
  @length = args.fetch(:length)
  @radians = args.fetch(:radians)
end
vertical(height) click to toggle source
# File lib/draught/line.rb, line 22
def vertical(height)
  build(end_point: Point.new(0, height))
end

Public Instance Methods

[](index_start_or_range, length = nil) click to toggle source
# File lib/draught/line.rb, line 63
def [](index_start_or_range, length = nil)
  if length.nil?
    case index_start_or_range
    when Range
      Path.new(points[index_start_or_range])
    when Numeric
      points[index_start_or_range]
    else
      raise TypeError, "requires a Range or Numeric in single-arg form"
    end
  else
    Path.new(points[index_start_or_range, length])
  end
end
extend(args = {}) click to toggle source
# File lib/draught/line.rb, line 53
def extend(args = {})
  default_args = {at: :end}
  args = default_args.merge(args)
  new_length = args[:to] || length + args[:by]
  new_line = self.class.build({
    start_point: start_point, length: new_length, radians: radians
  })
  args[:at] == :start ? shift_line(new_line) : new_line
end
height() click to toggle source
# File lib/draught/line.rb, line 98
def height
  @height ||= y_max - y_min
end
lower_left() click to toggle source
# File lib/draught/line.rb, line 90
def lower_left
  @lower_left ||= Point.new(x_min, y_min)
end
points() click to toggle source
# File lib/draught/line.rb, line 49
def points
  @points ||= [start_point, end_point]
end
transform(transformation) click to toggle source
# File lib/draught/line.rb, line 84
def transform(transformation)
  self.class.build(Hash[
    transform_args_hash.map { |arg, point| [arg, point.transform(transformation)] }
  ])
end
translate(vector) click to toggle source
# File lib/draught/line.rb, line 78
def translate(vector)
  self.class.build(Hash[
    transform_args_hash.map { |arg, point| [arg, point.translate(vector)] }
  ])
end
width() click to toggle source
# File lib/draught/line.rb, line 94
def width
  @width ||= x_max - x_min
end

Private Instance Methods

shift_line(new_line) click to toggle source
# File lib/draught/line.rb, line 104
def shift_line(new_line)
  translation = Vector.translation_between(new_line.end_point, end_point)
  self.class.new({
    start_point: start_point.translate(translation),
    end_point: new_line.end_point.translate(translation),
    length: new_line.length,
    radians: radians
  })
end
transform_args_hash() click to toggle source
# File lib/draught/line.rb, line 114
def transform_args_hash
  {start_point: start_point, end_point: end_point}
end
x_max() click to toggle source
# File lib/draught/line.rb, line 118
def x_max
  @x_max ||= points.map(&:x).max || 0
end
x_min() click to toggle source
# File lib/draught/line.rb, line 122
def x_min
  @x_min ||= points.map(&:x).min || 0
end
y_max() click to toggle source
# File lib/draught/line.rb, line 126
def y_max
  @y_max ||= points.map(&:y).max || 0
end
y_min() click to toggle source
# File lib/draught/line.rb, line 130
def y_min
  @y_min ||= points.map(&:y).min || 0
end