1

私は、(とりわけ)ユーザーが長方形の選択ツールを使用して1つまたは複数のオブジェクトを選択できるIDEのような環境を提供するプログラムを作成しています。

すべての選択は単純な長方形になり、選択可能なすべてのオブジェクトも単純な長方形になります。

ラバーバンディング効果を視覚的に作成するためのコード(VB.Net)はすでにあります。必要なのは、最終的な選択長方形内の領域の少なくとも一部を持っているオブジェクトを教えてくれる効率的なアルゴリズムです。

視覚化するのに役立つ場合、私がやりたいことは、Windowsデスクトップのアイコン上に選択ボックスをドラッグすることと同じです...その選択マーキー内にある領域の一部でさえあるアイコンが強調表示(選択)されます。

助けていただければ幸いです...よろしくお願いします

4

2 に答える 2

0
Dim Rect1 As New Rectangle(10, 10, 20, 20)
Dim Rect2 As New Rectangle(5, 5, 20, 20)

Debug.Print(Rect1.IntersectsWith(Rect2))
于 2010-11-10T21:20:47.050 に答える
0

IntersectsWithは、BigFunger が既に述べたように機能します。ただし、長方形に別の長方形が含まれているかどうかを確認する必要があります (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.first チェックに両方向に変更し、2. さらに重要なのは、ある長方形が別の長方形と交差するだけでなく、別の長方形が含まれているかどうかもチェックすることです。

于 2010-11-10T21:39:45.407 に答える