I am writing a simple raytracer, and I want to be able to raytrace capped vertical cone and cylinder primitives. I need to be able to find intersection points and normals for use in lighting and coloring calculations in the Phong lighting model and reflection calculations.
In my raytracer, a cone is described by:
- A vector $\mathbf{c}$ describing the position of the base of the cone.
- A scalar base radius $r$ and height $h$.
Cones would only be vertical, not horizontal or any other direction. Ideally, this would work for both positive and negative $h$. I.e. negative $h$ would cause the cone to point down. Something like this:
A cylinder is described by:
- A vector $\mathbf{c}$ describing its center
- A scalar radius $r$ and height $h$.
Something like this:
A ray is described by a vector $\mathbf{o}$ (its origin) and a unit vector $\mathbf{d}$ (its direction). A point on the ray, for some distance $t$, is located at $\mathbf{o} + t\mathbf{d}$. I cannot seem to find any simple-ish (working) methods to:
- Test for intersection of these primitives with a ray.
- Find the point of intersection.
- Calculate the normal at that point.
Also, be mindful that the $y$-axis is the "up" axis in my raytracer.


The specification of the cone and the cylinder is incomplete. To correctly specify a cone, you need its vertex $V$, its axis (unit vector) $a$, and the semi-vertical angle $\theta$. The semi-vertical angle is the angle between the axis of the cone, and its surface. The semi-vertical angle is related to the height $h$ and the radius $r$ of the base by
$ \theta = \tan^{-1} \dfrac{r}{h} $
The algebraic equation of the cone is
$ (r - V)^T ( \cos^2 \theta I - a a^T ) (r - V) = 0 $
where $r = [x, y, z]^T $
Now, for the cylinder, if a point on the axis is $C$ and the axis unit vector is $a$, and the radius is $r$, then the equation is
$ (r - C)^T (I - a a^T ) (r - C) = r^2 $
To intersect the ray $r = o + t d$ with either surface, just plug in $r$ into the algebraic equation of either the cone or the cylinder. You will get a quadratic equation in the parameter $t$. Solving by the quadratic formula, gives the values of $t$ where the intersections (if any) occur.
The normal vector at the intersection point $r$, for the cone is given by
$ g_{cone} = 2 ( \cos^2 \theta I - a a^T ) (r - V) $
and the normal vector at the intersection point $r$, for the cylinder is given by
$ g_{cylinder} = 2 ( I - a a^T) (r - C) $