Let's consider the example in the drawing above. The source point is (x1, y1) = (2, 3) and the destination point is (x2, y2) = (3, 5). C1 and C2 in the y-axis are the linear coefficients of the straight lines y = -x + C1 and y = -x + C2, which contain these points. From the drawing,
C1 = x1 + y1; C1 = 2 + 3; C1 = 5
C2 = x2 + y2; C2 = 4 + 5; C2 = 9
The number of steps marked in orange is
n1 = C2 - C1; n1 = 9 - 5; n1 = 4
The number of steps marked in blue, from (x
1, y
1) to C
1, is
n2 = x1; n2 = 2
The number of steps marked in green, from the x-axis to (x
2, y
2), is
n3 = y2; n3 = 5
The number of the remaining steps, marked in red, is
C2-1
----
n4 = \ k ; n4 = (C22 - C2 - C12 - C1)/2; n4 = (92 - 9 - 52 - 5)/2; n4 = 21
/
----
k=C1+1
Finally, the total number of steps is
n = n1 + n2 + n3 + n4; n = 4 + 2 + 5 + 21; n= 32
or, generically,
n = C2 - C1 + x1 + y2 + (C22 - C2 - C12 - C1)/2
which can be simplified to
n = ((x2 + y2)2 + (x2 + y2) - ((x1 + y1)2 + (x1 + y1)))/2 + y2 - y1
The symetry of the latter allows for this HP 50g 55-byte program:
PGM1:
« ROT DUP2 - 5 ROLLD
1 2
START
UNROT + DUP SQ +
NEXT
- 2 / +
»
Example:
2 ENTER 3 ENTER 4 ENTER 5 PGM1 --> 32
This doesn't mean, however, smaller RPL programs are not possible.
Gerson.
Edited: 25 Sept 2012, 9:17 p.m.