if values differ more than 3 percent

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
WalterRoberts
Posts: 67
Joined: 25 Feb 2020, 20:00

if values differ more than 3 percent

22 Jun 2021, 05:58

Code: Select all

MouseGetPos x1, y1
Loop
{
	MouseGetPos x2, y2
	dx := x1/x2
	dy := y1/y2
	
	; if dx or dy differ more than 3 percent
	;	break 
	Sleep 20
}
What is the best way to code this if statement?
I guess simply hard-coding the numbers would be possible, but annoying when changing the threshold:
if (dx < 0.97) || (dx > 1.03) || (dy < 0.97) || (dy > 1.03)

I have written a little function to check if the values differ more than a set limit:

Code: Select all

differ(ratio, limit) {
	lower := 1 - limit/100
	upper := 1 + limit/100
	if (ratio < lower)
		return true
	if (ratio > upper)
		return true
	return false
}

; if differ(dx, 3) || differ(dy, 3)
Is there another solution?
User avatar
mikeyww
Posts: 26888
Joined: 09 Sep 2014, 18:38

Re: if values differ more than 3 percent

22 Jun 2021, 06:20

AHK does accommodate If... between in case helpful. It has a specific syntax, so see the documentation & examples there.
WalterRoberts
Posts: 67
Joined: 25 Feb 2020, 20:00

Re: if values differ more than 3 percent

22 Jun 2021, 07:28

Thanks, that's good to know! I have realized that comparing the difference of total pixels is more reasonable than calculating the ratio since the percentage is going to vary the more the mouse is moving to the bottom right of the screen.

Code: Select all

CoordMode Mouse, Screen
MouseGetPos x1, y1
Loop
{
	MouseGetPos x2, y2
	dx := Abs(x1-x2)
	dy := Abs(y1-y2)

	; if dx or dy differ more than 32 pixels
	if ((dx > 32) || (dy > 32))
		break
	Sleep 20
}
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: if values differ more than 3 percent

22 Jun 2021, 10:24

Depending on what you are using this for. The distance between the two points may be what you want.

Try this.

Code: Select all

#SingleInstance, Force

Numpad1::
	MouseGetPos, x, y
	Start := New HB_Vector( x , y )
	End := New HB_Vector()
	Loop,	{
		MouseGetPos, x, y
		End.X := x , End.Y := y
		if( ( Distance := Start.Dist( End ) ) > 50 ){
			SoundBeep, 500
			break
		}
		sleep, 10
	}
	ToolTip, % "The distance between the start and end points is: " Floor(Distance) " pixels"
	return

Class HB_Vector	{
;Modified class from: https://www.autohotkey.com/boards/viewtopic.php?f=6&t=89290
	__New(x:=0,y:=0){
		This.X:=x , This.Y:=y
	}dist(in1){
		return Sqrt(((This.X-In1.X)**2) + ((This.Y-In1.Y)**2))
	}
}
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: if values differ more than 3 percent

22 Jun 2021, 13:24

Pixel distance visualization.

Code: Select all

;***************************************************************************************************
#Include <My Altered Gdip Lib>   ;Replace with your GDIP LIB
;***************************************************************************************************
#SingleInstance, Force
SetBatchLines, -1
CoordMode, Mouse, Client
GDIP_Startup()
global PicHwnd , Start := New HB_Vector( 100, 100 ) , End := New HB_Vector()
Gui, 1:+AlwaysOnTop -DPIScale +E0x02000000 +E0x00080000
Gui, 1:Color, 22262A
Gui, 1:Margin, 0, 0
Gui, 1:Add, Picture, xm ym w200 h250 0xE HwndPicHwnd
Gui, 1:Show,,Distance
CursorMove()
OnMessage(0x200,"CursorMove")
return
GuiClose:
GuiContextMenu:
*ESC::ExitApp
CursorMove(){
	MouseGetPos, x, y
	DrawGraphics( End.X := x , End.Y := y , Start.Dist(End))
}
Class HB_Vector	{
;Modified class from: https://www.autohotkey.com/boards/viewtopic.php?f=6&t=89290
	__New(x:=0,y:=0){
		This.X:=x , This.Y:=y
	}dist(in1){
		return Sqrt(((This.X-In1.X)**2) + ((This.Y-In1.Y)**2))
	}
}
DrawGraphics(x,y,Distance){
	;Bitmap Created Using: HB Bitmap Maker
	pBitmap := Gdip_CreateBitmap( 200 , 250 ) , G := Gdip_GraphicsFromImage( pBitmap ) , Gdip_SetSmoothingMode( G , 2 )
	Pen := Gdip_CreatePen( "0xFF3399FF" , 1 ) , Gdip_DrawEllipse( G , Pen , 20 , 20 , 160 , 160 ) , Gdip_DeletePen( Pen )
	Pen := Gdip_CreatePen( "0xFFffff00" , 1 ) , Gdip_DrawEllipse( G , Pen ,  100 - distance , 100 - distance , distance*2 , distance * 2  ) , Gdip_DeletePen( Pen )
	Pen := Gdip_CreatePen( "0xFF880000" , 1 ) , Gdip_DrawLine( G , Pen , 99 , 10 , 99 , 190 ) , Gdip_DeletePen( Pen )
	Pen := Gdip_CreatePen( "0xFF33C833" , 1 ) , Gdip_DrawLine( G , Pen , 10 , 99 , 190 , 99 ) , Gdip_DeletePen( Pen )
	Pen := Gdip_CreatePen( "0xFFffff00" , 1 ) , Gdip_DrawLine( G , Pen , 99 , 100 , x , y ) , Gdip_DeletePen( Pen )
	Pen := Gdip_CreatePen( "0xFFff00ff" , 1 ) , Gdip_DrawRectangle( G , Pen , (x>=99)?(99):(x) , (y>=99)?(99):(y) , (x>=99)?(x-99):(99-x) , (y>=99)?(y-99):(99-y) ) , Gdip_DeletePen( Pen )
	Brush := Gdip_BrushCreateSolid( "0xFF3399FF" ) , Gdip_TextToGraphics( G , "Distance 80px" , "s12 Center vCenter Bold c" Brush " x0 y0" , "Segoe ui" , 200 , 17 ) , Gdip_DeleteBrush( Brush )
	Brush := Gdip_BrushCreateSolid( "0xFFFFFF00" ) , Gdip_TextToGraphics( G , "Distance: " Distance "px" , "s12 Center vCenter Bold c" Brush " x0 y220" , "Segoe ui" , 200 , 17 ) , Gdip_DeleteBrush( Brush )
	Gdip_DeleteGraphics( G ) , hBitmap := Gdip_CreateHBITMAPFromBitmap(pBitmap) , Gdip_DisposeImage( pBitmap ) , SetImage( PicHwnd , hBitmap ) , DeleteObject( hBitmap )
}
Temp (1).gif
Temp (1).gif (189.3 KiB) Viewed 316 times
*EDIT*
Reduced gif file size and gui flickering

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Anput, jameswrightesq and 275 guests