这是计算父项非重叠区域的另一种方法:
Function max(ByVal v1 As Double, ByVal v2 As Double) As Double
If v1 > v2 Then
Return v1
Else
Return v2
End If
End Function
Function min(ByVal v1 As Double, ByVal v2 As Double) As Double
If v1 > v2 Then
Return v2
Else
Return v1
End If
End Function
Function IntervalOverLap(ByVal p1 As Double, ByVal p2 As Double, ByVal c1 As Double, ByVal c2 As Double) As Double
'determine how much of the parent(p1 to p2) segment '
' is overlapped by the child(c1 to c2) segment '
'sort to standard direction '
Dim ph As Double = max(p1, p2)
Dim pl As Double = min(p1, p2)
Dim ch As Double = max(c1, c2)
Dim cl As Double = min(c1, c2)
'restrict the child to within the parent '
ch = min(ph, max(pl, ch))
cl = max(pl, min(cl, ph))
'return the overlapped length '
Return (ch - cl)
End Function
Function NonOverLappedArea(ByVal parent As Rectangle, ByVal child As Rectangle) As Double
'return the area of the parent '
' that is not overlapped by the child '
Return IntervalOverLap(parent.X1, parent.X2, child.X1, child.X2) _
* IntervalOverLap(parent.Y1, parent.Y2, child.Y1, child.Y2)
End Function