|
-
Feb 26th, 2011, 07:56 AM
#1
Thread Starter
Junior Member
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.
-
Feb 26th, 2011, 08:11 AM
#2
Re: make a mouse path from a to b non-linear
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.
 
-
Feb 26th, 2011, 08:17 AM
#3
Thread Starter
Junior Member
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"..
-
Feb 26th, 2011, 08:39 AM
#4
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?
-
Feb 26th, 2011, 08:43 AM
#5
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.
-
Feb 26th, 2011, 08:49 AM
#6
Thread Starter
Junior Member
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)
-
Feb 26th, 2011, 10:11 AM
#7
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:
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
-
Feb 26th, 2011, 10:18 AM
#8
Thread Starter
Junior Member
Re: make a mouse path from a to b non-linear
okj 
is there any possibility to let the cursor follow this path?
-
Feb 26th, 2011, 10:34 AM
#9
Re: make a mouse path from a to b non-linear
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
-
Feb 26th, 2011, 11:40 AM
#10
Thread Starter
Junior Member
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)
-
Feb 26th, 2011, 12:00 PM
#11
Re: make a mouse path from a to b non-linear
 Originally Posted by dbasnett
Did you look at this at all?
-
Feb 26th, 2011, 12:01 PM
#12
Thread Starter
Junior Member
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,..
-
Feb 26th, 2011, 02:33 PM
#13
Re: make a mouse path from a to b non-linear
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.
-
Feb 26th, 2011, 05:14 PM
#14
Thread Starter
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|