Siemens Industry Online Support
Siemens AG
Entry type: Application example Entry ID: 19345644, Entry date: 08/23/2004

Calculation of the Distance of Two Point of the Cartesian Coordinate System

  • Entry
  • Associated product(s)

How can I calculate the distance "D" between two points in a rectangular system of coordinates with STEP 7?

Enclosed sample program implements the calculation of distance "D" between two points in a rectangular system of coordinates. The calculated distances (return value "RET_VAL") are saved one after the other in a data block. The program in FC10 consists of two parts:

  • Part 1:
    For the calculation of distances between both points FC10 expects coordinates X1 and Y1 for point 1 and coordinates X2 and Y2 for point 2 of input parameters. Output parameter RET_VAL returns the calculated distance between two points. The input tags X1, X2, Y1 and Y2 as well as output tag RET_VAL are of type REAL.

Fig. 1: Calculation of Distance "D" between Point 1 and Point 2

  • Part 2:
    Here the values of RET_VAL (calculated distances) are saved in the data block DB10. DB10 has 10 addresses (DB10.DBD0, DB10.DBD4, ...). Every address is reserved for one RET_VAL. If the address already is filled with a RET_VAL, the new (modified) Ret_Val is entered in the next address. In FC10 the query is run first as to which address of DB10 is free next. When it is found, the new current RET_VAL is written in this address (except for value 0) and this program is finished. 
    The entered value RET_VAL is not overwritten, and the next new, changing value RET_VAL is then written into the next address. If all 10 addresses of DB10 are occupied, in FC10 a reset must be done. All addresses in DB10 are then set to zero.

FC10 is called in OB1 and parametrized as follows:

Fig. 2: Calling FC10 in OB1

The enclosed download contains the STEP 7 Project with the above mentioned block to calculate the distance "D" between two points in a rectangular system of coordinates.


Copy file "Distance_Coor.exe" into a separate directory and then start the file per double click. The STEP 7 V5.2 project is now automatically unpacked with all related sub directories. Subsequently you can open and edit the unpacked project with the SIMATIC Manager.

Runtime Ability and Test Environment:
The example can be used on all CPU Variants of the SIMATIC S7-300, S7-400 , C7 and WinAC.
In the following table the components are listed, with which this entry was created and the feature described above was verified: 

Test Environment               Version                                                        
PC System PG 740 Pentium III
PC Operating System Microsoft Windows 2000 SP3
STEP 7 STEP 7 V5.2 SP1
Options Package S7-PLCSIM V5.2

Key Words:

Security information
In order to protect technical infrastructures, systems, machines and networks against cyber threats, it is necessary to implement – and continuously maintain – a holistic, state-of-the-art IT security concept. Siemens’ products and solutions constitute one element of such a concept. For more information about cyber security, please visit
Rate entry
no rating
Requests and feedback
What do you want to do?
Note: The feedback always relates to the current entry / product. Your message will be forwarded to our technical editors working in the Online Support. In a few days, you will receive a response if your feedback requires one. If we have no further questions, you will not hear from us.