# *** Archive *** Calculate Distance Between Fields

17242
Article # 308692 - Page views: 173

17242
308692

## Question / Problem:

How do I calculate the distance between two fields?

## Answer / Solution:

The following algorithm calculates the distance between two rectangles. This can be used to merge alternatives together.

```Public Function RectangleDistance(a As CscXDocFieldAlternative, b As CscXDocFieldAlternative) 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)
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.3 Scripting