# detect the nearest target ?

## Recommended Posts

My problem is "How to detect the nearest target"

EX :

I got a green point at (x,y) pixel and many white points around me . So , how to detect which is the nearest ?

thank you very much

##### Share on other sites

My problem is "How to detect the nearest target"

EX :

I got a green point at (x,y) pixel and many white points around me . So , how to detect which is the nearest ?

thank you very much

Pythagoras?

For each point, calculate Sqrt((Xwhite-Xgreen)^2+(Ywhite-Ygreen)^2).

Roses are FF0000, violets are 0000FF... All my base are belong to you.

##### Share on other sites

too much points - can't not calculate

thank for help .

i'm waiting for another solution

##### Share on other sites

I realize this is an old thread, but I just wanted to say Thanks SadBunny. Your Suggestion works incredibly well, I never woulda figured that one out on my own

;=================================   Closest   =============================
; Function Name:    _Closest
; Description:    Calculate which CoOrds are Closer
; Parameters:       \$Array      Array of CoOrds to check against the Center Location
;                   \$XCenter    Center X CoOrd
;                   \$YCenter    Center Y CoOrd
; Syntax:           _Closest(\$Array,\$XCenter,\$YCenter)
; Author(s):        ofLight
; Returns:      \$output[0] = 0 on failure, \$output[0] > 0 on Success
;============================================================================
Func _Closest(\$Array,\$XCenter,\$YCenter)
Dim \$output[\$Array[0][0]+1]
\$output[0] = \$Array[0][0]
For \$i = 1 to \$Array[0][0]
\$output[\$i] = Round(Sqrt((\$Array[\$i][0]-\$XCenter)^2+(\$Array[\$i][1]-\$YCenter)^2),2)
Next
\$t = 0
\$s = \$output[1]
For \$i = 1 to \$output[0]
If \$output[\$i] < \$s Then
\$s = \$output[\$i]
\$t = \$i
EndIf
Next
Dim \$output[2]
\$output[0] = \$Array[\$t][0]
\$output[1] = \$Array[\$t][1]
Return \$output
EndFunc

EDIT: posted wrong Code , thanks for the heads up

Edited by ofLight

There is always a butthead in the crowd, no matter how hard one tries to keep them out.......Volly

## Create an account

Register a new account