Class: Ra::Intersection
- Inherits:
-
Object
- Object
- Ra::Intersection
- 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
- #==(other) ⇒ Boolean
-
#initialize(t:, ray:, shape:) ⇒ Intersection
constructor
A new instance of Intersection.
- #position ⇒ Vector
- #surface ⇒ Ra::Surface
- #x ⇒ Numeric
- #y ⇒ Numeric
- #z ⇒ Numeric
Constructor Details
#initialize(t:, ray:, shape:) ⇒ Intersection
Returns a new instance of Intersection.
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
#t ⇒ Numeric
8 9 10 |
# File 'lib/ra/intersection.rb', line 8 def t @t end |
Class Method Details
.hit(intersections:) ⇒ Ra::Intersection?
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
42 43 44 |
# File 'lib/ra/intersection.rb', line 42 def ==(other) t == other.t && ray == other.ray && shape == other.shape end |
#position ⇒ Vector
57 58 59 |
# File 'lib/ra/intersection.rb', line 57 def position @position ||= @ray.position(t: @t) end |
#surface ⇒ Ra::Surface
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 |
#x ⇒ Numeric
62 63 64 |
# File 'lib/ra/intersection.rb', line 62 def x position[0] end |
#y ⇒ Numeric
67 68 69 |
# File 'lib/ra/intersection.rb', line 67 def y position[1] end |
#z ⇒ Numeric
72 73 74 |
# File 'lib/ra/intersection.rb', line 72 def z position[1] end |