none
please help curve RRS feed

  • Question

  • how use cruve in code 

    Public Class win
    
        Dim down = False
        Dim ptA As Point
        Dim ptB As Point
        Dim ptC As Point
        Dim ptStart As New List(Of Point)
        Dim ptEnd As New List(Of Point)
         
    
        Private Sub Win_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
            down = True
            ptA = New Point(e.X, e.Y)
        End Sub
    
        Private Sub Win_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
            If down = True Then
                ptB = New Point(e.X, e.Y)
                Invalidate()
            End If
        End Sub
    
        Private Sub Win_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
            down = False
            ptC = New Point(e.X, e.Y)
            ptStart.Add(ptA)
            ptEnd.Add(ptC)
    
        End Sub
    
        Private Sub Win_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
            e.Graphics.Clear(Color.Black)
            e.Graphics.DrawLine(Pens.LawnGreen, ptA, ptB)
            For i = 0 To ptStart.Count - 1
                e.Graphics.DrawLine(Pens.LawnGreen, ptStart(i), ptEnd(i))
            Next
        End Sub
    
    
        Private Sub win_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.DoubleBuffered = True
        End Sub
    End Class

    I want to control the 6 lines from one place like this

    Monday, January 29, 2018 11:11 PM

Answers

  • Is this what you mean?

    Public Class Form8
        Private Bezes As New List(Of Bez)   'list of bez obect shapes using bez class
        Private mdState As Integer = -1
        Private mdButton As Integer
        Private mdPt As PointF
        Private mdBez As New Bez(0, 0, 1)
        Private SelectedBezIndex As Integer = -1
        Private SelectedBezPt As Integer = -1
        Private HandleSize As Single = 5
    
        Private Sub Form8_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ClientSize = New Size(300, 300)
            DoubleBuffered = True
    
            'create  bez shapes
            Bezes.Add(New Bez(100, 100, 50))
            Bezes.Add(New Bez(200, 200, 50))
        End Sub
    
        Private Sub Form8_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
            With e.Graphics
                .Clear(Color.Black)
                .SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
                For Each bz As Bez In Bezes
                    bz.drawBez(e.Graphics)
                Next
            End With
        End Sub
    
        Private Sub Form8_MouseDown(sender As Object, e As MouseEventArgs) Handles Me.MouseDown
            If e.Button = MouseButtons.Left Then mdButton = 0 Else mdButton = 1
            mdPt = e.Location
    
            If ModifierKeys = Keys.Shift Then mdButton = 1 Else mdButton = 0
    
            'get the selected object
            GetMouseOverPoint(e.Location, SelectedBezIndex, SelectedBezPt)
            mdState = SelectedBezPt
    
            If SelectedBezIndex > -1 Then
                'if object is selected save unreferenced copy
                mdBez.Pts.Clear()
                For i As Integer = 0 To 6
                    mdBez.Pts.Add(New PointF(Bezes(SelectedBezIndex).Pts(i).X,
                                              Bezes(SelectedBezIndex).Pts(i).Y))
                Next
                mdBez.HandleSize = Bezes(SelectedBezIndex).HandleSize
            End If
        End Sub
    
        Private Sub Form8_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
            If mdState > -1 Then
                'an object is selected in mouse down
                'set dragged bez point to mouse location
                If mdButton = 1 And SelectedBezPt = 0 Then
                    'right button center point
                    'move selected point only
                    Bezes(SelectedBezIndex).Pts(mdState) = e.Location
                Else
                    If mdButton = 0 Then
                        'left button
                        If SelectedBezPt = 0 Then
                            'center point
                            'move all points by change in mouse location
                            Dim dx, dy As Single
                            dx = e.X - mdPt.X
                            dy = e.Y - mdPt.Y
    
                            For i As Integer = 0 To 6
                                Bezes(SelectedBezIndex).Pts(i) =
                                    New PointF(mdBez.Pts(i).X + dx,
                                    mdBez.Pts(i).Y + dy)
                            Next
                        Else
                            'move selected point only
                            Bezes(SelectedBezIndex).Pts(mdState) = e.Location
                        End If
                    End If
                End If
    
                Invalidate()
            Else
                Dim thisBezIndex, thisBezPt As Integer
                GetMouseOverPoint(e.Location, thisBezIndex, thisBezPt)
    
                If thisBezIndex > -1 Then
                    Select Case thisBezPt
                        Case 0  'center point
                            If ModifierKeys = Keys.Shift Then
                                Cursor = Cursors.Cross
                            Else
                                Cursor = Cursors.Hand
                            End If
                        Case Else
                            Cursor = Cursors.SizeAll
                    End Select
                Else
                    Cursor = Cursors.Default
                End If
            End If
        End Sub
    
        Private Sub Form8_MouseUp(sender As Object, e As MouseEventArgs) Handles Me.MouseUp
            mdState = -1
            SelectedBezIndex = -1
            Cursor = Cursors.Default
        End Sub
    
        Private Sub GetMouseOverPoint(ByVal pt As PointF, ByRef thisBezIndex As Integer, ByRef thisBezPt As Integer)
            Dim selectedpt As Integer
            thisBezIndex = -1
            thisBezPt = -1
    
            For i As Integer = 0 To Bezes.Count - 1
                selectedpt = Bezes(i).Contains(pt)
                If selectedpt > -1 Then
                    thisBezIndex = i
                    thisBezPt = selectedpt
                End If
            Next
        End Sub
    End Class
    
    Public Class Bez
        'shape consiting of 6 lines you can move with the mouse
        Public Pts As New List(Of PointF)
        Public HandleSize As Single = 5
    
        Public Sub New(rx As Single, ry As Single, r As Single)
            Pts.Clear()
            Pts.Add(New PointF(rx, ry))             'center point
            Pts.Add(New PointF(rx - r, ry - r))     'define the 6 points 
            Pts.Add(New PointF(rx, ry - r))         'from upperleft clockwise
            Pts.Add(New PointF(rx + r, ry - r))
            Pts.Add(New PointF(rx + r, ry + r))
            Pts.Add(New PointF(rx, ry + r))
            Pts.Add(New PointF(rx - r, ry + r))
        End Sub
    
        Public Sub drawBez(g As Graphics)
            For Each pt As PointF In Pts
                g.FillEllipse(Brushes.Lime, New RectangleF(pt.X - HandleSize, pt.Y - HandleSize,
                                                           2 * HandleSize, 2 * HandleSize))
                g.DrawLine(Pens.Red, Pts(0), pt)
            Next
        End Sub
    
        Public Function Contains(e As PointF) As Integer
            Dim pointIndex As Integer = -1
            Dim dx, dy, r As Single
    
            For i As Integer = 0 To 6
                'distance from click point to bez points
                dx = e.X - Pts(i).X
                dy = e.Y - Pts(i).Y
                r = CSng(Math.Sqrt((dx * dx) + (dy * dy)))
    
                'if clicked within tolerance select this point
                If r < HandleSize Then
                    pointIndex = i
                    Exit For
                End If
            Next
    
            Return pointIndex
    
        End Function
    End Class
    

    Tuesday, January 30, 2018 4:23 AM

All replies

  • how use cruve in code

    It's not clear what you mean by 'control'.  If you want to update all the start points or all the end points to a new point then you need to iterate over ptStart or ptEnd respectively, setting all entries to the new point. Then invalidate the form.   The problem will be that the mouse down event is currently used to set a new start point, so you will need to decide how you distinguish between adding new lines and 'controlling' the existing lines.  Perhaps you could test for left or right mouse button.  

    Monday, January 29, 2018 11:20 PM
  • how use cruve in code

    It's not clear what you mean by 'control'.  If you want to update all the start points or all the end points to a new point then you need to iterate over ptStart or ptEnd respectively, setting all entries to the new point. Then invalidate the form.   The problem will be that the mouse down event is currently used to set a new start point, so you will need to decide how you distinguish between adding new lines and 'controlling' the existing lines.  Perhaps you could test for left or right mouse button.  

    I am waiting for a two year solution and no one tells me I want an example of three lines and control of the middle

    Monday, January 29, 2018 11:43 PM
  • I am waiting for a two year solution and no one tells me I want an example of three lines and control of the middle

    If you  want assistance with your application you need to describe what you are trying to do. 

    What do you mean by 'control'?  Do you mean that you want to be able to click on the line, and move the point that defines the line?  You would do that by changing the relevant entry in the psStart or ptEnd array, and invalidating the form.  The line will then be drawn with the new start or end point.   You now mention 'middle'.  So perhaps you mean that you want to click on the line, have the mid-point of the line calculated, and move that mid-point.  To do that you would:
      Get the start and end points for the line that was selected
      Calculate the mid point
      Delete the original line
      Create two new lines using the original start and end points and the new mid point
      Invalidate the form

    Or perhaps it's something else.  In any case, you need to answer the first question above - how are you going to distinguish between a mouse button press to start a new line, and a mouse button press to 'control' the middle?  You could:
      Count the number of lines.  If there are already three lines then the next mouse button press must be a 'control' press.  Or
      Use the left mouse button to add new lines, and use the right mouse button to 'control' the lines.  Or
      Add a checkbox to the form to allow the user to select whether they are adding new lines or controlling existing lines.

    Without a proper description of what you want to do and how you want to do it then it is not possible to offer sugestions for a solution.

    Tuesday, January 30, 2018 12:10 AM
  • I am waiting for a two year solution and no one tells me I want an example of three lines and control of the middle

    If you  want assistance with your application you need to describe what you are trying to do. 

    What do you mean by 'control'?  Do you mean that you want to be able to click on the line, and move the point that defines the line?  You would do that by changing the relevant entry in the psStart or ptEnd array, and invalidating the form.  The line will then be drawn with the new start or end point.   You now mention 'middle'.  So perhaps you mean that you want to click on the line, have the mid-point of the line calculated, and move that mid-point.  To do that you would:
      Get the start and end points for the line that was selected
      Calculate the mid point
      Delete the original line
      Create two new lines using the original start and end points and the new mid point
      Invalidate the form

    Or perhaps it's something else.  In any case, you need to answer the first question above - how are you going to distinguish between a mouse button press to start a new line, and a mouse button press to 'control' the middle?  You could:
      Count the number of lines.  If there are already three lines then the next mouse button press must be a 'control' press.  Or
      Use the left mouse button to add new lines, and use the right mouse button to 'control' the lines.  Or
      Add a checkbox to the form to allow the user to select whether they are adding new lines or controlling existing lines.

    Without a proper description of what you want to do and how you want to do it then it is not possible to offer sugestions for a solution.

    Please give me an example code

    Tuesday, January 30, 2018 12:37 AM
  • Is this what you mean?

    Public Class Form8
        Private Bezes As New List(Of Bez)   'list of bez obect shapes using bez class
        Private mdState As Integer = -1
        Private mdButton As Integer
        Private mdPt As PointF
        Private mdBez As New Bez(0, 0, 1)
        Private SelectedBezIndex As Integer = -1
        Private SelectedBezPt As Integer = -1
        Private HandleSize As Single = 5
    
        Private Sub Form8_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ClientSize = New Size(300, 300)
            DoubleBuffered = True
    
            'create  bez shapes
            Bezes.Add(New Bez(100, 100, 50))
            Bezes.Add(New Bez(200, 200, 50))
        End Sub
    
        Private Sub Form8_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
            With e.Graphics
                .Clear(Color.Black)
                .SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
                For Each bz As Bez In Bezes
                    bz.drawBez(e.Graphics)
                Next
            End With
        End Sub
    
        Private Sub Form8_MouseDown(sender As Object, e As MouseEventArgs) Handles Me.MouseDown
            If e.Button = MouseButtons.Left Then mdButton = 0 Else mdButton = 1
            mdPt = e.Location
    
            If ModifierKeys = Keys.Shift Then mdButton = 1 Else mdButton = 0
    
            'get the selected object
            GetMouseOverPoint(e.Location, SelectedBezIndex, SelectedBezPt)
            mdState = SelectedBezPt
    
            If SelectedBezIndex > -1 Then
                'if object is selected save unreferenced copy
                mdBez.Pts.Clear()
                For i As Integer = 0 To 6
                    mdBez.Pts.Add(New PointF(Bezes(SelectedBezIndex).Pts(i).X,
                                              Bezes(SelectedBezIndex).Pts(i).Y))
                Next
                mdBez.HandleSize = Bezes(SelectedBezIndex).HandleSize
            End If
        End Sub
    
        Private Sub Form8_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
            If mdState > -1 Then
                'an object is selected in mouse down
                'set dragged bez point to mouse location
                If mdButton = 1 And SelectedBezPt = 0 Then
                    'right button center point
                    'move selected point only
                    Bezes(SelectedBezIndex).Pts(mdState) = e.Location
                Else
                    If mdButton = 0 Then
                        'left button
                        If SelectedBezPt = 0 Then
                            'center point
                            'move all points by change in mouse location
                            Dim dx, dy As Single
                            dx = e.X - mdPt.X
                            dy = e.Y - mdPt.Y
    
                            For i As Integer = 0 To 6
                                Bezes(SelectedBezIndex).Pts(i) =
                                    New PointF(mdBez.Pts(i).X + dx,
                                    mdBez.Pts(i).Y + dy)
                            Next
                        Else
                            'move selected point only
                            Bezes(SelectedBezIndex).Pts(mdState) = e.Location
                        End If
                    End If
                End If
    
                Invalidate()
            Else
                Dim thisBezIndex, thisBezPt As Integer
                GetMouseOverPoint(e.Location, thisBezIndex, thisBezPt)
    
                If thisBezIndex > -1 Then
                    Select Case thisBezPt
                        Case 0  'center point
                            If ModifierKeys = Keys.Shift Then
                                Cursor = Cursors.Cross
                            Else
                                Cursor = Cursors.Hand
                            End If
                        Case Else
                            Cursor = Cursors.SizeAll
                    End Select
                Else
                    Cursor = Cursors.Default
                End If
            End If
        End Sub
    
        Private Sub Form8_MouseUp(sender As Object, e As MouseEventArgs) Handles Me.MouseUp
            mdState = -1
            SelectedBezIndex = -1
            Cursor = Cursors.Default
        End Sub
    
        Private Sub GetMouseOverPoint(ByVal pt As PointF, ByRef thisBezIndex As Integer, ByRef thisBezPt As Integer)
            Dim selectedpt As Integer
            thisBezIndex = -1
            thisBezPt = -1
    
            For i As Integer = 0 To Bezes.Count - 1
                selectedpt = Bezes(i).Contains(pt)
                If selectedpt > -1 Then
                    thisBezIndex = i
                    thisBezPt = selectedpt
                End If
            Next
        End Sub
    End Class
    
    Public Class Bez
        'shape consiting of 6 lines you can move with the mouse
        Public Pts As New List(Of PointF)
        Public HandleSize As Single = 5
    
        Public Sub New(rx As Single, ry As Single, r As Single)
            Pts.Clear()
            Pts.Add(New PointF(rx, ry))             'center point
            Pts.Add(New PointF(rx - r, ry - r))     'define the 6 points 
            Pts.Add(New PointF(rx, ry - r))         'from upperleft clockwise
            Pts.Add(New PointF(rx + r, ry - r))
            Pts.Add(New PointF(rx + r, ry + r))
            Pts.Add(New PointF(rx, ry + r))
            Pts.Add(New PointF(rx - r, ry + r))
        End Sub
    
        Public Sub drawBez(g As Graphics)
            For Each pt As PointF In Pts
                g.FillEllipse(Brushes.Lime, New RectangleF(pt.X - HandleSize, pt.Y - HandleSize,
                                                           2 * HandleSize, 2 * HandleSize))
                g.DrawLine(Pens.Red, Pts(0), pt)
            Next
        End Sub
    
        Public Function Contains(e As PointF) As Integer
            Dim pointIndex As Integer = -1
            Dim dx, dy, r As Single
    
            For i As Integer = 0 To 6
                'distance from click point to bez points
                dx = e.X - Pts(i).X
                dy = e.Y - Pts(i).Y
                r = CSng(Math.Sqrt((dx * dx) + (dy * dy)))
    
                'if clicked within tolerance select this point
                If r < HandleSize Then
                    pointIndex = i
                    Exit For
                End If
            Next
    
            Return pointIndex
    
        End Function
    End Class
    

    Tuesday, January 30, 2018 4:23 AM