# Chapter 28: Spheres

## Overview

The implict equation for a sphere is:

where is the radius of the sphere.

Assuming the sphere has some center (offset) , then the implicit equation can be:

Using the equation of a ray:

We want to solve for any values of that are points on the sphere.

## Vectors

First we write the sphere equation in vector form (using vector dot product):

Now, plug in our ray equation for p:

If we can rearrange these terms to look like a quadratic equation () then we can use the quadratic equation to solve for t.

But first, let’s cover some fundamentals of dot products.

### Dot Product

The dot product operator has the following algebratic properties:

**Commutative Property****Scalar Multiplcation Property****Distributive Property**

None of those should be particularily suprising (especially if you work out the underlying component-wise math) but that last property lets us do something kind of insteresting.

## Derivation

Consider:

Multiply the first two quantities (as we just did above)

or… (rearrange terms)

(Note that any time we use for some vector v we mean )

Now we have a quadratic equation with the following parts:

## Quadratic Equation

Recall:

This part is the determinant:

When the determinant is negative, there are no (real) solutions to this quadratic equation. This indicates no intersection between the ray and the sphere.

If the determinant is zero, there is exactly one solution. This indicates the ray just barely brushes the surface of the sphere.

In all other cases, there are multiple solutions. This indicates the ray passes through the sphere in the usual way. Typically, we are concerned with the smaller value, as this is the “closer” intersection.

Be careful, however, about rays that originate inside the sphere!
For such rays, the smaller value will be negative.
Typically we do not want to consider any negative intersections,
so you more likely want to return the smallest *positive* intersection.

This distinction becomes important when dealing with:

- shadow feeler rays
- reflection/refraction rays