Rectangle2D 的 intersects 方法就是判断这个的,
代码如下public boolean intersects(Rectangle r) {
int tw = this.width;
int th = this.height;
int rw = r.width;
int rh = r.height;
if (rw <= 0 || rh <= 0 || tw <= 0 || th <= 0) {
return false;
}
int tx = this.x;
int ty = this.y;
int rx = r.x;
int ry = r.y;
rw += rx;
rh += ry;
tw += tx;
th += ty;
// overflow || intersect
return ((rw < rx || rw > tx) &&
(rh < ry || rh > ty) &&
(tw < tx || tw > rx) &&
(th < ty || th > ry));
}
代码如下public boolean intersects(Rectangle r) {
int tw = this.width;
int th = this.height;
int rw = r.width;
int rh = r.height;
if (rw <= 0 || rh <= 0 || tw <= 0 || th <= 0) {
return false;
}
int tx = this.x;
int ty = this.y;
int rx = r.x;
int ry = r.y;
rw += rx;
rh += ry;
tw += tx;
th += ty;
// overflow || intersect
return ((rw < rx || rw > tx) &&
(rh < ry || rh > ty) &&
(tw < tx || tw > rx) &&
(th < ty || th > ry));
}
Private Function PointInLine(X As Single, Y As Single) As Boolean
Dim i As Integer, j As Integer, Rts As RECT ' y -y1 x -x1
'原理:根据直线的“两点式”方程: ──── = ──── 确定点x、y是否落在直线上
' y2-y1 x2-x1
For j = 1 To NumRt
Rts = rt(j).Rects
SwapRect Rts '根据直线位置判断鼠标是否落在直线上,3为监界值,
'只要鼠标在靠近直线在3以内 , 就算鼠标落在直线上
If Abs(rt(j).Rects.Right - rt(j).Rects.Left) <= 10 Then '垂直直线
For i = 1 To 3
If Y > Rts.Top And Y < Rts.Bottom And (X - i = rt(j).Rects.Left Or X + i = rt(j).Rects.Left) Then
PointInLine = True
TmpCurrentNumRt = j
Exit For
End If
Next
ElseIf Abs(rt(j).Rects.Bottom - rt(j).Rects.Top) <= 10 Then '水平直线
For i = 1 To 3
If X > Rts.Left And X < Rts.Right And (Y - i = rt(j).Rects.Top Or Y + i = rt(j).Rects.Bottom) Then
PointInLine = True
TmpCurrentNumRt = j
Exit For
End If
Next
Else '一般直线
For i = 1 To 3
If (Format(Abs((Y - rt(j).Rects.Top) / (rt(j).Rects.Bottom - rt(j).Rects.Top)), ".##") = _
Format(Abs((X - rt(j).Rects.Left) / (rt(j).Rects.Right - rt(j).Rects.Left)), ".##") Or _
Format(Abs((Y - rt(j).Rects.Top) / (rt(j).Rects.Bottom - rt(j).Rects.Top)), ".##") = _
Format(Abs((X - rt(j).Rects.Left - i) / (rt(j).Rects.Right - rt(j).Rects.Left)), ".##") Or _
Format(Abs((Y - rt(j).Rects.Top) / (rt(j).Rects.Bottom - rt(j).Rects.Top)), ".##") = _
Format(Abs((X - rt(j).Rects.Left + i) / (rt(j).Rects.Right - rt(j).Rects.Left)), ".##") Or _
Format(Abs((Y - rt(j).Rects.Top - i) / (rt(j).Rects.Bottom - rt(j).Rects.Top)), ".##") = _
Format(Abs((X - rt(j).Rects.Left) / (rt(j).Rects.Right - rt(j).Rects.Left)), ".##") Or _
Format(Abs((Y - rt(j).Rects.Top + i) / (rt(j).Rects.Bottom - rt(j).Rects.Top)), ".##") = _
Format(Abs((X - rt(j).Rects.Left) / (rt(j).Rects.Right - rt(j).Rects.Left)), ".##")) And _
PointInRect(X, Y, rt(j).Rects) Then
PointInLine = True
TmpCurrentNumRt = j
Exit For
End If
Next
End If
Next
End Function