Sunday, September 23, 2007

HP-35s Euclid Implementation - V : Closest point on a line to a point

The following program finds the parameter giving the closest point (problem 6 (and 5) in the requirements) on the line entered by the L program to a given point.

Remembering that the positional vector for first point defining the line is in variable A, second point is in variable B and saying that the point in question's positional vector is in the X stack register the formula for the parameter is: t=((X-A)*(B-A))/((B-A)*(B-A)).

Stack Input/Output:
[[x, y, z], Y, Z, T | L]-> XEQ C ->[t, Y, Z, T | [x, y, z]]
Where (x, y, z) is the point p in question and t is the parameter defining the closest point on the line to p.

Variables:

Reads:
A: First point defining the line, populated by the L program.
B: Second point defining the line, populated by the L program.
Program:
C001 LBL C
C002 XEQ U001
C003 RCL A
C004 -
C005 RCL B
C006 RCL A
C007 -
C008 x
C009 LASTX
C010 ENTER
C011 x
C012 /
C013 XEQ U050
C014 RTN
Comments:

Terms of use.

Mnemonic: C for closest point.

The program does not calculate the coordinates for the closest point on line since end user may only be interested in the parameter that tells if point is on the defining line segment or on which ray. The coordinates are only one program away: XEQ E.

I decide not to implement a program for problem 5 (distance from a 3D line to a point) in the requirements since end user easily now can find the perpendicular line from the point to the line, and length of it's defining line segment is the distance from line to the point. The operation sequence after this program is run:

LASTX // Get back point which to find perpendicular line on stored line or distance.
X<>Y // Parameter back in X stack register.
XEQ E // Find coordinates to closest point on line.
// Now Y and X stack registers contains two points defining the perpendicular
// line, one could now run program L to store this line for further
// computation relating to that line.
- // Continuing to find distance: Get perpendicular line segment directional
// vector of segment's length in X and
ABS // the length.
This program works for both 2D and 3D data so do not need to check the 2 flag, but if one are to enter the perpendicular line as computed in above paragraph one should remember to have it set if working in 2D so the implicit equation is computed.

Change history:

20070929:2035UTC : Change because of fix in U program.

No comments: