-
-
Notifications
You must be signed in to change notification settings - Fork 360
Graham scan implementation in Common Lisp #605
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
berquist
merged 14 commits into
algorithm-archivists:master
from
Trashtalk217:graham-scan-lisp
May 24, 2020
Merged
Changes from 8 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
7b5c628
Added a proof for the Euclidean Algorithm
Trashtalk217 131ae54
Revert "Added a proof for the Euclidean Algorithm"
Trashtalk217 0b2b247
First working version thomas algorithm in lisp.
Trashtalk217 d953323
Added lisp code to the .md file
Trashtalk217 66d2ed2
First version of the graham scan in common lisp.
Trashtalk217 cc372ca
Added Common lisp implementation to .md file
Trashtalk217 af338d6
Revert "Added lisp code to the .md file"
Trashtalk217 bf8faff
Revert "First working version thomas algorithm in lisp."
Trashtalk217 32b6571
minor changes, might have broken something
Trashtalk217 05dc102
made the graham scan non-destructive-ish
Trashtalk217 d367a91
added extra comments to explain unclear code
Trashtalk217 c96d30f
changed the counterclockwise function so it complise with julia version
Trashtalk217 c088595
extraordinarily minor change
Trashtalk217 d64531a
Merge branch 'master' into graham-scan-lisp
Trashtalk217 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
;;;; Graham scan implementation in Common Lisp | ||
|
||
(defstruct (point (:constructor make-point (x y))) x y) | ||
|
||
(defun counterclockwise-p (p1 p2 p3) | ||
"Determines if a turn between three points is counterclockwise" | ||
(>= | ||
(* | ||
(- (point-y p3) (point-y p1)) | ||
(- (point-x p2) (point-x p1))) | ||
(* | ||
(- (point-y p2) (point-y p1)) | ||
(- (point-x p3) (point-x p1))))) | ||
|
||
(defun atan2 (y x) | ||
"Calculates the angle of a point in the euclidean plane in radians" | ||
(cond | ||
((> x 0) (atan y x)) | ||
((and (< x 0) (>= y 0)) (+ (atan y x) pi)) | ||
((and (< x 0) (< y 0)) (- (atan y x) pi)) | ||
((and (eql x 0) (> y 0)) (/ pi 2)) | ||
((and (eql x 0) (< y 0)) (- (/ pi 2))) | ||
;the -1 signifies an exception and is usefull later for sorting by the polar angle | ||
((and (eql x 0) (eql y 0)) (- 1)))) | ||
|
||
(defun polar-angle (ref point) | ||
"Returns the polar angle from a point relative to a reference point" | ||
(atan2 (- (point-y point) (point-y ref)) (- (point-x point) (point-x ref)))) | ||
|
||
(defun lowest-point (gift) | ||
"Returns the lowest point of a gift" | ||
(reduce | ||
(lambda (p1 p2) | ||
(if (< (point-y p1) (point-y p2)) p1 p2)) | ||
gift)) | ||
|
||
(defun graham-scan (gift) | ||
"Finds the convex hull of any distribution of points destructively" | ||
(loop | ||
with n = (length gift) | ||
with lowest = (lowest-point gift) | ||
with sorted = (sort gift #'< :key (lambda (p) (polar-angle lowest p))) | ||
with hull = (subseq sorted 0 3) | ||
|
||
for point in (subseq sorted 3 nil) do | ||
(loop | ||
until (counterclockwise-p (nth (- (length hull) 2) hull) (nth (1- (length hull)) hull) point) do | ||
(setf hull (butlast hull))) | ||
(setf hull (append hull (list point))) | ||
finally (return hull))) | ||
|
||
(defvar gift | ||
(map | ||
'list | ||
(lambda (e) (apply #'make-point e)) | ||
'((-5 2) (5 7) (-6 -12) (-14 -14) (9 9) | ||
(-1 -1) (-10 11) (-6 15) (-6 -8) (15 -9) | ||
(7 -7) (-2 -9) (6 -5) (0 14) (2 8)))) | ||
|
||
(print (graham-scan gift)) |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.