Skip to content

Instantly share code, notes, and snippets.

@nickbauman
Last active December 14, 2015 21:08
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 nickbauman/5148590 to your computer and use it in GitHub Desktop.
Save nickbauman/5148590 to your computer and use it in GitHub Desktop.
"Brute force" find farthest points in a 2D graph.
(defn find-distance
"Pthagorian distance. Functions x and y are just wrappers around a two-item seq."
[a-coord b-coord]
(let [xsize (- (x a-coord) (x b-coord))
ysize (- (y a-coord) (y b-coord))]
(sqrt (+ (* xsize xsize) (* ysize ysize)))))
(defn find-farthest-point
"Takes a point 'pt' (a two-item seq of numbers) and a seq of points and returns
the farthest point away from 'pt' in 'pt-list'"
[pt pt-list]
(last (sort (map (fn [coord] [(find-distance pt coord) pt coord]) pt-list))))
(defn find-farthest-points
"Returns the the two points farthest from each other in a 2D graph expressed
as a seq of two-item seq 'pt-list'"
[pt-list]
(first
(sort
(comparator (fn[[distance-a _ _] [distance-b _ _]] (> distance-a distance-b)))
(map (fn[coord] (find-farthest-point coord pt-list)) pt-list))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment