Class: Ra::Intersection

Inherits:
Object
  • Object
show all
Defined in:
lib/ra/intersection.rb

Overview

An intersection tracks the time t at which a shape is intersected by a ray.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(t:, ray:, shape:) ⇒ Intersection

Returns a new instance of Intersection.

Parameters:



25
26
27
28
29
# File 'lib/ra/intersection.rb', line 25

def initialize(t:, ray:, shape:)
  @t = t
  @ray = ray
  @shape = shape
end

Instance Attribute Details

#rayObject

Returns the value of attribute ray.



6
7
8
# File 'lib/ra/intersection.rb', line 6

def ray
  @ray
end

#shapeObject

Returns the value of attribute shape.



6
7
8
# File 'lib/ra/intersection.rb', line 6

def shape
  @shape
end

#tObject

Returns the value of attribute t.



6
7
8
# File 'lib/ra/intersection.rb', line 6

def t
  @t
end

Class Method Details

.hit(intersections:) ⇒ Ra::Intersection?

Parameters:

  • intersections

    Array<Ra::Intersection>

Returns:



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/ra/intersection.rb', line 10

def self.hit(intersections:)
  hit = nil

  intersections.each do |interaction|
    next if interaction.t.negative?

    hit = interaction if hit.nil? || interaction.t < hit.t
  end

  hit
end

Instance Method Details

#==(other) ⇒ Boolean

Returns:

  • (Boolean)


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

def ==(other)
  t == other.t && ray == other.ray && shape == other.shape
end

#surfaceRa::Surface

Returns:



37
38
39
40
41
42
43
# File 'lib/ra/intersection.rb', line 37

def surface
  point = ray.position(t:)
  eyev = -ray.direction
  normalv = shape.normal(point:)

  Surface.new(shape:, eyev:, normalv:, point:)
end