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.
Printable View
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.
Try this:
vb.net Code:
Dim g As Graphics Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load g = Graphics.FromHwnd(Me.Handle) End Sub Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove Static lastPosition As Point = e.Location g.DrawLine(Pens.Red, lastPosition, e.Location) lastPosition = e.Location 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.
 
=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"..
Do you have the path (array of points) you want your mouse to follow?
See http://msdn.microsoft.com/en-us/libr...=VS.80%29.aspx.
Changing the control points should give you what you want.
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)
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:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Const Pitch As Integer = 5 Const MaxDeviation As Integer = 100 Dim random As New Random Dim PointsToTrace() As Point ReDim PointsToTrace(100000) PointsToTrace(0) = New Point(100, 500) 'start point PointsToTrace(PointsToTrace.Length - 1) = New Point(1000, 500) 'end point '' create points For i As Integer = 1 To PointsToTrace.Length - 2 PointsToTrace(i) = PointsToTrace(i - 1) + New Point(random.Next(0, Pitch), random.Next(-1 * Pitch, Pitch)) If PointsToTrace(i).Y > PointsToTrace(0).Y + MaxDeviation Then PointsToTrace(i).Y -= Pitch ElseIf PointsToTrace(i).Y < PointsToTrace(0).Y - MaxDeviation Then PointsToTrace(i).Y += Pitch End If If PointsToTrace(i).X > PointsToTrace(PointsToTrace.Length - 1).X Then PointsToTrace(i + 1) = PointsToTrace(PointsToTrace.Length - 1) ReDim Preserve PointsToTrace(i + 1) Exit For End If Next '' plot the points Dim g As Graphics g = Graphics.FromHwnd(Me.Handle) g.DrawRectangle(Pens.Blue, New Rectangle(PointsToTrace(0), New Size(5, 5))) g.DrawRectangle(Pens.Blue, New Rectangle(PointsToTrace(PointsToTrace.Length - 1), New Size(5, 5))) For i As Integer = 1 To PointsToTrace.Length - 1 g.DrawLine(Pens.Red, PointsToTrace(i - 1), PointsToTrace(i)) Next End Sub
okj :p
is there any possibility to let the cursor follow this path?
vb.net Code:
Dim PointsToTrace() As Point Dim g As Graphics Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Const Pitch As Integer = 5 Const MaxDeviation As Integer = 100 Dim random As New Random ReDim PointsToTrace(100000) PointsToTrace(0) = New Point(100, 500) 'start point PointsToTrace(PointsToTrace.Length - 1) = New Point(1000, 500) 'end point For i As Integer = 1 To PointsToTrace.Length - 2 PointsToTrace(i) = PointsToTrace(i - 1) + New Point(random.Next(0, Pitch), random.Next(-1 * Pitch, Pitch)) If PointsToTrace(i).Y > PointsToTrace(0).Y + MaxDeviation Then PointsToTrace(i).Y -= Pitch ElseIf PointsToTrace(i).Y < PointsToTrace(0).Y - MaxDeviation Then PointsToTrace(i).Y += Pitch End If If PointsToTrace(i).X > PointsToTrace(PointsToTrace.Length - 1).X Then PointsToTrace(i + 1) = PointsToTrace(PointsToTrace.Length - 1) ReDim Preserve PointsToTrace(i + 1) Exit For End If Next g = Graphics.FromHwnd(Me.Handle) g.DrawRectangle(Pens.Blue, New Rectangle(PointsToTrace(0), New Size(5, 5))) g.DrawRectangle(Pens.Blue, New Rectangle(PointsToTrace(PointsToTrace.Length - 1), New Size(5, 5))) Timer1.Interval = 100 Timer1.Start() End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick Static index As Integer = 1 Cursor.Current.Position = Me.PointToScreen(PointsToTrace(index)) g.DrawLine(Pens.Red, PointsToTrace(index - 1), PointsToTrace(index)) index += 1 If index >= PointsToTrace.Length Then Timer1.Stop() End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Timer1.Stop() End Sub
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)
yeah, but this just draws a curve.. this curve is great, but i wanna let the cursor follow that curve,..
vb.net Code:
Dim PointsToTrace() As Point Dim g As Graphics Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Const Pitch As Integer = 10 Const MaxDeviation As Integer = 50 Dim random As New Random ReDim PointsToTrace(100000) PointsToTrace(0) = New Point(100, 500) 'start point PointsToTrace(PointsToTrace.Length - 1) = New Point(1000, 500) 'end point For i As Integer = 1 To PointsToTrace.Length - 2 PointsToTrace(i) = PointsToTrace(i - 1) + New Point(random.Next(0, Pitch), random.Next(-1 * Pitch, Pitch)) If PointsToTrace(i).Y > PointsToTrace(0).Y + MaxDeviation Then PointsToTrace(i).Y -= Pitch ElseIf PointsToTrace(i).Y < PointsToTrace(0).Y - MaxDeviation Then PointsToTrace(i).Y += Pitch End If If PointsToTrace(i).X > PointsToTrace(PointsToTrace.Length - 1).X Then PointsToTrace(i + 1) = PointsToTrace(PointsToTrace.Length - 1) ReDim Preserve PointsToTrace(i + 1) Exit For End If Next '' smoothen the points before plotting them. '' each additional time you call this function, it will smoothen the points further. SmothenCurve(PointsToTrace) SmothenCurve(PointsToTrace) g = Graphics.FromHwnd(Me.Handle) g.DrawRectangle(Pens.Blue, New Rectangle(PointsToTrace(0), New Size(5, 5))) g.DrawRectangle(Pens.Blue, New Rectangle(PointsToTrace(PointsToTrace.Length - 1), New Size(5, 5))) Timer1.Interval = 100 Timer1.Start() End Sub Private Sub SmothenCurve(ByVal points() As Point) For i As Integer = 1 To PointsToTrace.Length - 2 Step 2 points(i).X = (points(i - 1).X + points(i + 1).X) \ 2 points(i).Y = (points(i - 1).Y + points(i + 1).Y) \ 2 Next For i As Integer = 2 To PointsToTrace.Length - 2 Step 2 points(i).X = (points(i - 1).X + points(i + 1).X) \ 2 points(i).Y = (points(i - 1).Y + points(i + 1).Y) \ 2 Next End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick Static index As Integer = 1 Cursor.Current.Position = Me.PointToScreen(PointsToTrace(index)) g.DrawLine(Pens.Red, PointsToTrace(index - 1), PointsToTrace(index)) index += 1 If index >= PointsToTrace.Length Then index = 1 : Timer1.Stop() 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.
thanks dude =D great code, now i need to change my old code to it :P 'll post program and final code soon =)