[sci.electronics] Power Glove math

f@Alliant.COM (Bill Freeman) (01/12/91)

If something like this has come by before, I have missed it, so please forgive
me.  If not, here is my candidate for an easily computable solution to power
glove position.

	This gets really simple with the right coordinate system, given that
the receivers are arranged as the corners of a right triangle.  Put the origin
at the receiver at the right angle.  If the transmitter is at (x, y, z), and
the distance between the transmitter and this receiver is is called d0, then:
1.		x**2 + y**2 + z**2 = d0**2
(where "foo**2" means foo squared).  Another receiver goes at (W, 0, 0), and
the distance between it and the transmitter is called dX, so:
2.		(x - W)**2 + y**2 + z**2 = dX**2
or:
3.		y**2 + z**2 = dX**2 - (x - W)**2
Substituting for y**2 + z**2 in eq. 1:
4.		x**2 + dX**2 - (x - W)**2 = d0**2
5.		x**2 - (x**2 - 2*W*x + W**2) = d0**2 - dX**2
6.		2*W*x = W**2 + d0**2 - dX**2
7.		x = W/2 + (d0**2 - dX**2)/(2*W)
The last receiver goes at (0, H, 0), its distance from the transmitter is dY,
and, by similar reasoning to eqs. 2 through 7:
8.		y = H/2 + (d0**2 - dY**2)/(2*H)
If this leaves you with the x axis vertical or the y axis increasing down, or
a left handed coordinate system, and you don't like that, you fix it.  It isn't
hard.  Lastly, eq. 1 can be rearranged as follows:
9.		z**2 = d0**2 - x**2 - y**2
10.		z = (d0**2 - x**2 - y**2)**0.5
Just the positive square root will do, assuming you want z positive in the area
where you will use the glove.

	The squares or the distances are found with 3 multiplies.  x and y each
then require an additional subtract, divide by a constant, and add a constant.
z then requires 2 multiplies to form the squares of x and y, two subtracts, and
a square root.  If you are clever about your scale factors, the divides can
be shifts, or even eliminated, but at least note that divide by a constant is
the same thing as multiply by a constant so long as you can express fractions
(since the i-860 has no divide instruction, that is a consideration for such
an method on our machines).  But playing it straight, you need:
	2 adds of a constant
	2 divides by a constant
	3 subtracts
	5 multiplies (by self, i.e.; squarings)
and	1 square root.

A snipit in C:

	d0 *= d0;
	dX *= dX;
	dY *= dY
	x = W/2.0 + (d0 - dX)/(2.0 * W);
	y = H/2.0 + (d0 - dY)/(2.0 * H);
	z = sqrt(d0 - x*x - y*y);

	Obviously, if you don't have fast floating point math on your micro of
choice, you can cheaply do fixed point on integer hardware.

	If your divide is peppy, Newton's method is probably best for square
root.  Otherwise there is a bit at a time method that will remind you of the
hardware for divide, which is just the school boy longhand square root method
done in binary rather than decimal.  I believe that it can even be non-
restoring if that helps on your processor.
-- 
-- 
...!{decvax!linus,mit-eddie}!alliant!f		Bill Freeman	KE1G
alliant!f@eddie.mit.edu		PP-SMEL