# INTERSECTING BOX AND CYLINDER

In this example we will calculate the three dimensional coordinates of holes through a precast box.  We can think of this as a cylinder intersecting the wall of a box while laying on its side.  These calculations are possible using cross products to develop a generic mathematical formula but we can simplify this by using a definite cylinder height of 1000 inches.  You will see in the diagrams below details for the points we are going to calculate.  By the end of this we will create a formula to layout round, arch or elliptical holes at any rotation through a box wall.  All dimensions will be in inches.   The First step is to plot our hole on the plane y=0.  The x,y,z can be expressed using the following parametric equation where r=hole radius.

$0<=t<=2\pi$

$x(t) = r * \cos(t)$

$y(t) = 0 (On Plane y=0)$

$z(t) = r * \sin(t)$

Next we want to derive an equation to work if the hole is rotated at an angle phi about the z axis.

$0<=t<=2\pi$

$\begin{pmatrix} x_0(t) \\y_0(t) \\z_0(t) \end{pmatrix}=\begin{pmatrix} cos(\phi)&-sin(\phi)&0 \\sin(\phi)&cos(\phi)&0 \\0&0&1 \end{pmatrix}\begin{pmatrix} r * \cos(t)\\0 \\r * \sin(t)\end{pmatrix}$

$\begin{pmatrix} x_0(t) \\y_0(t) \\z_0(t) \end{pmatrix}=\begin{pmatrix} r * \cos(t)*\cos(\phi)\\r*\cos(t)*\sin(\phi)\\r * \sin(t)\end{pmatrix}$

Next we want to derive an equation for the hole initially located on the y=-1000 plane and rotated at an angle phi about the z axis.

$0<=t<=2\pi$

$\begin{pmatrix} x_1(t) \\y_1(t) \\z_1(t) \end{pmatrix}=\begin{pmatrix} \cos(\phi)&-sin(\phi)&0 \\sin(\phi)&cos(\phi)&0 \\0&0&1 \end{pmatrix}\begin{pmatrix} r * \cos(t)\\-1000 \\r * \sin(t)\end{pmatrix}$

$\begin{pmatrix} x_1(t) \\y_1(t) \\z_1(t) \end{pmatrix}=\begin{pmatrix} r * \cos(t)*\cos(\phi)+1000*\sin(\phi)\\r*\cos(t)*\sin(\phi)-1000*\cos(\phi)\\r * \sin(t)\end{pmatrix}$

Now we need to find an equation for the line between the circle at the origin and the point 1000 units away through points

$P_1=(r\cos(t)*cos(\phi),r\cos(t)*sin(\phi),r*sin(\phi))$

and

$P_2=(r\cos(t)*cos(\phi)+1000*sin(\phi),r\cos(t)*sin(\phi)-1000*\cos(\phi),r*sin(t))$

and now we find the vector r(t) between P1 and P2.  The formula for a line in parametric form has is r(t)=P+tD where P is a point on the line and D is a direction vector for the line.  In our case the direction vector D will be P2-P1 and we know a point P on the line will be P2.

$r(\tau)=P_1+\tau*(P_2-P_1)$

$r(\tau)=(r\cos(t)*cos(\phi),r\cos(t)*sin(\phi),r*sin(t)) + \tau*(r\cos(t)*cos(\phi)+1000*sin(\phi)-r\cos(t)*cos(\phi),r\cos(t)*sin(\phi)-1000*\cos(\phi)-r\cos(t)*sin(\phi),r*sin(\phi)-r*sin(\phi))$

$r(\tau)=(r\cos(t)*cos(\phi),r\cos(t)*sin(\phi),r*sin(t))+\tau*(1000*sin(\phi),-1000\cos(\phi),0)$

