
18-13
18-6. A program to calculate distance between points
A program to calculate distance between points by calculating differences of X, Y and Z of two points respectively, and
then find the distance from the square root of sum of the squares of X, Y and Z.
[ Equation:
(x
1
-x
2
)
2
+(y
1
-y
2
)
2
+(z
1
-z
2
)
2
]
Program (PtoPlen.prg)
The following is a program to calculate distance between points (point-1 and point-2) and then add the result to “GR001”.
The coordinate of the point-1 is assigned to “GD001” and that of the point-2 to “GD002”.
Program
Description
SUB GD001 GD002
Difference between GD001 and GD002.
GETEL LR001 = GD.X#(1:GD001)
Take X element of GD001.
MUL LR001 LR001
Raise the X element to the second power.
GETEL LR002 = GD.Y#(1:GD001)
Take the Y element of GD001.
MUL LR002 LR002
Raise the Y element to the second power.
ADD LR001 LR002
Add the square of the Y element to the square of the X
element.
GETEL LR003 = GD.Z#(1:GD001)
Take the Z element of GD001.
MUL LR003 LR003
Raise the Z element to the second power.
ADD LR001 LR003
Add the square of the Z element to the sum of the square of
the X element and of the Y element.
SQRT LR001 LR001
Calculate the square root of the sum to find the distance.
ADD GR001 LR001
*1)
Add the distance to GR001.
RET
End of the program
●
Application example
TOOL 1:TOOL0001
MOVEL P1
MOVEL P2
CNVSET GD001 P1
CNVSET GD002 P2
CALL PtoPLen.prg
*1)
Please note that in the above sample program, result of the calculation (distance) is added to the GR variable. Since the
maximum value of the GR variable is “99999.99”, in case of calculation distance longer than 100 m, it is necessary to use
the GL variable. (See the following example.)
ADD GR001 LR001
Î
CNVSET LL001 LR001
ADD GL001 LL001