This is accurate...not sure about the speed but can't optimise it more as
I'm in the pub at the moment...
put the stuff below in a movie script and call getNearest with your point
list and mouseLoc
Make sure to check that the return value (pNearest) is not 0 (zero)

Like:

nearestPoint = getNearest(pointList,the mouseLoc)

Code below
--------------------------------------------------------------------
-- pList is the list of all points
-- pCenter is the reference center (e.g. mouseLoc)

on getNearest pList,pCenter
  pCount = pList.count
  if pCount > 0 then -- check it's not an empty list
    pNearest = 0
    repeat with y = 1 to pCount
      pDistance = pythagora2Points(pList[y],pCenter)
      if pDistance > pNearest then
        pNearest = y
      end if
    end repeat
    
    return pNearest -- the position in the list..i.e. your pixel position in
the list
  end if
end

-- does the Pythagora's avoiding zero values
on pythagora2Points point1,point2
  x=point1[1]
  y=point1[2]
  a=point2[1]
  b=point2[2]
  
  if point1=point2 then
    return 0
  end if
  if a=x and b>y then
    return (b-y)
  end if
  if a=x and b<y then
    return (y-b)
  end if
  if b=y and a>x then
    return (a-x)
  end if
  if b=y and a<x then
    return (x-a)
  end if
  
  
  if a<>x and b<>y then
    c = sqrt(power((a-x),2) + power((b-y),2))
    return c
  end if  
  
end

--------------------------------------

Good luck


-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Tim Welford
Sent: 11 April 2006 19:26
To: Lingo programming discussion list
Subject: <lingo-l> Finding the closest point from a list of points in lingo


Hi Guys,

I'm hoping someone can help me out with a quick pointer, I've got a tight
deadline and my minds gone a bit blank.

I have a list of points....

[point(112,456), point(12,485), ...] list goes to about 50

These are actually co-ordinates of certain pixels on my stage.

I need to find the closest one (radius) to any given point - i.e. the
mouseLoc - it needs to be fairly efficient as I will need to do it every
frame \ couple of frames.

Can someone point me in the right direction please.

Many thanks

Tim





[To remove yourself from this list, or to change to digest mode, go to
http://www.penworks.com/lingo-l.cgi  To post messages to the list, email
[email protected]  (Problems, email [EMAIL PROTECTED]). Lingo-L is
for learning and helping with programming Lingo.  Thanks!]




[To remove yourself from this list, or to change to digest mode, go to 
http://www.penworks.com/lingo-l.cgi  To post messages to the list, email 
[email protected]  (Problems, email [EMAIL PROTECTED]). Lingo-L is for 
learning and helping with programming Lingo.  Thanks!]

Reply via email to