Class: Triangular::Line

Inherits:
Object
  • Object
show all
Defined in:
lib/triangular/line.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line_start, line_end) ⇒ Line

Returns a new instance of Line.



7
8
9
10
# File 'lib/triangular/line.rb', line 7

def initialize(line_start, line_end)
  @start = line_start
  @end = line_end
end

Instance Attribute Details

#endObject

Returns the value of attribute end.



5
6
7
# File 'lib/triangular/line.rb', line 5

def end
  @end
end

#startObject

Returns the value of attribute start.



5
6
7
# File 'lib/triangular/line.rb', line 5

def start
  @start
end

Instance Method Details

#==(other) ⇒ Object



12
13
14
15
16
# File 'lib/triangular/line.rb', line 12

def ==(other)
  return false unless other.is_a?(Line)

  start == other.start && self.end == other.end
end

#intersection_at_x(x_plane) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/triangular/line.rb', line 22

def intersection_at_x(x_plane)
  return nil unless intersects_x?(x_plane)
  return nil if @start.x == x_plane && @end.x == x_plane

  y_intersect = (@end.y - @start.y) / (@end.x - @start.x) * (x_plane - @start.x) + @start.y
  z_intersect = (@end.z - @start.z) / (@end.x - @start.x) * (x_plane - @start.x) + @start.z

  Point.new(x_plane, y_intersect, z_intersect)
end

#intersection_at_y(y_plane) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/triangular/line.rb', line 36

def intersection_at_y(y_plane)
  return nil unless intersects_y?(y_plane)
  return nil if @start.y == y_plane && @end.y == y_plane

  x_intersect = (@end.x - @start.x) / (@end.y - @start.y) * (y_plane - @start.y) + @start.x
  z_intersect = (@end.z - @start.z) / (@end.y - @start.y) * (y_plane - @start.y) + @start.z

  Point.new(x_intersect, y_plane, z_intersect)
end

#intersection_at_z(z_plane) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/triangular/line.rb', line 50

def intersection_at_z(z_plane)
  return nil unless intersects_z?(z_plane)
  return nil if @start.z == z_plane && @end.z == z_plane

  x_intersect = (@end.x - @start.x) / (@end.z - @start.z) * (z_plane - @start.z) + @start.x
  y_intersect = (@end.y - @start.y) / (@end.z - @start.z) * (z_plane - @start.z) + @start.y

  Point.new(x_intersect, y_intersect, z_plane)
end

#intersects_x?(x_plane) ⇒ Boolean

Returns:

  • (Boolean)


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

def intersects_x?(x_plane)
  (@start.x >= x_plane && @end.x <= x_plane) || (@start.x <= x_plane && @end.x >= x_plane)
end

#intersects_y?(y_plane) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/triangular/line.rb', line 32

def intersects_y?(y_plane)
  (@start.y >= y_plane && @end.y <= y_plane) || (@start.y <= y_plane && @end.y >= y_plane)
end

#intersects_z?(z_plane) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/triangular/line.rb', line 46

def intersects_z?(z_plane)
  (@start.z >= z_plane && @end.z <= z_plane) || (@start.z <= z_plane && @end.z >= z_plane)
end

#to_svg_path(units) ⇒ Object



60
61
62
# File 'lib/triangular/line.rb', line 60

def to_svg_path(units)
  "<path d=\"M #{@start.x} #{@start.y} L #{@end.x} #{@end.y}\" fill=\"none\" stroke=\"black\" stroke-width=\"#{Units.stroke_width(units)}\" />"
end