Calculate distance between 2 pixels

Recommended Posts

How can I calculate the distance between 2 Pixels which I load into an Array ?

Share on other sites

Get the location of the pixel further away from the top left of the screen and subtract the location of the other pixel from it ?

Edited by Xenogis

[font="Times"] If anyone remembers me, I am back. Maybe to stay, maybe not.----------------------------------------------------------------------------------------------------------[/font][font="Times"]Things I am proud of: Pong! in AutoIt | SearchbarMy website: F.R.I.E.S.A little website that is trying to get started: http://thepiratelounge.net/ (not mine)[/font][font="Times"] ----------------------------------------------------------------------------------------------------------[/font][font="Arial"]The newbies need to stop stealing avatars!!! It is confusing!![/font]

Share on other sites

```Func GetDistance(\$x1, \$y1, \$x2, \$y2)
Return Sqrt((\$x2 - \$x1)^2 + (\$y2 - \$y1)^2)
EndFunc```

Share on other sites

```Func GetDistance(\$x1, \$y1, \$x2, \$y2)
Return Sqrt((\$x2 - \$x1)^2 + (\$y2 - \$y1)^2)
EndFuncoÝ÷ Ûú®¢×«b}}¿ch¬n( z»azuõým «­¢+ÙÕ¹}A¥á±¥ÍÐ ÀÌØí}`Ä°ÀÌØí}dÄ°ÀÌØí}`È°ÀÌØí}dÈ¤)¥´ÀÌØíIÌÈ)%ÀÌØí}`ÄÐìÀÌØí}`ÈQ¡¸(\$ÀÌØía¥ÍÐôÀÌØí}`Ä´ÀÌØí}`È)±Í%ÀÌØí}`Ä±ÐìÀÌØí}`ÈÑ¡¸(\$ÀÌØía¥ÍÐôÀÌØí}`È´ÀÌØí}`Ä)±Í(\$ÀÌØía¥ÍÐôÀ)¹%()%ÀÌØí}dÄÐìÀÌØí}dÈQ¡¸(\$ÀÌØíe¥ÍÐôÀÌØí}dÄ´ÀÌØí}dÈ)±Í%ÀÌØí}`Ä±ÐìÀÌØí}`ÈÑ¡¸(\$ÀÌØíe¥ÍÐôÀÌØí}dÈ´ÀÌØí}dÄ)±Í(\$ÀÌØíe¥ÍÐôÀ)¹%()1½°ÀÌØíIÌÈô ÀÌØíe¥ÍÑxÈ¤¬ ÀÌØía¥ÍÑxÈ¤)1½°ÀÌØíIÌôI½Õ¹¡MÅÉÐ ÀÌØíIÌÈ¤°Ä¤)IÑÕÉ¸ÀÌØíIÌ)¹Õ¹```

Try this

Edited by Paulie

Share on other sites

What if X2/Y2 is bigger then X1/Y1 though?

Actually it doesn't matter.

```MsgBox(64, "", GetDistance(10, 10, 50, 50))
MsgBox(64, "", GetDistance(50, 50, 10, 10))

Func GetDistance(\$x1, \$y1, \$x2, \$y2)
Return Sqrt((\$x2 - \$x1)^2 + (\$y2 - \$y1)^2)
EndFunc```

Edit : forgot a line

Edited by Helge

Share on other sites

Actually it doesn't matter.

```MsgBox(64, "", GetDistance(10, 10, 50, 50))
MsgBox(64, "", GetDistance(50, 50, 10, 10))

Func GetDistance(\$x1, \$y1, \$x2, \$y2)
Return Sqrt((\$x2 - \$x1)^2 + (\$y2 - \$y1)^2)
EndFunc```

Edit : forgot a line

lol use helge's then Edited by Paulie

Share on other sites

```Func GetDistance(\$x1, \$y1, \$x2, \$y2)
Return Sqrt((\$x2 - \$x1)^2 + (\$y2 - \$y1)^2)
EndFunc```
Oh no! The distance formula!! It followed me all the way from geometry class!

