Skip to main content

Overlap of 2 Fields


Question / Problem: 

How do I calculate the overlap of 2 fields?

Answer / Solution: 

The following code calculates the overlap of two fields, words, alternatives, locators, etc. as a number from 0.0 (no overlap) to 1.0 (perfect overlap):

Public Function FieldOverlap(a As Object, b As Object) As Double
   'returns percentage overlap of two fields,subfields, locators, 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)
   FieldOverlap = 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


  • Was this article helpful?