This makes the design much simpler and avoids coding things multiple times for the two primitive types. Cylinder Mathematics A cylinder is just a 2D circle extruded into three dimensions. Notice that a circle is just a sphere in one lower dimension. This means that all the forumas you used for spheres pretty much apply to the cylinder computations. Here, X is a point on the sphere or circle, C is the center, and r is the radius.
P is the start of the ray and V is the direction.
For cylinders with caps, you will also have to consider the intersections of the ray with the planes of the cylinder caps. N is the normal of the plane. For axis-aligned cylinders, the normal will be a unit vector in the direction of the axis and d is the distance of the plane from the origin. There will be either 0 or 1 intersection. The intersection only counts if it is within the cylinder's circle, of course.