Sha256: 1d44dad9962fd4bc68f60a4170c08c9b841d79d03874b9895f065778489e08de

Contents?: true

Size: 654 Bytes

Versions: 1

Compression:

Stored size: 654 Bytes

Contents

module Rubytracer
  class Sphere
    attr_reader :material

    def initialize(center, radius, material)
      @center = center
      @radius = radius
      @material = material
    end

    def normal(point)
      (point - @center).unit
    end

    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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubytracer-0.1.0 lib/rubytracer/shapes/sphere.rb