Skip to content

Instantly share code, notes, and snippets.

@gf3
Created December 11, 2011 07:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gf3/1459052 to your computer and use it in GitHub Desktop.
Save gf3/1459052 to your computer and use it in GitHub Desktop.
# Find closest point to a set of points.
findClosestPoint = (needle, haystack) ->
(init = new Point).diff = Number.MAX_VALUE
comparator = (memo, point) ->
point.diff = Math.abs(needle.x - point.x) + Math.abs(needle.y - point.y)
if point.diff < memo.diff then point else memo
_.foldl haystack, comparator, init
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment