我正在编写一个程序,除其他外,为用户提供类似IDE的环境,他们可以使用rectangualr选择工具选择一个或多个对象。
所有选择都是一个简单的矩形,所有可选对象也都是简单的矩形。
我已经有了代码(VB.Net)来直观地创建橡皮筋效果 - 我需要的是一种有效的算法,它会告诉我哪些对象在最终选择矩形中至少有一部分区域。< / p>
如果它有助于可视化,我想要做的就是将选择框拖到Windows桌面上的图标上...无论哪个图标甚至包含位于该选择框内的部分区域都会突出显示(选中)
任何帮助将不胜感激...提前谢谢
答案 0 :(得分:0)
Dim Rect1 As New Rectangle(10, 10, 20, 20)
Dim Rect2 As New Rectangle(5, 5, 20, 20)
Debug.Print(Rect1.IntersectsWith(Rect2))
答案 1 :(得分:0)
IntersectsWith就像BigFunger已经提到过的那样。但是你应该检查一个矩形contains是否有另一个矩形(intersectsWith只检查交集)。
一个演示它的小样本表:
Public Class SelectionRectangle
Private first As Point
Private allRectangles As New List(Of RectangleF)
Private Sub form_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Me.MouseDown
first = New Point(e.X, e.Y)
End Sub
Private Sub form_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Me.MouseUp
Dim p As New Pen(Brushes.Black, 2)
Dim g As Graphics
Dim second As New Point(e.X, e.Y)
Dim x, y, w, h As Int32
x = DirectCast(IIf(first.X > second.X, second.X, first.X), Int32)
y = DirectCast(IIf(first.Y > second.Y, second.Y, first.Y), Int32)
w = Math.Abs(second.X - first.X)
h = Math.Abs(second.Y - first.Y)
Dim nextRec As New RectangleF(x, y, w, h)
Dim intersects As Boolean = False
For Each rec As RectangleF In allRectangles
If rec.Contains(nextRec) OrElse rec.IntersectsWith(nextRec) Then
intersects = True
Exit For
End If
Next
If Not intersects Then
p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot
g = Me.CreateGraphics()
g.DrawLine(p, first.X, first.Y, second.X, first.Y)
g.DrawLine(p, second.X, second.Y, first.X, second.Y)
g.DrawLine(p, first.X, first.Y, first.X, second.Y)
g.DrawLine(p, second.X, second.Y, second.X, first.Y)
allRectangles.Add(nextRec)
Else
Beep()
End If
End Sub
End Class
UPDATE :将此代码更改为1.首先检查两个方向和2.更重要的是:检查一个矩形是否不仅与另一个矩形相交,而且如果包含< / strong>另一个。