Christoph_ Posted October 8, 2006 Share Posted October 8, 2006 How can I calculate the distance between 2 Pixels which I load into an Array ? Link to comment Share on other sites More sharing options...
zcoacoaz Posted October 8, 2006 Share Posted October 8, 2006 (edited) 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 ? You aren't going to get a very good answer unless you give more info. Edited October 8, 2006 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] Link to comment Share on other sites More sharing options...
Helge Posted October 8, 2006 Share Posted October 8, 2006 Func GetDistance($x1, $y1, $x2, $y2) Return Sqrt(($x2 - $x1)^2 + ($y2 - $y1)^2) EndFunc Link to comment Share on other sites More sharing options...
Paulie Posted October 8, 2006 Share Posted October 8, 2006 (edited) 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 October 8, 2006 by Paulie Link to comment Share on other sites More sharing options...
Helge Posted October 8, 2006 Share Posted October 8, 2006 (edited) 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 October 8, 2006 by Helge Link to comment Share on other sites More sharing options...
Paulie Posted October 8, 2006 Share Posted October 8, 2006 (edited) 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 linelol use helge's then Edited October 8, 2006 by Paulie Link to comment Share on other sites More sharing options...
gamerman2360 Posted October 8, 2006 Share Posted October 8, 2006 (edited) Func GetDistance($x1, $y1, $x2, $y2) Return Sqrt(($x2 - $x1)^2 + ($y2 - $y1)^2) EndFuncOh 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 October 8, 2006 by gamerman2360 Link to comment Share on other sites More sharing options...
jvanegmond Posted October 8, 2006 Share Posted October 8, 2006 What if X2/Y2 is bigger then X1/Y1 though?In a normal case this would happen:X1 = 5X2 = 1X difference = 4X difference squared = 16Just looking at the X-coordinates:X1 = 1X2 = 5X difference = -4X difference squared = 16You see that when squared, it doesn't matter at all. github.com/jvanegmond Link to comment Share on other sites More sharing options...
jvanegmond Posted October 8, 2006 Share Posted October 8, 2006 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. github.com/jvanegmond Link to comment Share on other sites More sharing options...
Paulie Posted October 8, 2006 Share Posted October 8, 2006 In a normal case this would happen:X1 = 5X2 = 1X difference = 4X difference squared = 16Just looking at the X-coordinates:X1 = 1X2 = 5X difference = -4X difference squared = 16You see that when squared, it doesn't matter at all.Yeah, i realized this not to long after i posted minebut its the weekend, and i'm mot thinking right... Link to comment Share on other sites More sharing options...
jvanegmond Posted October 8, 2006 Share Posted October 8, 2006 Yeah, i realized this not to long after i posted minebut its the weekend, and i'm mot thinking right... Alcohol? github.com/jvanegmond Link to comment Share on other sites More sharing options...
gamerman2360 Posted October 8, 2006 Share Posted October 8, 2006 (edited) In a normal case this would happen:X1 = 5X2 = 1X difference = 4X difference squared = 16Just looking at the X-coordinates:X1 = 1X2 = 5X difference = -4X difference squared = 16You 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 October 8, 2006 by gamerman2360 Link to comment Share on other sites More sharing options...
jvanegmond Posted October 8, 2006 Share Posted October 8, 2006 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. github.com/jvanegmond Link to comment Share on other sites More sharing options...
Christoph_ Posted October 8, 2006 Author Share Posted October 8, 2006 (edited) 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. That's why I was asking. The old greek should do the trick I think. Edited October 8, 2006 by Christoph_ Link to comment Share on other sites More sharing options...
jvanegmond Posted October 8, 2006 Share Posted October 8, 2006 (edited) 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. expandcollapse popup#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 If $IndiHandle[$x] Then ;not dead $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 If $IndiHandle[$x] Then ;not dead WinMove($IndiHandle[$x],"",$IndiX[$x],$IndiY[$x]) EndIf Next EndFunc Func Kill($n) GUIDelete($IndiHandle[$n]) $IndiHandle[$n] = "" $Population -= 1 $Deaths += 1 EndFunc Edited October 8, 2006 by Manadar github.com/jvanegmond Link to comment Share on other sites More sharing options...
Christoph_ Posted October 8, 2006 Author Share Posted October 8, 2006 Ah cool, yes it's very close to my idea. I will complete my scan routine tomorrow Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now