# Day 4, Intersections

More math heavy stuff, calculating the positions along a ray, where it intersects a sphere. Turns out this is as simple as solving a quadratic equation, with some special coefficients.

\[\|\dot{O} + t\overrightarrow{D} - \dot{C}\|^2 = R^2\]```
def intersect(ray)
sphere_to_ray = ray.origin - @origin
a = ray.direction.dot(ray.direction)
b = 2 * ray.direction.dot(sphere_to_ray)
c = sphere_to_ray.dot(sphere_to_ray) - @radius_squared
intersections = quadratic(a, b, c).map { |t| Intersection.new(t, self) }
Intersections.new(*intersections)
end
```

I also had to refactor the symbolic evaluation helpers I use to evaluate the gherkin expressions that makes up the specification.

```
def seval(recv, method = nil, *args)
case method
when :'='
instance_variable_set(recv, seval(*args))
when nil
recv.is_a?(Symbol) ? instance_variable_get(recv) : recv
else
fcall(map_operator(method), recv, *args)
end
end
def fcall(method, *args)
method.to_proc.call(*args.map do |arg|
arg.is_a?(Symbol) ? instance_variable_get(arg) : arg
end)
end
```

## Next related

### Day 5, Assume nothing

Today I Learned that I shouldn’t have assumed the sphere would ever need to change from being a unit sphere, so intersecting gets even simpler:

## Previous related

### Day 3, Casting rays into spheres

Finally some practical applications of the math heavy weekend, casting abstract rays at abstract spheres. Well, on a sliding scale from linear algebra to physical representation, it’s still a step in the direction.

### Day 2, Matrices, matrices, matrices. Oh, and more matrices

Today was spent on a heavy chunk of linear algebra, matrices. This is the foundation for everything else, so time well spent reimplementing what’s already in the stdlib, just to refresh my math skills. If there are still days left of Devember when I’m finished, I’ll come back and replace all this with the stdlib implementation instead, hoping I’m somewhat close to the same API.

### Day 1, Overdoing it

The first proper day of Devember is here. Of course I overdid it and spent all day coding, instead of just that one hour.