Actually that only works if your doing something like wanting the real distance between 2 points. If you want something like, the distance between 2 checker pieces(movement wise), you just need to find out which number(x or y distance) is bigger.

Edited by gamerman2360

Share on other sites

What if X2/Y2 is bigger then X1/Y1 though?

In a normal case this would happen:

X1 = 5

X2 = 1

X difference = 4

X difference squared = 16

Just looking at the X-coordinates:

X1 = 1

X2 = 5

X difference = -4

X difference squared = 16

You see that when squared, it doesn't matter at all.

Share on other sites

Oh no! The distance formula!! It followed me all the way from geometry class!

Actually that only works if your doing something like wanting the real distance between 2 points. If you want something like, the distance between 2 checker pieces(movement wise), you just need to find out which number(x or y distance) is bigger.

Yes, but if you're talking about checkers. Your moves would be limited, and that's a whole different case, and therefore cannot be solved with pythagoras.

Share on other sites

In a normal case this would happen:

X1 = 5

X2 = 1

X difference = 4

X difference squared = 16

Just looking at the X-coordinates:

X1 = 1

X2 = 5

X difference = -4

X difference squared = 16

You see that when squared, it doesn't matter at all.

Yeah, i realized this not to long after i posted mine

but its the weekend, and i'm mot thinking right...

Share on other sites

Yeah, i realized this not to long after i posted mine

but its the weekend, and i'm mot thinking right...

Alcohol?

Share on other sites

In a normal case this would happen:

X1 = 5

X2 = 1

X difference = 4

X difference squared = 16

Just looking at the X-coordinates:

X1 = 1

X2 = 5

X difference = -4

X difference squared = 16

You see that when squared, it doesn't matter at all.

What should actually happen is that ex: Sqrt(9) would return +9 and -9. (Since 3^2 = 9 and (-3)^2 = 9)

Since we don't have that ability, the absolute value is givin.

If you wanted to be mathmatically correct Sqrt() should be called something like AbsSqrt().

Edited by gamerman2360

Share on other sites

What should actually happen is that ex: Sqrt(9) would return +9 and -9. (Since 3^2 = 9 and (-3)^2 = 9)

Since we don't have that ability, the absolute value is givin.

If you wanted to be mathmatically correct Sqrt() should be called something like AbsSqrt().

I agree. However, when you are talking distance. It doesn't matter wether you go 9 units forward or 9 units backwards. The distance is always 9 units.

I learned to use a -Sqrt and a +sqrt in formulas that I create, because calculators always return AbsSqrt() as you call it.

Share on other sites

Jesus, many replies in not many minutes.

I'm talking about movement; I've written a small bot which works well so far, however I'm still working on it to optimize it.

I've a routine which scans the screen for targets & pixelsearch returns only the first found <<< sucks. Only workaround to get the closest target is to run multiple scans in multiple areas, which is not good either as it is not flexible.

So I scan the screen line per line right now (to sort false positives out) which is pretty fast (drops fps a little for something like 0,2s or so).

Now I want to drop the first target into an Array, get distance; check the next targt -> if distance to next target is bigger, drop it, if the distance is smaller replace the target in the Array until the scan has run through the screen, the Array will contain the next target location with the shortest route. You could use it for OCR as well though.

The old greek should do the trick I think.

Edited by Christoph_

Share on other sites

I just so happen to have the perfect example for you:

I was building this as a step-up to a full-scaled Evolution simulator.

