Results 1 to 14 of 14

Thread: make a mouse path from a to b non-linear

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2011
    Location
    Belgium
    Posts
    22

    make a mouse path from a to b non-linear

    i think the title says all what needs to be said, so i wanna make my mouse follow a random path from A to B, without straight lines..
    ask when you need more information.

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: make a mouse path from a to b non-linear

    Try this:

    vb.net Code:
    1. Dim g As Graphics
    2.  
    3. Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    4.     g = Graphics.FromHwnd(Me.Handle)
    5. End Sub
    6.  
    7. Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
    8.     Static lastPosition As Point = e.Location
    9.     g.DrawLine(Pens.Red, lastPosition, e.Location)
    10.     lastPosition = e.Location
    11. End Sub

    This is just the basic code. In addition to this you might want to trap the MouseDown and MouseUp events to determine when to start drawing and when to stop.

     
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2011
    Location
    Belgium
    Posts
    22

    Re: make a mouse path from a to b non-linear

    =s this is not what i mean.. i already have a program that clicks on point A , then on point B,.. but the path between those 2 points may not be a straight line like.. now the mouse is going straight from A to B,.. but i wanna let it follow something like a random "curve"..

  4. #4
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: make a mouse path from a to b non-linear

    Do you have the path (array of points) you want your mouse to follow?

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: make a mouse path from a to b non-linear

    See http://msdn.microsoft.com/en-us/libr...=VS.80%29.aspx.

    Changing the control points should give you what you want.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jan 2011
    Location
    Belgium
    Posts
    22

    Re: make a mouse path from a to b non-linear

    it doesn't matters what path it follows, aslong as it isn't a straight path.

    ( i wrote a program that clicks on a certain point, then wait and then clicks on another point. but the path between those points is a straight path, i wanna make a random, curved path between these points)

  7. #7
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: make a mouse path from a to b non-linear

    It all depends on wahtever logic you want to trace the path.

    Try this. It is the Horizontal version. I hope you would be adapt it to your needs.
    I just showed the creation of points and plotting them separately to explain thing clearly. You can easily modify this code to remove the array and instead plot the points directly.
    vb Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.     Const Pitch As Integer = 5
    3.     Const MaxDeviation As Integer = 100
    4.     Dim random As New Random
    5.     Dim PointsToTrace() As Point
    6.     ReDim PointsToTrace(100000)
    7.  
    8.     PointsToTrace(0) = New Point(100, 500)                          'start point
    9.     PointsToTrace(PointsToTrace.Length - 1) = New Point(1000, 500)  'end point
    10.  
    11.     '' create points
    12.     For i As Integer = 1 To PointsToTrace.Length - 2
    13.         PointsToTrace(i) = PointsToTrace(i - 1) + New Point(random.Next(0, Pitch), random.Next(-1 * Pitch, Pitch))
    14.         If PointsToTrace(i).Y > PointsToTrace(0).Y + MaxDeviation Then
    15.             PointsToTrace(i).Y -= Pitch
    16.         ElseIf PointsToTrace(i).Y < PointsToTrace(0).Y - MaxDeviation Then
    17.             PointsToTrace(i).Y += Pitch
    18.         End If
    19.         If PointsToTrace(i).X > PointsToTrace(PointsToTrace.Length - 1).X Then
    20.             PointsToTrace(i + 1) = PointsToTrace(PointsToTrace.Length - 1)
    21.             ReDim Preserve PointsToTrace(i + 1)
    22.             Exit For
    23.         End If
    24.     Next
    25.  
    26.     '' plot the points
    27.     Dim g As Graphics
    28.     g = Graphics.FromHwnd(Me.Handle)
    29.     g.DrawRectangle(Pens.Blue, New Rectangle(PointsToTrace(0), New Size(5, 5)))
    30.     g.DrawRectangle(Pens.Blue, New Rectangle(PointsToTrace(PointsToTrace.Length - 1), New Size(5, 5)))
    31.     For i As Integer = 1 To PointsToTrace.Length - 1
    32.         g.DrawLine(Pens.Red, PointsToTrace(i - 1), PointsToTrace(i))
    33.     Next
    34. End Sub
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jan 2011
    Location
    Belgium
    Posts
    22

    Re: make a mouse path from a to b non-linear

    okj
    is there any possibility to let the cursor follow this path?

  9. #9
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: make a mouse path from a to b non-linear

    vb.net Code:
    1. Dim PointsToTrace() As Point
    2. Dim g As Graphics
    3.  
    4. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    5.     Const Pitch As Integer = 5
    6.     Const MaxDeviation As Integer = 100
    7.     Dim random As New Random
    8.     ReDim PointsToTrace(100000)
    9.  
    10.     PointsToTrace(0) = New Point(100, 500)                          'start point
    11.     PointsToTrace(PointsToTrace.Length - 1) = New Point(1000, 500)  'end point
    12.  
    13.     For i As Integer = 1 To PointsToTrace.Length - 2
    14.         PointsToTrace(i) = PointsToTrace(i - 1) + New Point(random.Next(0, Pitch), random.Next(-1 * Pitch, Pitch))
    15.         If PointsToTrace(i).Y > PointsToTrace(0).Y + MaxDeviation Then
    16.             PointsToTrace(i).Y -= Pitch
    17.         ElseIf PointsToTrace(i).Y < PointsToTrace(0).Y - MaxDeviation Then
    18.             PointsToTrace(i).Y += Pitch
    19.         End If
    20.         If PointsToTrace(i).X > PointsToTrace(PointsToTrace.Length - 1).X Then
    21.             PointsToTrace(i + 1) = PointsToTrace(PointsToTrace.Length - 1)
    22.             ReDim Preserve PointsToTrace(i + 1)
    23.             Exit For
    24.         End If
    25.     Next
    26.  
    27.     g = Graphics.FromHwnd(Me.Handle)
    28.     g.DrawRectangle(Pens.Blue, New Rectangle(PointsToTrace(0), New Size(5, 5)))
    29.     g.DrawRectangle(Pens.Blue, New Rectangle(PointsToTrace(PointsToTrace.Length - 1), New Size(5, 5)))
    30.     Timer1.Interval = 100
    31.     Timer1.Start()
    32. End Sub
    33.  
    34. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    35.     Static index As Integer = 1
    36.     Cursor.Current.Position = Me.PointToScreen(PointsToTrace(index))
    37.     g.DrawLine(Pens.Red, PointsToTrace(index - 1), PointsToTrace(index))
    38.     index += 1
    39.     If index >= PointsToTrace.Length Then Timer1.Stop()
    40. End Sub
    41.  
    42. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    43.     Timer1.Stop()
    44. End Sub
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Jan 2011
    Location
    Belgium
    Posts
    22

    Re: make a mouse path from a to b non-linear

    but, there is still a problem =S

    the mouse follows a path, from little straight lines, it needs to be a smooth curve from point A to B
    (point a and b are inputs)

  11. #11
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: make a mouse path from a to b non-linear

    Quote Originally Posted by dbasnett View Post
    See http://msdn.microsoft.com/en-us/libr...=VS.80%29.aspx.

    Changing the control points should give you what you want.
    Did you look at this at all?
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Jan 2011
    Location
    Belgium
    Posts
    22

    Re: make a mouse path from a to b non-linear

    yeah, but this just draws a curve.. this curve is great, but i wanna let the cursor follow that curve,..

  13. #13
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: make a mouse path from a to b non-linear

    vb.net Code:
    1. Dim PointsToTrace() As Point
    2. Dim g As Graphics
    3.  
    4. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    5.     Const Pitch As Integer = 10
    6.     Const MaxDeviation As Integer = 50
    7.     Dim random As New Random
    8.     ReDim PointsToTrace(100000)
    9.  
    10.     PointsToTrace(0) = New Point(100, 500)                          'start point
    11.     PointsToTrace(PointsToTrace.Length - 1) = New Point(1000, 500)  'end point
    12.  
    13.     For i As Integer = 1 To PointsToTrace.Length - 2
    14.         PointsToTrace(i) = PointsToTrace(i - 1) + New Point(random.Next(0, Pitch), random.Next(-1 * Pitch, Pitch))
    15.         If PointsToTrace(i).Y > PointsToTrace(0).Y + MaxDeviation Then
    16.             PointsToTrace(i).Y -= Pitch
    17.         ElseIf PointsToTrace(i).Y < PointsToTrace(0).Y - MaxDeviation Then
    18.             PointsToTrace(i).Y += Pitch
    19.         End If
    20.         If PointsToTrace(i).X > PointsToTrace(PointsToTrace.Length - 1).X Then
    21.             PointsToTrace(i + 1) = PointsToTrace(PointsToTrace.Length - 1)
    22.             ReDim Preserve PointsToTrace(i + 1)
    23.             Exit For
    24.         End If
    25.     Next
    26.  
    27.     '' smoothen the points before plotting them.
    28.     '' each additional time you call this function, it will smoothen the points further.
    29.     SmothenCurve(PointsToTrace)
    30.     SmothenCurve(PointsToTrace)
    31.  
    32.     g = Graphics.FromHwnd(Me.Handle)
    33.     g.DrawRectangle(Pens.Blue, New Rectangle(PointsToTrace(0), New Size(5, 5)))
    34.     g.DrawRectangle(Pens.Blue, New Rectangle(PointsToTrace(PointsToTrace.Length - 1), New Size(5, 5)))
    35.     Timer1.Interval = 100
    36.     Timer1.Start()
    37. End Sub
    38.  
    39. Private Sub SmothenCurve(ByVal points() As Point)
    40.     For i As Integer = 1 To PointsToTrace.Length - 2 Step 2
    41.         points(i).X = (points(i - 1).X + points(i + 1).X) \ 2
    42.         points(i).Y = (points(i - 1).Y + points(i + 1).Y) \ 2
    43.     Next
    44.     For i As Integer = 2 To PointsToTrace.Length - 2 Step 2
    45.         points(i).X = (points(i - 1).X + points(i + 1).X) \ 2
    46.         points(i).Y = (points(i - 1).Y + points(i + 1).Y) \ 2
    47.     Next
    48. End Sub
    49.  
    50. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    51.     Static index As Integer = 1
    52.     Cursor.Current.Position = Me.PointToScreen(PointsToTrace(index))
    53.     g.DrawLine(Pens.Red, PointsToTrace(index - 1), PointsToTrace(index))
    54.     index += 1
    55.     If index >= PointsToTrace.Length Then index = 1 : Timer1.Stop()
    56. End Sub

    I added a function SmothenCurve() to smoothen the curve and remove the sharp edges. Each additional time you call it, it will smoothen the curve more.
    Also notice that setting a bigger Pitch value will result in more smooth curve than with lower values.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Jan 2011
    Location
    Belgium
    Posts
    22

    Re: make a mouse path from a to b non-linear

    thanks dude =D great code, now i need to change my old code to it :P 'll post program and final code soon =)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width