$r(\tau)=(r\cos(t)*cos(\phi)+\tau*1000*sin(\phi),r\cos(t)*sin(\phi)-\tau*1000\cos(\phi),r*sin(t)$

The next step is to calculate the intersection point of this parametric line and the plane at each wall 0,90,180 and 270.  We will use substitution in the above parametric equation to solve.

0 Degree Plane Intersection y=-w/2

We start with the y term of the parametric line since the 0 degree wall is on the plane y=-w/2.

$\frac{-w}{2}=r\cos(t)*sin(\phi)-\tau*1000\cos(\phi)$

$\frac{-w}{2}-r\cos(t)*sin(\phi)=-\tau*1000\cos(\phi)$

$\tau=\frac{\frac{w}{2}+r\cos(t)*sin(\phi)}{1000\cos(\phi)}$

Now that we have Tau we can substitute back into the parametric equation to get equations for x,y and z on the 0 degree wall/plane. L= Box Length and w = Box Width

$r(\tau)=(r\cos(t)*cos(\phi)+\tau*1000*sin(\phi),r\cos(t)*sin(\phi)-\tau*1000\cos(\phi),r*sin(t)$

Solve for x:

$x_i=(r\cos(t)*cos(\phi)+\frac{\frac{w}{2}+r\cos(t)*sin(\phi)}{1000\cos(\phi)}*1000*sin(\phi)$

$x_i=r\cos(t)*cos(\phi)+\frac{\frac{w}{2}\sin(\phi)+r\cos(t)*sin^2(\phi)}{cos(\phi)}$

Solve for y:

$y_i=r\cos(t)*sin(\phi)-\frac{\frac{w}{2}+r\cos(t)*sin(\phi)}{1000\cos(\phi)}*1000\cos(\phi)$

$y_i=r\cos(t)*sin(\phi)-\frac{w}{2}-r\cos(t)*sin(\phi)$

Solve for z:

$z_i=r*sin(t)$

90 Degree Plane Intersection x=-L/2

We start with the x term of the parametric line since the 90 degree wall is on the plane x=-L/2.

$\frac{-L}{2}=r\cos(t)*cos(\phi)+\tau*1000*sin(\phi)$

$\frac{-L}{2}-r\cos(t)*cos(\phi)=\tau*1000\sin(\phi)$

$\tau=\frac{\frac{-L}{2}-r\cos(t)*\cos(\phi)}{1000\sin(\phi)}$

Now that we have Tau we can substitute back into the parametric equation to get equations for x,y and z on the 90 degree wall/plane. L= Box Length and w = Box Width

$r(\tau)=(r\cos(t)*cos(\phi)+\tau*1000*sin(\phi),r\cos(t)*sin(\phi)-\tau*1000\cos(\phi),r*sin(t)$

Solve for x:

$x_i=r\cos(t)*cos(\phi)+\frac{\frac{-L}{2}-r\cos(t)*\cos(\phi)}{1000\sin(\phi)}*1000*sin(\phi)$

$x_i=r\cos(t)*cos(\phi)-.5L-r\cos(t)*cos(\phi)$

Solve for y:

$y_i=r\cos(t)*sin(\phi)-\frac{\frac{-L}{2}-r\cos(t)*\cos(\phi)}{1000\sin(\phi)}*1000\cos(\phi)$

$y_i=r\cos(t)*sin(\phi)+\frac{\frac{w}{2}\cos(\phi)+r\cos(t)*cos^2(\phi)}{sin(\phi)}$

Solve for z:

$z_i=r*sin(t)$

180 Degree Plane Intersection y=w/2

We start with the y term of the parametric line since the 180 degree wall is on the plane y=w/2.

$\frac{w}{2}=r\cos(t)*sin(\phi)-\tau*1000\cos(\phi)$

$\frac{-w}{2}+r\cos(t)*sin(\phi)=\tau*1000\cos(\phi)$

$\tau=\frac{\frac{-w}{2}+r\cos(t)*\sin(\phi)}{1000\cos(\phi)}$

Now that we have Tau we can substitute back into the parametric equation to get equations for x,y and z on the 180 degree wall/plane. L= Box Length and w = Box Width

$r(\tau)=(r\cos(t)*cos(\phi)+\tau*1000*sin(\phi),r\cos(t)*sin(\phi)-\tau*1000\cos(\phi),r*sin(t)$

Solve for x:

$x_i=(r\cos(t)*cos(\phi)+\frac{\frac{-w}{2}+r\cos(t)*\sin(\phi)}{1000\cos(\phi)}*1000*sin(\phi)$

$x_i=r\cos(t)*cos(\phi)-\frac{\frac{w}{2}\sin(\phi)+r\cos(t)*\sin^2(\phi)}{cos(\phi)}$

Solve for y:

$y_i=r\cos(t)*sin(\phi)-\frac{\frac{-w}{2}+r\cos(t)*\sin(\phi)}{1000\cos(\phi)}*1000\cos(\phi)$

$y_i=r\cos(t)*\sin(\phi)+\frac{w}{2}-r\cos(t)*sin(\phi)$

Solve for z:

$z_i=r*sin(t)$

270 Degree Plane Intersection x=L/2

We start with the x term of the parametric line since the 270 degree wall is on the plane x=L/2.

$\frac{L}{2}=r\cos(t)*cos(\phi)+\tau*1000*sin(\phi)$

$\frac{L}{2}-r\cos(t)*cos(\phi)=\tau*1000\sin(\phi)$

$\tau=\frac{\frac{L}{2}-r\cos(t)*\cos(\phi)}{1000\sin(\phi)}$

Now that we have Tau we can substitute back into the parametric equation to get equations for x,y and z on the 270 degree wall/plane. L= Box Length and w = Box Width

$r(\tau)=(r\cos(t)*cos(\phi)+\tau*1000*sin(\phi),r\cos(t)*sin(\phi)-\tau*1000\cos(\phi),r*sin(t)$

Solve for x:

$x_i=r\cos(t)*cos(\phi)+\frac{\frac{L}{2}-r\cos(t)*\cos(\phi)}{1000\sin(\phi)}*1000*sin(\phi)$

$x_i=r\cos(t)*cos(\phi)+.5L-r\cos(t)*cos(\phi)$

Solve for y:

$y_i=r\cos(t)*sin(\phi)-\frac{\frac{L}{2}-r\cos(t)*\cos(\phi)}{1000\sin(\phi)}*1000\cos(\phi)$

$y_i=r\cos(t)*sin(\phi)-\frac{\frac{w}{2}\cos(\phi)+r\cos(t)*cos^2(\phi)}{sin(\phi)}$

Solve for z:

$z_i=r*sin(t)$

Now we can solve for the three dimensional coordinates at any angle.  If the plotted coordinates fall outside the box or are extended past the wall we need to clip those points off.  To clip those points we can quickly trim points using the following ranges.

0 and 180 degree wall:

$\frac{-L}{2}\leq x\leq\frac{L}{2}$

90 and 270 degree wall:

$\frac{-w}{2}\leq y\leq\frac{w}{2}$

These ranges can be applied to all points where L=length of box and w = width of box.  All points must fall in the ranges below or should be excluded.

$\frac{-L}{2}\leq x_i\leq\frac{L}{2}\\\\ or \\\\\frac{-w}{2}\leq y_i\leq\frac{w}{2}$

$x_i=r\cos(t)*cos(\phi)+.5L-r\cos(t)*cos(\phi)$

$y_i=r\cos(t)*sin(\phi)-\frac{\frac{w}{2}\cos(\phi)+r\cos(t)*cos^2(\phi)}{sin(\phi)}$

$z_i=r*sin(t)$