Last active
December 14, 2015 21:08
-
-
Save nickbauman/5148590 to your computer and use it in GitHub Desktop.
"Brute force" find farthest points in a 2D graph.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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