Class: Draught::Line

Inherits:
Object
  • Object
show all
Includes:
Boxlike, Pathlike
Defined in:
lib/draught/line.rb

Defined Under Namespace

Classes: LineBuilderFromAngles, LineBuilderFromPoint

Constant Summary collapse

DEGREES_90 =
Math::PI / 2
DEGREES_180 =
Math::PI
DEGREES_270 =
Math::PI * 1.5
DEGREES_360 =
Math::PI * 2

Constants included from Boxlike

Boxlike::POSITION_METHODS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Pathlike

#==, #approximates?, #box_type, #containers, #empty?, #first, #last, #number_of_points, #paths

Methods included from Boxlike

#bottom_edge, #box_type, #centre, #centre_left, #centre_right, #containers, #corners, #disjoint?, #include_point?, #left_edge, #lower_centre, #lower_right, #min_gap, #move_to, #overlaps?, #paths, #right_edge, #top_edge, #upper_centre, #upper_left, #upper_right

Constructor Details

#initialize(args) ⇒ Line

Returns a new instance of Line.



42
43
44
45
46
47
# 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

Instance Attribute Details

#end_pointObject (readonly)

Returns the value of attribute end_point.



40
41
42
# File 'lib/draught/line.rb', line 40

def end_point
  @end_point
end

#lengthObject (readonly)

Returns the value of attribute length.



40
41
42
# File 'lib/draught/line.rb', line 40

def length
  @length
end

#radiansObject (readonly)

Returns the value of attribute radians.



40
41
42
# File 'lib/draught/line.rb', line 40

def radians
  @radians
end

#start_pointObject (readonly)

Returns the value of attribute start_point.



40
41
42
# File 'lib/draught/line.rb', line 40

def start_point
  @start_point
end

Class Method Details

.build(args = {}) ⇒ Object



26
27
28
29
30
# 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) ⇒ Object



32
33
34
35
36
37
# 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) ⇒ Object



18
19
20
# File 'lib/draught/line.rb', line 18

def horizontal(width)
  build(end_point: Point.new(width, 0))
end

.vertical(height) ⇒ Object



22
23
24
# File 'lib/draught/line.rb', line 22

def vertical(height)
  build(end_point: Point.new(0, height))
end

Instance Method Details

#[](index_start_or_range, length = nil) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# 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 = {}) ⇒ Object



53
54
55
56
57
58
59
60
61
# 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

#heightObject



98
99
100
# File 'lib/draught/line.rb', line 98

def height
  @height ||= y_max - y_min
end

#lower_leftObject



90
91
92
# File 'lib/draught/line.rb', line 90

def lower_left
  @lower_left ||= Point.new(x_min, y_min)
end

#pointsObject



49
50
51
# File 'lib/draught/line.rb', line 49

def points
  @points ||= [start_point, end_point]
end

#transform(transformation) ⇒ Object



84
85
86
87
88
# 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) ⇒ Object



78
79
80
81
82
# 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

#widthObject



94
95
96
# File 'lib/draught/line.rb', line 94

def width
  @width ||= x_max - x_min
end