SELECT DRAWN LINE by selecting part of it

user20 161 Reputation points
2021-11-28T06:59:53.187+00:00

I DRWAN A FEW LINES WITH SEGMENT IN VB.NET.
and i can select them by selecting all of that line. but i must select them by selecting every part of that line! TO get index of line.
How can this be done??

I also take a GIF of my work153074-22.gif

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,779 questions
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 33,451 Reputation points Microsoft Vendor
    2021-11-29T06:39:04.837+00:00

    Hi @user20 ,
    I modified your ContainsCompletely method.
    Now you can select a line segment by selecting a part of it.
    Here is my code which you can refer to.

        Public Function ContainsCompletely(r As Rectangle) As Boolean  
            If (r.X > Me.Pt_Start.X AndAlso r.X > Me.Pt_End.X) OrElse (r.Y > Me.Pt_Start.Y AndAlso r.Y > Me.Pt_End.Y) Then  
                Return False  
            ElseIf (r.X + r.Width < Me.m_ptStart.X AndAlso r.X + r.Width < Me.m_ptEnd.X) OrElse (r.Y + r.Height < Me.Pt_Start.Y AndAlso r.Y + r.Height < Me.Pt_End.Y) Then  
                Return False  
            Else  
                Dim a = (r.Y - Me.Pt_Start.Y) * (Me.Pt_End.X - Me.Pt_Start.X) / (Me.Pt_End.Y - Me.Pt_Start.Y) + Me.Pt_Start.X  
                Dim b = (r.Y + r.Height - Me.Pt_Start.Y) * (Me.Pt_End.X - Me.Pt_Start.X) / (Me.Pt_End.Y - Me.Pt_Start.Y) + Me.Pt_Start.X  
                If a < r.X Then  
                    If b > r.X Then  
                        Return True  
                    ElseIf b > r.X + r.Width Then  
                        Return True  
                    Else  
                        Return False  
                    End If  
                ElseIf a > r.X + r.Width Then  
                    If b < r.X Then  
                        Return True  
                    ElseIf b < r.X + r.Width Then  
                        Return True  
                    Else  
                        Return False  
                    End If  
                Else  
                    Return True  
                End If  
            End If  
            Return False  
        End Function  
    

    Hope the code above could be helpful.
    Best Regards.
    Jiachen Li

    ----------

    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. user20 161 Reputation points
    2021-11-28T10:56:17.883+00:00

    my code is :
    Public Class Form1

         Private Segments As List(Of Segment) = New List(Of Segment)()
         Private NewSegment As Segment = Nothing
    
         Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
         End Sub
    
         Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
             NewSegment = New Segment(Pens.Blue, e.Location, e.Location)
             PictureBox1.Refresh()
         End Sub
    
         Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
             e.Graphics.Clear(PictureBox1.BackColor)
             e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
    
             For Each segment As Segment In Segments
                 segment.Draw(e.Graphics)
             Next
    
             If NewSegment IsNot Nothing Then NewSegment.Draw(e.Graphics)
         End Sub
    
         Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
             If NewSegment Is Nothing Then Return
             NewSegment.pt2 = e.Location
             PictureBox1.Refresh()
         End Sub
    
         Private Sub PictureBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseUp
             If NewSegment Is Nothing Then Return
             NewSegment.pen1 = Pens.Black
             Segments.Add(NewSegment)
             NewSegment = Nothing
             PictureBox1.Refresh()
         End Sub
     End Class
    
     Class Segment
         Public pen1 As Pen
         Public pt1, pt2 As Point
    
         Public Sub New(pen As Pen, point1 As Point, point2 As Point)
             pen1 = pen
             pt1 = point1
             pt2 = point2
         End Sub
    
         Public Sub Draw(gr As Graphics)
             gr.DrawLine(pen1, pt1, pt2)
         End Sub
     End Class
    
    0 comments No comments

  2. user20 161 Reputation points
    2021-11-28T11:16:39.13+00:00

    my code is :

    Option Strict On
    Imports System.Drawing.Drawing2D
    
    Public Class Form2
        Private m_Lines As New List(Of line)
        Private m_rnd As New Random
        Private m_Pt As Point
        Private m_Pt2 As Point
        Private m_tracking As Boolean
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            For i As Integer = 0 To 1
    
                m_Lines.Add(New line() With {.Pt_Start = New Point(m_rnd.Next(Me.PictureBox1.ClientSize.Width), m_rnd.Next(Me.PictureBox1.ClientSize.Height)),
                              .Pt_End = New Point(m_rnd.Next(Me.PictureBox1.ClientSize.Width), m_rnd.Next(Me.PictureBox1.ClientSize.Height))})
            Next
        End Sub
    
        Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
            If e.Button = MouseButtons.Left Then
                ResetSelected(Me.m_Lines)
                m_Pt = e.Location
            End If
        End Sub
    
        Private Sub ResetSelected(m_Lines As List(Of line))
            For i As Integer = 0 To m_Lines.Count - 1
                m_Lines(i).Selected = False
            Next
        End Sub
    
        Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
            If e.Button = MouseButtons.Left Then
                m_Pt2 = e.Location
                m_tracking = True
                Me.PictureBox1.Invalidate()
            End If
        End Sub
    
        Private Sub PictureBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseUp
            m_tracking = False
            Me.PictureBox1.Invalidate()
        End Sub
    
        Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
            Dim r As New Rectangle(m_Pt, New Size(m_Pt2.X - m_Pt.X, m_Pt2.Y - m_Pt.Y))
            If m_tracking Then
                e.Graphics.DrawRectangle(Pens.Cyan, r)
            End If
    
            For i As Integer = 0 To m_Lines.Count - 1
                Me.m_Lines(i).Draw(e.Graphics, r)
            Next
        End Sub
    End Class
    
    Public Class line
        Private m_ptStart As Point
        Public Property Pt_Start As Point
            Get
                Return m_ptStart
            End Get
            Set(value As Point)
                m_ptStart = value
            End Set
        End Property
    
        Private m_ptEnd As Point
        Public Property Pt_End As Point
            Get
                Return m_ptEnd
            End Get
            Set(value As Point)
                m_ptEnd = value
            End Set
        End Property
    
        Private m_selected As Boolean
        Public Property Selected As Boolean
            Get
                Return m_selected
            End Get
            Set(value As Boolean)
                m_selected = value
            End Set
        End Property
    
        Public Sub Draw(g As Graphics, r As Rectangle)
            g.SmoothingMode = SmoothingMode.AntiAlias
            If Me.ContainsCompletely(r) OrElse Me.Selected Then
                Me.Selected = True
                g.DrawLine(Pens.Red, Me.Pt_Start, Me.Pt_End)
            Else
                g.DrawLine(Pens.Blue, Me.Pt_Start, Me.Pt_End)
            End If
        End Sub
    
        Public Function ContainsCompletely(r As Rectangle) As Boolean
            If r.Contains(Me.Pt_Start) AndAlso r.Contains(Me.Pt_End) Then
                Return True
            End If
    
            Return False
        End Function
    End Class
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.