I agree with your algorithm, DJ. Several such algorithms can be found
on the web, often for computing compound miters for crown molding,
which is essentially the same problem. But many of them give slightly
different answers. Why is yours right, and what's wrong with the other
If you don't want to see a bunch of crazy math, stop reading now.
One of the other common algoriths for computing the miter (x in your
notation) is x=1/2*arccos(cos^2(b)*cos(a)+sin^2(b)). This is a pretty
simple equation to derive using simple vector algebra. Going back to
first semester Calculus, recall that the dot product of two vectors is
defined as a scalar value equal to the product of the vector magnitudes
times the cosine of the angle between them.
A.dot.B = |A| * |B| * cos(alpha),
where alpha is the angle formed between them, and the | | notation
means magnitude (i.e. length, independent of direction).
A second way to calculate the dot product is to write the vectors as
functions of the unit vectors i, j, and k which are simply vectors of
length 1 along the x, y, and z axes. If the vectors are written as A ax*i + ay*j + az*k and B = bx*i + by*j + bz*k then their dot product is
A.dot.B = ax*bx + ay*by + az*bz
Now if we simply find two vectors which form one wedge of the ten-sided
roof, we can easily compute the angle between them by using the two
definitions of dot product. It's an easy construction:
If we take the peak of the roof to be the point (0,0,0), and we imagine
ten rafters radiating outward, angled down with a pitch (slope) of
5/12, then it's easy to find their endpoints (which will define our
vectors). If we assume for simplicity's sake that they have a length
of 1 foot, then one of the rafters would stretch from (0,0,0) to
(cos(atan(5/12)),0,-sin(atan(5/12))). Since the choice of 5/12 for a
pitch gives us a 5-12-13 right triangle, we can simpify the second
coordinate of the vector to (12/13,0,-5/13). A second rafter would
start at (0,0,0) and go to (12/13*cos(36), 12/13*sin(36), -5/13). The
36 degree angle is the angle of one wedge of roof when viewed from
directly above (i.e. there are ten sides so the the angle is a tenth of
Now that we have two vectors, we can compute their dot product both of
the ways desribed above.
A.dot.B = |A| * |B| * cos (angle) = 1 * 1 * cos (alpha) = cos(alpha)
A.dot.B = 12/13*12/13*cos(36) + 0*12/13*sin(36) + 5/13*5/13
Equating the two different definitions we get ((12/13)^2*cos(36) +
(5/13)^2)= cos (alpha)
Thus alpha = arccos(0.83727) = 33.147 degrees.
This is the angle between the two vectors (i.e. the two roof rafters).
The miter angle is simply going to be half of this angle, or
x = 16.57 degrees.
This is pretty close to what DJ's formula gives us, yet it's slightly
I'll post the reason next. I don't want this one post to get too long.