I need help with a radar code that is too slow

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
mexican scientist
Posts: 33
Joined: 02 Jul 2020, 21:55

I need help with a radar code that is too slow

02 Feb 2023, 01:23

I created a code that acts like a tiny radar that searches pixels around a center (108,520) and with varying radius going from 10 to 97. So, the first loop sets the radius, the second loop searches for all the angles and calculates the x and y coordinates. Then it uses the pixel search to test if the pixel contains the color. The problem with this code is that it is way too slow! The problem is that cos and sin don't change too fast and most of the time the rounded values are the same, so the pixel search function searches for the same area thus wasting time!

Code: Select all

radius := 10								;initialize radius to 10
angle := 0									;initialize angle to 0 degrees
z::
Loop, 87									;loop 87 times only (create 87 circles with radius 10,11,12,13,...97)
	{
	Loop, 360								;test all possible angles in increments of 1 degree
		{
		theta := (3.1415/180)*angle			;convert angle to radians
      		x := round(108 + radius * Cos(theta))		;calculate x, round the value of x because pixel coordinates are integers
      		y := round(521 - radius * Sin(theta))		;calculate y, round the value of y because pixel coordinates are integers
      		PixelGetColor, color, %x%, %y%, Fast, 20	;get the pixel color code at x,y
		Tooltip %radius%
		if (color = 0xFF0000)					;if the color is found, then print box with information
			{
			MsgBox "X is: ", %x% , "Y is: ", %y%, "The angle is: ", %angle%
			Exit
			}
		angle++ 							;increase angle by 1 degree
		}
	radius++								;increase radius by 1
	angle := 0								;reset angle to 0 to start a new search from origin
	}
MsgBox Pixel not found!
For example, lets assume theta = 36 and radius = 10
y = 515.122
The next loop sets theta = 37, so
y = 514.98
The next loop sets theta = 38, so
y = 514.84
the next loop sets theta = 39, so
y = 514.706

Do you think this is the reason why this code takes too long to run? The sin and cos do not change fast and pixel search is looking at the same coordinates over and over again. What is one way to modify this code so that I can make it fast?

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], mikeyww and 183 guests