Solve equations for distance

Let's say that I have a camera positioned in front of a wall. The camera is looking straight at the closest point on the wall. The camera can see 2 horizontal lines on the wall (extending to infinity). Taking into account the camera's field of view and the y position (up and down) of the lines in the camera's viewport, I can compute the angle between the 2 lines ($a$). I also know the distance of both of the lines to the floor ($i$ and $j$). Using this information, I need to compute the distance of the camera to the base of the wall ($d$).

Here is a side-view diagram of this problem:

Here are the 2 equations (with 2 unknowns) that I have come up with:

$$\sin(a+b)d-\sin(b)d=j$$ $$\sin(b)d=i$$

So, the question is how to solve these 2 equations for $b$ and $d$. I feel like I'm missing something really simple. I'd really appreciate it if someone could point me in the right direction.

-

Your equations are not quite right. The right equations, in your notation, are $$d(\tan(a+b)-\tan b)=j \quad\text{and}\quad d\tan b=i.$$ For recall that "opposite" divided by "adjacent" is the tangent, not the sine.

Now we proceed to solve the corrected equations. For my own comfort, I would like to use $x$ instead of $d$.

A key ingredient is the addition law for the tangent function. It is derivable from the perhaps more familiar addition laws for sine and cosine. The equation is $$\tan(a+b)=\frac{\tan a+\tan b}{1-\tan a\tan b}.$$

We have $x\tan b=i$ and $x\tan(a+b)=i+j$. Applying the addition law for $\tan(a+b)$ we get $$\frac{x(\tan a+\tan b)}{1-\tan a\tan b}=i+j.$$ But $\tan b=\frac{i}{x}$. Substitute $\frac{i}{x}$ for $\tan b$ in the above equation, and simplify algebraically. After a while we arrive at the quadratic equation $$(\tan a)x^2-jx +(i)(i+j)(\tan a)=0.$$ Solve for $x$, using the Quadratic Formula.

Note that in general there will be two solutions. This is because in general there are two positions of the camera for which the angle subtended at the eye by the two parallel lines is equal to $a$. You can see this yourself by thinking of extreme cases. How can the subtended angle be tiny? There are two positions, one quite far from the wall, and another one quite close to the wall.

-
Thanks. I think your last equation should be $(\tan a)x^2-jx +(i)(i+j)(\tan a)=0$. Is this correct? –  Joel Feb 24 '12 at 18:29
@Joel: Yes, thanks, a slip, soon to be corrected. –  André Nicolas Feb 24 '12 at 18:31
Solved for the quadratic equation: $\frac{j\pm\sqrt{j^2-4(\tan a)^2i(i+j)}}{2\tan a}$. Put some values in, and the equation seems to work. Thanks so much for your help. –  Joel Feb 24 '12 at 18:35
@Joel: You are welcome. I am glad that it is doing the job. –  André Nicolas Feb 24 '12 at 18:46

There may not be a closed-form solution. Certainly there isn't a unique solution, because $a$ goes towards 0 for $d\to0$ as well as $d\to\infty$. So you may have to solve it numerically.

On the other hand, if you're sure the camera is pointing right at the wall, then why not just measure angle $b$ instead? Then no trigonometry is needed: $d$ becomes $i$ times the focal length of the camera lens (which you'd need to know in order to measure $a$ anyway) divided by the distance between the image of the lower line and the center of the camera image.

-
I realize that there will be 2 solutions. I will have a general idea of b (within about 10 degrees), but not an exact value. So, I could use b to determine if the camera is right beside the wall or far away from the wall, to get a unique solution. –  Joel Feb 24 '12 at 17:57