Class: Triangular::Ray

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y) ⇒ Ray

Returns a new instance of Ray.



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

def initialize(x, y)
  @x = x
  @y = y
end

Instance Attribute Details

#xObject

Returns the value of attribute x.



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

def x
  @x
end

#yObject

Returns the value of attribute y.



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

def y
  @y
end

Instance Method Details

#intersection(facet) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/triangular/ray.rb', line 12

def intersection(facet)
  lines = facet.lines

  x_intersection = false
  y_intersection = false

  lines.each do |line|
    x_intersection = true if line.start.x <= @x && line.end.x >= @x
    x_intersection = true if line.end.x <= @x && line.start.x >= @x

    y_intersection = true if line.start.y <= @y && line.end.y >= @y
    y_intersection = true if line.end.y <= @y && line.start.y >= @y
  end

  return nil unless x_intersection && y_intersection

  x_intersections = lines.map do |line|
    line.intersection_at_x(@x)
  end

  x_intersections.compact!

  if x_intersections[0] == x_intersections[1]
    x_intersections[0]
  else
    Line.new(x_intersections[0], x_intersections[1]).intersection_at_y(@y)
  end
end