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:



35
36
37
38
39
# File 'lib/ra/intersection.rb', line 35

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

Instance Attribute Details

#rayRa::Ray

Returns:



12
13
14
# File 'lib/ra/intersection.rb', line 12

def ray
  @ray
end

#shapeRa::Shape::Base

Returns:



16
17
18
# File 'lib/ra/intersection.rb', line 16

def shape
  @shape
end

#tNumeric

Returns:

  • (Numeric)


8
9
10
# File 'lib/ra/intersection.rb', line 8

def t
  @t
end

Class Method Details

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

Parameters:

  • intersections

    Array<Ra::Intersection>

Returns:



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ra/intersection.rb', line 20

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)


42
43
44
# File 'lib/ra/intersection.rb', line 42

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

#positionVector

Returns:

  • (Vector)


57
58
59
# File 'lib/ra/intersection.rb', line 57

def position
  @position ||= @ray.position(t: @t)
end

#surfaceRa::Surface

Returns:



47
48
49
50
51
52
53
54
# File 'lib/ra/intersection.rb', line 47

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

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

#xNumeric

Returns:

  • (Numeric)


62
63
64
# File 'lib/ra/intersection.rb', line 62

def x
  position[0]
end

#yNumeric

Returns:

  • (Numeric)


67
68
69
# File 'lib/ra/intersection.rb', line 67

def y
  position[1]
end

#zNumeric

Returns:

  • (Numeric)


72
73
74
# File 'lib/ra/intersection.rb', line 72

def z
  position[1]
end