Intersection and distance between two affine subspaces Given are two $m$-dimensional affine subspaces embedded in $\mathbb{R}^n$
$$a_{1i}x_{1i} + a_{2i}x_{2i}  + \cdots + a_{ni}x_{ni} = a_{0i}$$
$$b_{1i}x_{1i} + b_{2i}x_{2i}  + \cdots + b_{ni}x_{ni} = b_{0i}$$
where $i = 1 ... m$ define the $m$ hyperplanes defining each of the subspaces. The goal is to compute their intersection. If they do not intersect, also compute the (orthogonal) shortest distance between the two subspaces.
Not sure if I formulated the problem correctly, so I'll provide examples:


*

*The intersection of two lines in 3D (respectively defined by the intersection of two planes, i.e., $m=2$) can either be empty or a point. In case they do not intersect, I would like to compute the shortest distance between the lines.

*The intersection of two planes in 4D (respectively defined by the intersection of 3 hyperplanes, i.e., $m=3$) can either be empty, a point, or a line. In case they do not intersect, I would like to compute the shortest distance between the two planes.


My approach would be to put all the hyperplanes in a matrix and compute the nullspace of the matrix to compute the span of the solution. However, I would like to get this confirmed by somebody else and I am not sure how to compute the distance. The final goal is to implement this in a program, so any (pseudo) code would be highly appreciated.

Let's start with 3D and 2 lines. If they are parallel, normalizing their coefficients (the normal to both) , and dividing by the same factor the known term to the right, then the difference of the normalized known terms (taken in case with the absolute value) is the distance between the lines.   
Can you then proceed and extend ?
