lib/ruby_on_rays/shapes/sphere.rb
# Finds the intersection between the ray and the sphere.
# @param ray [Ray3]
# @return [nil] if no such intersection.
# @return [HitInfo] if an intersection was found.
Finds the intersection between the ray and the sphere.
Finds the intersection between the ray and the sphere .
def intersection(ray)
      a = ray.d.dot(ray.d)
      b = 2.0 * ray.d.dot(ray.o.sub(@c))
      c = @c2 - 2.0 * @c.dot(ray.o) - @r2
      d2 = b**2 - 4 * a * c
      return nil unless d2 > 0
      t0 = (-b - Math.sqrt(d2)) / (2 * a)
      t1 = (-b + Math.sqrt(d2)) / (2 * a)
      t0, t1 = t1, t0 if t0 > t1
      return nil if t1 < ray.tmin || t0 > ray.tmax
      t_hit = t0
      if t_hit < ray.tmin
        t_hit = t1
        return nil if t_hit > ray.tmax
      end
      p_hit = ray[t_hit]
      n_hit = (p_hit.sub(@c).div @r).to_n
      { t: t_hit, p: p_hit, n: n_hit, shape: self }
    end
def intersection ( ray ) a = ray . d . dot ( ray . d ) b = 2.0 * ray . d . dot ( ray . o . sub ( @c ) ) c = @c2 - 2.0 * @c . dot ( ray . o ) - @r2 d2 = b ** 2 - 4 * a * c return nil unless d2 > 0 t0 = ( - b - Math . sqrt ( d2 ) ) / ( 2 * a ) t1 = ( - b + Math . sqrt ( d2 ) ) / ( 2 * a ) t0 , t1 = t1 , t0 if t0 > t1 return nil if t1 < ray . tmin || t0 > ray . tmax t_hit = t0 if t_hit < ray . tmin t_hit = t1 return nil if t_hit > ray . tmax end p_hit = ray [ t_hit ] n_hit = ( p_hit . sub ( @c ) . div @r ) . to_n { t : t_hit , p : p_hit , n : n_hit , shape : self } end
Finds the intersection between the ray and the sphere.
Finds the intersection between the ray and the sphere .
if no such intersection.
if no such intersection .
nil
if an intersection was found.
if an intersection was found .
HitInfo
ray
<not_specific>
<not_specific>
None