# Distance Between Two Fields

17233
3010796

## Question / Problem:

How Do I Return the Distance Between Two Fields?

The following code measures the distance in pixels between two fields, locators, zones, subfields, etc. The distance is the maximum of the horizontal and vertical distances.

```Public Function RectangleDistance(a As Object, b As Object) As Long

Dim vertDistance As Long
vertDistance = Abs(b.Top + b.Height / 2 - a.Top - a.Height / 2) - b.Height / 2 - a.Height / 2

Dim horDistance As Long
horDistance = Abs(b.Left + b.Width / 2 - a.Left - a.Width / 2) - b.Width / 2 - a.Width / 2

If vertDistance > 0 Or horDistance > 0 Then
RectangleDistance = Max(vertDistance, horDistance)            'Rectangle distance
'RectangleDistance = sqrt(vertDistance ^ 2 + horDistance ^ 2) 'Diagonal distance
Else
RectangleDistance = 0
End If
End Function

Private Function Max(v1 As Long, v2 As Long) As Long
If v1 > v2 Then Max = v1 Else Max = v2
End Function
```

## Applies to:

Product Version Category
KTM 6.3 Scripting
KTM 6.2 Scripting