HP 15C: Distance and Slope Between Two Points Using Polar Conversion and the Stack
HP 15C Program: Distance and Slope
This short program calculates the slope and distance between two Cartesian points (x1, y1) and (x2, y2) using the four level stack and rectangular-polar conversion. The code can be adopted to other Hewlett Packard, Swiss Micros, and other RPN with four-stacks. RPL will need a short adjustment.
Input Stack:
T: y2
Z: x2
Y: y1
X: x1
Code:
LBL A |
001 |
42, 21, 11 |
Program start |
X<>Y |
002 |
34 |
|
R↓ |
003 |
33 |
|
- |
004 |
30 |
|
R↓ |
005 |
33 |
|
- |
006 |
30 |
|
CHS |
007 |
16 |
|
R↑ |
008 |
43, 33 |
Y: Δy, X: Δx |
→P |
009 |
43, 1 |
Rectangular to polar conversion; calculate distance |
X<>Y |
010 |
34 |
|
TAN |
011 |
25 |
Calculate slope |
X<>Y |
012 |
34 |
|
RTN |
013 |
43, 32 |
Program end |
|
|
|
|
Reference formulas
Distance = √((x2^2 – x1^2) + (y2^2 – y1^2))
Slope = (y2 – y1) ÷ (x2 – x1) = tan(Θ)
Derivation:
Let y’ = y2 – y1 and x’ = x2 – x1
Then by rectangular to polar function, angle:
Θ = arctan( y’ / x’ )
tan Θ = y’ / x’
tan Θ = (y2 – y1) ÷ (x2 – x1)
Examples
Example 1: (-3, 8) to (11, 16)
Stack:
T: 16
Z: 11
Y: 8
X: -3
Result:
Y: slope ≈ 0.5714
X: distance ≈ 16.1245
Example 2: (5, 6) to (7, 9)
Stack:
T: 9
Z: 7
Y: 6
X: 5
Result:
Y: slope = 1.5000
X: distance ≈ 3.6056
Eddie
All original content copyright, © 2011-2026. Edward Shore. Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited. This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.