Here's a solution to your problem Shadron:
Define a line thusly:
[latex]$
\vec P(u) = \vec p + \vec d \cdot u
[/latex]
Lowercase p is any arbitrary point on the line and lower case d is a vector which points in the direction of the line. This is a parametric function in "u" that returns points on the line.
We can define two lines which represent the center lines of your walls:
[latex]$
\vec {P_1}(u) = \vec {p_1} + \vec {d_1} \cdot u
[/latex]
[latex]$
\vec {P_2}(v) = \vec {p_2} + \vec {d_2} \cdot v
[/latex]
These lines meet where [latex]$
\vec {P_1}(u) = \vec {P_2}(v)
[/latex]
So we can write :
[latex]$
\vec {p_1} - \vec {p_2} = - \vec {d_1} \cdot u + \vec {d_2} \cdot v
[/latex]
Now write it in matrix form.
[latex]$
\[
\begin{bmatrix}
- \vec {d_1} & \vec {d_2}
\end{bmatrix} \] \cdot
\[
\begin{bmatrix}
\ u \\
\ v
\end{bmatrix} \] =$ \vec {p_1} - \vec {p_2}[/latex]
For two dimensions, we can expand the vectors as column vectors:
[latex]$
\[
\begin{bmatrix}
\ -d_1.x & d_2.x \\
\ -d_1.y & d_2.y
\end{bmatrix} \] \cdot
\[
\begin{bmatrix}
\ u \\
\ v
\end{bmatrix} \] = $
\[
\begin{bmatrix}
\ p_1.x - p_2.x \\
\ p_1.y - p_2.y
\end{bmatrix} \][/latex]
[latex]$
A \cdot
\[
\begin{bmatrix}
\ u \\
\ v
\end{bmatrix} \] = $
b[/latex]
Note that A is a square matrix so it's a snap to invert it, multiply it to b and that gives you your solution to u and v.
*Note: I'll talk about the case for 3 or more dimensions at the end of this post.
[latex]$
\[
\begin{bmatrix}
\ u \\
\ v
\end{bmatrix} \] = $ A^{-1} \cdot
b[/latex]
Plug u into P1(u) or v into P2(v) and that gives you the point where the center lines cross. You may be wondering at this point why I explained all this since you already know how to find the crossing point of two lines. The reason is that describing lines and solving them this way allows us to do some really neat things to solve your problem.
For instance, we can define two new lines which are parallel to the center line but are moved over by half the thickness of each wall.
[latex]$
\vec {P_3}(u) = (\vec {p_1} + \vec{d_1}\perp \cdot 0.5T_1) + \vec {d_1} \cdot u = \vec {p_3} + \vec {d_1} \cdot u
[/latex]
[latex]$
\vec {P_4}(v) = (\vec {p_2} + \vec{d_2}\perp \cdot 0.5T_2) + \vec {d_2} \cdot v = \vec {p_4} + \vec {d_2} \cdot u
[/latex]
The terms I added here are normalized (magnitude=1) vectors which are perpendicular to the center line and are multiplied by half the thickness. A program would need to establish that the perpendicular vectors are pointing to the inside of the corner which you want to make the miter for.
Run through the steps of solving u and v once more (with p3 substituted for p1 and p4 for p2) and then you can get the point where the walls meet on the inside corner.
Let's define the two points that have been found so far:
[latex]$
\vec {X_1} = [/latex]the point where the center lines cross
[latex]$
\vec {X_2} = [/latex]the point where the walls meet at the inside corner
Now you can get a normalized vector which points along the miter joint.
[latex]$
\vec {M} = (\vec {X_2} - \vec {X_1}) / |\vec {X_2} - \vec {X_1}| [/latex]
Assuming that the vectors pointing along the center lines have been normalized, you can get the angle of the miter cuts from the cross products.
[latex]$
\theta_1 = |\vec {M} \times \vec {d_1}| \\
\theta_2 = |\vec {M} \times \vec {d_2}|
[/latex]
* As promised, here's what happens when you try to solve the crossing point for two lines in 3 dimensions.
[latex]$
\[
\begin{bmatrix}
\ -d_1.x & d_2.x \\
\ -d_1.y & d_2.y \\
\ -d_1.z & d_2.z
\end{bmatrix} \] \cdot
\[
\begin{bmatrix}
\ u \\
\ v
\end{bmatrix} \] = $
\[
\begin{bmatrix}
\ p_1.x - p_2.x \\
\ p_1.y - p_2.y \\
\ p_1.z - p_2.z
\end{bmatrix} \][/latex]
[latex]$
A \cdot
\[
\begin{bmatrix}
\ u \\
\ v
\end{bmatrix} \] = $
b[/latex]
In this case our matrix A is a 3 by 2 matrix so it's not directly invertible. You can use the projection matrix here.
[latex]$
\[
\begin{bmatrix}
\ u \\
\ v
\end{bmatrix} \] = $(A^{T}A)^{-1}A^T
b[/latex]
The projection matrix will yield a sort of "least square fit" to the problem. If the lines don't actually cross the solution will be the u and v parameter that corresponds to P1(u) and P2(v) which are the points where the lines come closest.
Note that if your walls are parallel, the solution is two infinite sets of points that are all equally distant. In other words, the solution is both lines. In this instance, (A^T*A) won't be invertible. The computer will find a divide by zero error.
If you're working in three dimensions, it's possible that you intended for your lines to cross precisely but because of a computer's imprecision they'll appear to miss by a tiny margin.
Well, that was a nice little Saturday morning project to drink coffee to. That'll be $150 USD for engineering consulting fees please
