Jump to content
Sign in to follow this  
polaris2490

detect the nearest target ?

Recommended Posts

polaris2490

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 this post


Link to post
Share on other sites
SadBunny

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 this post


Link to post
Share on other sites
polaris2490

too much points - can't not calculate :)

thank for help .

i'm waiting for another solution

Share this post


Link to post
Share on other sites
ofLight

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

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×