Help with math please

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
tomoe_uehara
Posts: 213
Joined: 05 Oct 2013, 12:37
Contact:

Help with math please

21 Aug 2018, 22:36

Hello, here's what I'm trying to do:
  1. Press F1 to start the script
  2. It will move the mouse cursor to center of monitor screen
  3. Then the mouse cursor will move away from the center, toward the edge of the screen
  4. The angle should be randomized (imagine an exploding firework)
  5. When the mouse cursor hits the edge, it will return back to the center again
I can't seem to write the math of this kind of thing :oops:
Thank you in advance
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Help with math please

22 Aug 2018, 03:48

Hi, tomoe_uehara! :wave:

Try this:

Code: Select all

#NoEnv
#SingleInstance, Force
CoordMode, Mouse, Screen

Pi := 4 * ATan(1)
cX := A_ScreenWidth // 2
cY := A_ScreenHeight // 2

Esc:: ExitApp ; emergency exit



;-------------------------------------------------------------------------------
F1:: ; hotkey to start the animation
;-------------------------------------------------------------------------------
    Loop {
        MouseMove, cX, cY
        Random, Angle, 0, 2 * Pi
        MouseMove
            , cX + 500 * Cos(Angle)
            , cY - 500 * Sin(Angle)
            , 20
    }

Return
I hope you like. Please ask for refinements if needed.
I kept the trigonometry simple, "explosion range" is hardcoded to be 500 pixels. (for now, mouse does not hit the edges of a 1920x1080 screen)
garry
Posts: 3764
Joined: 22 Dec 2013, 12:50

Re: Help with math please

22 Aug 2018, 06:23

@wolf_II, great ,thank you
hope you like. Please ask for refinements if needed.
I kept the trigonometry simple, "explosion range" is hardcoded to be 500 pixels. (for now, mouse does not hit the edges of a 1920x1080 screen)
( maybe can use like this , which is correct for vertical / horizontal , for diagonal move ( which is longer ) needs new calculation )

Code: Select all

 
 ;...
            , cX + cx * Cos(Angle)
            , cY - cy * Sin(Angle)
;...
Noo0B
Posts: 151
Joined: 26 Jan 2018, 19:54

Re: Help with math please

22 Aug 2018, 07:28

Maybe this will give a more homogeneous spread:

Code: Select all

#SingleInstance, Force
CoordMode, Mouse
SetFormat, Float, 0.6
SetDefaultMouseSpeed, 0
ExplosionSpeed:= 2
Esc:: ExitApp
F1::
	Loop {
		Mousemove, A_ScreenWidth/2, A_ScreenHeight/2
		Random, SpreadX, -1.00000, 1.00000
		Random, SpreadY, -1.00000, 1.00000
		Mousemove, SpreadX * A_ScreenWidth/2, SpreadY * A_ScreenHeight/2, %ExplosionSpeed%, R
	}
Return
Regards.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Help with math please

22 Aug 2018, 09:11

there are probably cleverer ways of doing this:

Code: Select all

; https://autohotkey.com/boards/viewtopic.php?p=234882#p234882
#NoEnv
#SingleInstance Force
; SendMode Input
SetBatchLines -1
CoordMode Mouse, Screen

F1::move(rand(0.0, 360.0))
Esc::ExitApp

rand(min, max) {
	Random rand, min, max
	return rand
}

toRad(deg) {
	static PI := 4 * ATan(1)
	return deg * PI / 180
}

toDeg(rad) {
	static PI := 4 * ATan(1)
	return rad * 180 / PI
}

move(deg, speed := 5) {
	static xMid := A_ScreenWidth // 2
	, yMid := A_ScreenHeight // 2
	, thresh := toDeg(ATan(yMid / xMid))

	deg := Mod(deg, 360)

	if (deg < 0)
		deg += 360

	alpha := Tan(toRad(deg))
	beta := Tan(toRad(90 - deg))

	if ((deg > 360 - thresh || deg >= 0) && deg <= thresh)
		x := A_ScreenWidth, y := yMid - (xMid * alpha)
	else if (deg > thresh && deg <= 180 - thresh)
		x := xMid + (yMid * beta), y := 0
	else if (deg > 180 - thresh && deg <= 180 + thresh)
		x := 0, y := yMid + (xMid * alpha)
	else if (deg > 180 + thresh && deg <= 360 - thresh)
		x := xMid - (yMid * beta), y := A_ScreenHeight

	MouseMove % xMid, % yMid, % speed
	MouseMove % x, % y, % speed
	MouseMove % xMid, % yMid, % speed
}
Last edited by swagfag on 22 Aug 2018, 16:03, edited 3 times in total.
User avatar
tomoe_uehara
Posts: 213
Joined: 05 Oct 2013, 12:37
Contact:

Re: Help with math please

22 Aug 2018, 10:57

wolf_II wrote:Hi, tomoe_uehara! :wave:
Try this...
Hello wolf_II & garry, the script works great, Thanks!

Noo0B wrote:Maybe this will give a more homogeneous spread
Sorry but I need the math, by using only the MouseMove command, AHK calculates the coordinates by itself, thus I can't accept this as accepted answer, sorry ;)

swagfag wrote:there are probably cleverer ways of doing this
Your script seem doesn't work as intended, it only moves the cursor to the center of the screen but doesn't move the cursor anywhere else, maybe something is missing?
User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: Help with math please

22 Aug 2018, 11:50

this should do it using simple math ;)

Code: Select all

CoordMode, Mouse, Screen
cx := A_ScreenWidth // 2, cy := A_ScreenHeight // 2
return

F1::
MouseMove, cx, cy
Random, side, 1, 4				; 1=top, 2=bottom, 3=left, 4=right
Random, x, 0, A_ScreenWidth
Random, y, 0, A_ScreenHeight
if (side =1)
	y := 0
if (side =2)
	y := A_ScreenHeight
if (side =3)
	x := 0
if (side =4)
	x := A_ScreenWidth
MouseMove, x, y, 8
MouseMove, cx, cy, 8
return
Last edited by AlphaBravo on 22 Aug 2018, 13:12, edited 1 time in total.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Help with math please

22 Aug 2018, 12:33

tomoe_uehara wrote:Your script seem doesn't work as intended, it only moves the cursor to the center of the screen but doesn't move the cursor anywhere else, maybe something is missing?
i guess u cant call user-defined functions to initialize static variables. it was an issue with PI being uninitialized, while the static initializers were depending on it. its fixed now
Last edited by swagfag on 22 Aug 2018, 16:06, edited 1 time in total.
garry
Posts: 3764
Joined: 22 Dec 2013, 12:50

Re: Help with math please

22 Aug 2018, 14:29

thank you all for the interessant solutions , I think I never found them all
AlphaBravo , tricky, seems so easy ....
Noo0B
Posts: 151
Joined: 26 Jan 2018, 19:54

Re: Help with math please

23 Aug 2018, 01:04

tomoe_uehara wrote:Sorry but I need the math, by using only the MouseMove command, AHK calculates the coordinates by itself, thus I can't accept this as accepted answer, sorry ;)
I was wondering, if in case you need math help, StackOverflow or so might be a better bet for you.
Either way, you will need to formulate better what you are asking for, I'm not sure what you mean with "I need the math".
I can't also really guess what kind of coordinates you need as well.
In case you need circular coordinate values for a script, you can randomize for a full circle and use sin and cosin for the move factors on the code I showed you.
Regards.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 351 guests