-
-
Notifications
You must be signed in to change notification settings - Fork 357
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
Emacs Lisp implementation of Graham scan #908
Open
berquist
wants to merge
5
commits into
algorithm-archivists:main
Choose a base branch
from
berquist:graham-scan-elisp
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2bd895e
First Emacs Lisp implementation of Graham scan
berquist da294f7
Simpler snoc
berquist 7cc5b42
Remove stray comment
berquist 9fa8bad
Remove dependence on dash.el, add more docstrings
berquist edeb7d2
Add code sections to text
berquist 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 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,56 @@ | ||
(require 'cl-seq) | ||
|
||
(defun snoc (list elem) | ||
"Append ELEM to the end of the list. | ||
|
||
This is like `cons', but operates on the end of list. | ||
|
||
Adapted from dash.el." | ||
(append list (list elem))) | ||
|
||
(defun nthrev (n lst) | ||
"Return the Nth element of LST from the end." | ||
(nth (- (length lst) (1+ n)) lst)) | ||
|
||
(defun is-ccw (a b c) | ||
"Determines if a turn between three points is counterclockwise." | ||
(>= (* (- (nth 1 c) (nth 1 a)) (- (nth 0 b) (nth 0 a))) | ||
(* (- (nth 1 b) (nth 1 a)) (- (nth 0 c) (nth 0 a))))) | ||
|
||
(defun polar-angle (ref point) | ||
"Returns the polar angle from a point relative to a reference point" | ||
(atan (- (nth 1 point) (nth 1 ref)) (- (nth 0 point) (nth 0 ref)))) | ||
|
||
(defun graham-scan (initial-gift) | ||
"Finds the convex hull of a distribution of points with a Graham scan." | ||
(let* ((gift (cl-remove-duplicates initial-gift)) | ||
;; This is /only/ to get the starting point. | ||
(min-sorted-gift (sort gift (lambda (p1 p2) (< (nth 1 p1) (nth 1 p2))))) | ||
(start (car min-sorted-gift)) | ||
(trimmed-gift (cdr min-sorted-gift)) | ||
(points (sort trimmed-gift (lambda (p1 p2) (< (polar-angle start p1) | ||
(polar-angle start p2))))) | ||
(hull (list start (car points) (cadr points)))) | ||
(dolist (point (cddr points)) | ||
(while (not (is-ccw (nthrev 1 hull) (nthrev 0 hull) point)) | ||
(setq hull (reverse (cdr (reverse hull))))) | ||
(setq hull (snoc hull point))) | ||
hull)) | ||
|
||
(princ | ||
(graham-scan | ||
'((-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)))) |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This probably destroys the formal scaling of the method.Nevermind, this is still linear, just inefficient.