Class: Rubytracer::Sphere

Inherits:
Object
  • Object
show all
Defined in:
lib/rubytracer/shapes/sphere.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(center, radius, material) ⇒ Sphere

Returns a new instance of Sphere.



5
6
7
8
9
# File 'lib/rubytracer/shapes/sphere.rb', line 5

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

Instance Attribute Details

#materialObject (readonly)

Returns the value of attribute material.



3
4
5
# File 'lib/rubytracer/shapes/sphere.rb', line 3

def material
  @material
end

Instance Method Details

#intersect(ray) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 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) ⇒ Object



11
12
13
# File 'lib/rubytracer/shapes/sphere.rb', line 11

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