You should look at in particular where I use \$Distance = Sqrt(((\$IndiX[\$x]-\$IndiX[\$z])^2)+((\$IndiY[\$x]-\$IndiY and check it against \$SmallestDistance.

The rest of the code is just there to make it work in my case.

```#include <GUIConstants.au3>
#include <Color.au3>

Global \$IndiNo = 0, \$Population = 0, \$Deaths = 0
Global \$IndiX[500],\$IndiY[500],\$IndiHandle[500],\$IndiColor[500]
Global \$SmallestZ

Create(0xFF0000)
Create(0xFF0000)
Create(0xFF0000)

Create(0x00FF00)
Create(0x00FF00)
Create(0x00FF00)

While \$Population > 1
\$Lowest = 100
For \$x = 0 to \$IndiNo
If \$IndiHandle[\$x] Then
If \$x < \$Lowest Then
\$Lowest = \$x
EndIf
EndIf
Next
For \$x = \$Lowest to \$IndiNo

\$SmallestZ = False
\$SmallestDistance = Sqrt(((@DesktopWidth)^2)+((@DesktopHeight)^2))

For \$z = \$Lowest to \$IndiNo
If \$IndiHandle[\$z] AND (\$x<>\$z) Then ;not dead
;If \$IndiColor[\$x] <> \$IndiColor[\$z] Then

\$Distance = Sqrt(((\$IndiX[\$x]-\$IndiX[\$z])^2)+((\$IndiY[\$x]-\$IndiY[\$z])^2))

If \$Distance < \$SmallestDistance Then

\$SmallestDistance = \$Distance
\$SmallestZ = \$z

EndIf
;EndIf
EndIf
Next

If \$SmallestZ Then
\$z = \$SmallestZ

\$XOk = 0
\$YOk = 0
If (\$IndiX[\$x]-\$IndiX[\$z]) > 0 Then
\$IndiX[\$x] -= 1
ElseIf (\$IndiX[\$x]-\$IndiX[\$z]) < 0 Then
\$IndiX[\$x] += 1
Else
\$XOk = 1
EndIf

If (\$IndiY[\$x]-\$IndiY[\$z]) > 0 Then
\$IndiY[\$x] -= 1
ElseIf (\$IndiY[\$x]-\$IndiY[\$z]) < 0 Then
\$IndiY[\$x] += 1
Else
\$YOk = 1
EndIf

If \$XOk AND \$YOk Then
Kill(\$x)
Kill(\$z)
\$Red = (_ColorGetRed(\$IndiColor[\$x]) + _ColorGetRed(\$IndiColor[\$z])) / 2
\$Green = (_ColorGetGreen(\$IndiColor[\$x]) + _ColorGetGreen(\$IndiColor[\$z])) / 2
\$Blue = (_ColorGetBlue(\$IndiColor[\$x]) + _ColorGetBlue(\$IndiColor[\$z])) / 2
\$Output = "0x" & Hex(\$Red,2) & Hex(\$Green,2) & Hex(\$Blue,2)
Create(\$Output)
If Random(0,1,1) Then
Create(\$Output)
Else
Create(Random(0,0xFFFFFF,1))
EndIf
EndIf
EndIf
EndIf
Next

Update()
WEnd

Func Create(\$Color)
For \$x = 0 to \$Population
If Not \$IndiHandle[\$x] Then
ExitLoop
EndIf
Next
\$IndiX[\$x] = Random(0,@DesktopWidth-30,1)
\$IndiY[\$x] = Random(0,@DesktopHeight-30,1)
\$IndiColor[\$x] = \$Color

\$IndiHandle[\$x] = GUICreate(\$x,40,40,\$IndiX[\$x],\$IndiY[\$x],-1,\$WS_EX_TOOLWINDOW)
GUISetBkColor(\$IndiColor[\$x])

WinSetOnTop(\$IndiHandle[\$x],"", 1)
GUISetState()

\$Population += 1
\$IndiNo += 1
EndFunc

Func Update()
For \$x = \$Lowest to \$IndiNo
WinMove(\$IndiHandle[\$x],"",\$IndiX[\$x],\$IndiY[\$x])
EndIf
Next
EndFunc

Func Kill(\$n)
GUIDelete(\$IndiHandle[\$n])
\$IndiHandle[\$n] = ""
\$Population -= 1
\$Deaths += 1
EndFunc```

Share on other sites

Ah cool, yes it's very close to my idea.

I will complete my scan routine tomorrow