Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

# RandomBezier() - Move the mouse through a random Bézier path

4 replies to this topic
• Moderators
• 4323 posts
• Last active: Jan 28 2016 01:38 AM
• Joined: 08 Apr 2009
####################################################################################
This old forum will be archived ( see https://autohotkey.c...s-decommission/ ).
For my up-to-date code and information, check the following links:
http://git.io/master | https://github.com/MasterFocus | http://masterfocus.ahk4.net/ | https://sites.google...masterfocusahk/
####################################################################################

Enormous thanks to [VxE] for his excellent approach here with the Bézier curve formula based on the elapsed time.

This function moves the mouse from X0,Y0 to Xf,Yf through a "random" path.
This trajectory is calculated using the Bézier curve formula: [ Wikipedia article ]

The number of control points for the Bézier curve can be defined by the user.
(PX for X points, or PX-Y for a random number of points between X and Y)
If not specified, defaults to P2-19, since we must have at least X0,Y0 and Xf,Yf (a straight line) and values greater than 19 can generate a wrong (negative) coefficient.

I added the possibility of defining "offsets" for the randomized points according to the [X0,Y0,Xf,Yf] rectangle.
They're all 100 pixels by default.
This means that the random Bézier control points will be generated within a box that is wider by 100px in all directions, and the trajectory will never go beyond that.

Finally, the user can specify if the coordinates are relative to the current mouse position, as well as the total time (in miliseconds) of the movement.
It is now possible to specify relative initial coordinates, relative final coordinates, or both.

******************************
CODE REMOVED, CHECK GITHUB: https://github.com/M...ns/RandomBezier
******************************

Old test image (link kept for reference) - http://i.imgur.com/bwksX.png

Old notes (kept for reference):
+ The new code is reduced and features an Options String, but I ended up killing readability.
+ I've managed to adapt the function to evaluate Rational Bézier curves [unreleased code]. However, it makes the curve excessively unpredictable. Also, when using negative weights, any offsets are useless because the curve tends to go beyond them. So, I guess this won't be implemented at all, unless I figure how to "stabilize" the behaviour.

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Antonio França -- git.io -- github.com -- ahk4.net

Member of the AHK community since 08/Apr/2009. Moderator since mid-2012.

• Members
• 44 posts
• Last active: Sep 01 2015 08:24 AM
• Joined: 16 Oct 2012

MasterFocus, this is an excellent piece of work! Just one question; after a few goes at testing this piece of art, I have noticed it will move the mouse from it's current position to a second value relative to the first. Can I make it so the script will always move to a defined coordinate, regardless of the mouse starting position?

-Hugh

• Moderators
• 4323 posts
• Last active: Jan 28 2016 01:38 AM
• Joined: 08 Apr 2009

Yes. The coordinates are relative only when "REL" is present in the options string.

The current way of doing what you want is this:

```MouseGetPos, MouseX, MouseY
RandomBezier( MouseX, MouseY, DestinationX, DestinationY, "options string without the REL part" )
```

Turns out this is a good suggestion... I will implement later a way of starting the movement from the current

mouse position, without the need of using MouseGetPos before.

EDIT: In fact, I shall add a way to specify this for origin and destination separately.

Something like specifying 'RO' for relative origin coords and 'RD' for relative destination coords.

EDIT 2: Added, as I promised. Check GitHub.

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Antonio França -- git.io -- github.com -- ahk4.net

Member of the AHK community since 08/Apr/2009. Moderator since mid-2012.

• Members
• 44 posts
• Last active: Sep 01 2015 08:24 AM
• Joined: 16 Oct 2012

Hello MasterFocus, I've been using your RandomBezier function a bit, and it works very well for what I need.

Just a quick one: I've noticed you can change the amount of points randomly by specifying "Px-x".

Is there a way to specify a random time for the RandomBezier?

Relative origin coords and Relative destination coords sound good as well!

-Hugh

• Moderators
• 4323 posts
• Last active: Jan 28 2016 01:38 AM
• Joined: 08 Apr 2009

Time can be specified after a "T" in the options string.

If you want to achieve a random time, you can do this:

```Random, RandTime, 500, 3000 ; time in milliseconds
RandomBezier( OrigX, OrigY, DestinationX, DestinationY, "some_options T" RandTime " some_other_options" )
```

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Antonio França -- git.io -- github.com -- ahk4.net

Member of the AHK community since 08/Apr/2009. Moderator since mid-2012.