Create points has equal distance between two points on screen Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: Create points has equal distance between two points on screen

Post by Hellbent » 17 Apr 2021, 20:33

The code I posted earlier to do the calculations was a modified version of code I use to apply forces such as simulated gravity. After a second thought I realized that since the change in position is always the same in your case, there is no need to do the calculations in the loop.
Here is the altered version.

Code: Select all

#SingleInstance, Force
CoordMode, Mouse, Screen
SetBatchLines, -1
Handles := _CreateGui()
return
GuiClose:
GuiContextMenu:
*ESC::ExitApp
SetPositions:
	While( !GetKeyState( "ctrl" ) ){
		MouseGetPos, x , y
		ToolTip, Set Position 1: `nMove your cursor to your Starting Position `nand then press the "ctrl" key to lock it.`nX: %x% `tY: %y%
		sleep, 30
	}
	SoundBeep, 500
	;Create a vector object with the starting position
	StartingPosition :=  New Vector( x , y )
	KeyWait, ctrl
	While(!GetKeyState("ctrl")){
		MouseGetPos, x , y
		ToolTip, Set Position 2: `nMove your cursor to your Ending Position `nand then press the "ctrl" key to lock it.`nX: %x% `tY: %y%
		sleep, 30
	}
	ToolTip,
	SoundBeep, 500
	;Create a vector object with the ending position
	EndingPosition := New Vector( x , y )
	return
	
PlayBack:

	;Get the values from the gui edits
	for, k, v in Handles
		GuiControlGet, %k% , 1: , % v
		
	;Set / Reset the value of "currentPosition" to be the starting position selected by the user
	CurrentPosition := New Vector( StartingPosition.X , StartingPosition.Y ) 
	
	;Get the distance between the starting and ending positions. This is used with the intervals value to determin how far to move for each step.
	Distance := EndingPosition.Dist( CurrentPosition ) 
	
	;Create a new vector object with the same values as the endingposition that will be used to calculate the change in position
	PositionUpDate := New Vector( EndingPosition.X , EndingPosition.Y ) 
	
	;Subtract the value of "currentPosition" to get a vector that points from the current position to the ending position
	PositionUpDate.Sub( CurrentPosition )
	
	;Take the vector and scale it down to the distance to move for each interval
	PositionUpDate.SetMag( Distance / IntervalEdit )
	
	;Move the cursor to the starting position
	DllCall("SetCursorPos", "int", StartingPosition.X, "int", StartingPosition.Y) 
	
	;wait the delay value amount of time
	Sleep, % DelayEdit
	
	;Loop the number of interval times set by the user minus 1 (moves = interval + 1 ( the starting position move ) )
	Loop, % IntervalEdit - 1	{
		
		;Add the scaled vector to the current position
		CurrentPosition.Add( PositionUpDate )
		
		;Move the cursor to the current position
		DllCall("SetCursorPos", "int", CurrentPosition.X, "int", CurrentPosition.Y)
		
		;wait the delay value amount of time
		Sleep, % DelayEdit
	}
	
	;Move the cursor one last time to the endingposition
	DllCall("SetCursorPos", "int", EndingPosition.X, "int", EndingPosition.Y)
	
	return	
	
_CreateGui(){
	local Handles := {}
	Gui, 1:+AlwaysOnTop HwndGuiHwnd
	Gui, 1:Add, Text, xm ym ,Intervals:
	Gui, 1:Add, Edit, x+10 w50 r1 Number Center hwndhwnd, 100
	Handles.IntervalEdit := hwnd
	Gui, 1:Add, Text, xm , Delay Between Intervals [ms]: 
	Gui, 1:Add, Edit, x+10 w50 r1 Number Center hwndhwnd, 10
	Handles.DelayEdit := hwnd
	Gui, 1:Show, 
	WinGetPos,,, w ,, % "ahk_id " GuiHwnd 
	Gui, 1:Add, Button, % "xm  w" w / 2 - 20  " gSetPositions", Set Positions
	Gui, 1:Add, Button, % "x+10 w" w / 2 - 15  " gPlayBack", >  PlayBack
	Gui, 1:Show, AutoSize, Vector Move
	return Handles
}
Class Vector	{
	__New(x:=0,y:=0){
		This.X:=x,This.Y:=y
	}Add(Other_HB_Vector){
		This.X+=Other_HB_Vector.X,This.Y+=Other_HB_Vector.Y
	}Sub(Other_HB_Vector){
		This.X-=Other_HB_Vector.X,This.Y-=Other_HB_Vector.Y
	}mag(){
		return Sqrt(This.X*This.X + This.Y*This.Y)
	}magsq(){
		return This.Mag()**2
	}setMag(in1){
		m:=This.Mag(),This.X := This.X * in1/m,This.Y := This.Y * in1/m
		return This
	}dist(in1){
		return Sqrt(((This.X-In1.X)**2) + ((This.Y-In1.Y)**2))
	}
}
User avatar
Smile_
Posts: 858
Joined: 03 May 2020, 00:51

Re: Create points has equal distance between two points on screen

Post by Smile_ » 18 Apr 2021, 02:07

Hi again @HiSoKa

Optimized it a little, now it is more specific at positions:

Code: Select all

Splits := 4, Count := 1
CoordMode, Mouse, Screen

~LButton::
    xPositions := [], yPositions := [], Count++
    If (Count = 1)
    {
        MouseGetPos, x1, y1
        ToolTip, % "Starting position taken! (" x1 ", " y1 ")"
    }
    Else
    {
        MouseGetPos, x2, y2
        ToolTip, % "Ending position taken! (" x2 ", " y2 ")`nNow Click right mouse button."
        xInterval := Abs((x2 - x1) / Splits),  yInterval := Abs((y2 - y1) / Splits), xPositions.Push(x1), yPositions.Push(y1)
        Loop, % Splits
            (x1 < x2) ? x1 += xInterval : x1 -= xInterval, xPositions.Push(x1), (y1 < y2) ? y1 += yInterval : y1 -= yInterval, yPositions.Push(y1)
        Count := 0, x1 := 0, y1 := 0, x2 := 0, y2 := 0
    }

Return
RButton::
    If (xPositions[1] = "") || (yPositions[1] = "")
    {
        ToolTip, % "Make sure end position is taken first!"
        Return
    }
    Loop, % Splits + 1
    {
        MouseMove, xPositions[A_Index], yPositions[A_Index]
        ToolTip, % "Reached position " A_Index ": X = " xPositions[A_Index] ", Y = " yPositions[A_Index]
        Sleep, 500
    }
Return

Esc::ExitApp

Sqr(Number) {
Return % Number*Number
}
Last edited by Smile_ on 18 Apr 2021, 03:21, edited 2 times in total.
User avatar
HiSoKa
Posts: 480
Joined: 27 Jan 2020, 15:43

Re: Create points has equal distance between two points on screen

Post by HiSoKa » 18 Apr 2021, 02:21

@Hellbent & @Smile_
Your impressed me guys, thanks for both of you, your codes is Amazing.
Post Reply

Return to “Ask for Help (v1)”