# Calculate Overlap Between Fields

17241

## Question / Problem:

How do I calculate the overlap between fields?

## Answer / Solution:

The functions below calculate the 2 dimensional and horizontal overlap of two rectangles as a value from 0 to 1:

Public Function FieldOverlapHoriz(a As Object, b As Object) As Double If a.PageIndex <> b.PageIndex Or a.PageIndex = -1 Then Exit Function Dim overlap As Double overlap = Max((Min(a.Left + a.Width, b.Left + b.Width) - Max(a.Left, b.Left)), 0) FieldOverlapHoriz = overlap / Min(a.Width, b.Width) End Function Public Function FieldOverlap2D(a As Object, b As Object) As Double 'returns percentage overlap of two fields, subfields or ' alternatives (0.0 if no overlap, 1.0 if perfect overlap) FieldOverlap = 0 'Check if fields are on the same page and that both exist If a.PageIndex <> b.PageIndex Or a.PageIndex = -1 Then Exit Function Dim overlapArea As Double overlapArea = Max((Min(a.Left + a.Width, b.Left + b.Width) - Max(a.Left, b.Left)), 0) _ * Max((Min(a.Top + a.Height, b.Top + b.Height) - Max(a.Top, b.Top)), 0) FieldOverlap2D = overlapArea / Max(a.Width * a.Height, b.Width * b.Height) End Function Private Function Max(v1 As Long, v2 As Long) As Long If v1 > v2 Then Max = v1 Else Max = v2 End Function Private Function Min(v1 As Long, v2 As Long) As Long If v1 < v2 Then Min = v1 Else Min = v2 End Function

## Applies to:

Product |
Version |
Category |
---|---|---|

KTM | 6.3 | Scripting |

KTM | 6.2 | Scripting |