class Rubytracer::Sphere

Attributes

material[R]

Public Class Methods

new(center, radius, material) click to toggle source
# File lib/rubytracer/shapes/sphere.rb, line 5
def initialize(center, radius, material)
  @center = center
  @radius = radius
  @material = material
end

Public Instance Methods

intersect(ray) click to toggle source
# File lib/rubytracer/shapes/sphere.rb, line 15
def intersect(ray)
  q = @center - ray.start
  v_dot_q = ray.dir.dot(q)
  square_diffs = q.dot(q) - @radius ** 2
  discrim = v_dot_q ** 2 - square_diffs
  if discrim >= 0
    root = Math.sqrt(discrim)
    t0 = v_dot_q - root
    t1 = v_dot_q + root
    [t0, t1]
  else
    [Float::INFINITY, -Float::INFINITY] # May not be portable
  end
end
normal(point) click to toggle source
# File lib/rubytracer/shapes/sphere.rb, line 11
def normal(point)
  (point - @center).unit
end