-
-
Notifications
You must be signed in to change notification settings - Fork 46.7k
Add graham scan algorithm #4205
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
Changes from 1 commit
2563270
fdaacde
efdca9e
79c4b6a
bc2b60d
c232984
ecd59a1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
""" | ||
This is a pure Python implementation of the merge-insertion sort algorithm | ||
Source: https://en.wikipedia.org/wiki/Graham_scan | ||
|
||
For doctests run following command: | ||
python3 -m doctest -v graham_scan.py | ||
""" | ||
|
||
from __future__ import annotations | ||
from collections import deque | ||
from enum import Enum | ||
from math import atan2, degrees | ||
from sys import maxsize | ||
|
||
|
||
def graham_scan(points: list[list[int, int]]) -> list[list[int, int]]: | ||
"""Pure implementation of graham scan algorithm in Python | ||
|
||
:param points: The unique points on coordinates. | ||
:return: The points on convex hell. | ||
|
||
Examples: | ||
>>> graham_scan([(9, 6), (3, 1), (0, 0), (5, 5), (5, 2), (7, 0), (3, 3), (1, 4)]) | ||
[(0, 0), (7, 0), (9, 6), (5, 5), (1, 4)] | ||
|
||
>>> graham_scan([(0, 0), (1, 0), (1, 1), (0, 1)]) | ||
[(0, 0), (1, 0), (1, 1), (0, 1)] | ||
|
||
>>> graham_scan([(0, 0), (1, 1), (2, 2), (3, 3), (-1, 2)]) | ||
[(0, 0), (1, 1), (2, 2), (3, 3), (-1, 2)] | ||
|
||
>>> graham_scan([(-100, 20), (99, 3), (1, 10000001), (5133186, -25), (-66, -4)]) | ||
[(5133186, -25), (1, 10000001), (-100, 20), (-66, -4)] | ||
""" | ||
|
||
if len(points) <= 2: | ||
# There is no convex hull | ||
raise ValueError("graham_scan: argument must contain more than 3 points.") | ||
if len(points) == 3: | ||
return points | ||
# find the lowest and the most left point | ||
minidx = 0 | ||
miny, minx = maxsize, maxsize | ||
for i, point in enumerate(points): | ||
x = point[0] | ||
y = point[1] | ||
if y < miny: | ||
miny = y | ||
minx = x | ||
minidx = i | ||
if y == miny: | ||
if x < minx: | ||
minx = x | ||
minidx = i | ||
|
||
# remove the lowest and the most left point from points for preparing for sort | ||
points.pop(minidx) | ||
|
||
def angle_comparer(p: list[int, int]) -> float: | ||
# sort the points accorgind to the angle from the lowest and the most left point | ||
x = p[0] | ||
y = p[1] | ||
angle = degrees(atan2(y - miny, x - minx)) | ||
return angle | ||
|
||
sorted_points = sorted(points, key=lambda p: angle_comparer(p)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. argument name is fixed here fdaacde |
||
# This insert actually costs complexity, | ||
# and you should insteadly add (minx, miny) into stack later. | ||
# I'm using insert just for easy understanding. | ||
sorted_points.insert(0, (minx, miny)) | ||
|
||
# traversal from the lowest and the most left point in anti-clockwise direction | ||
# if direction gets right, the previous point is not the convex hull. | ||
class Direction(Enum): | ||
left = 1 | ||
straight = 2 | ||
right = 3 | ||
|
||
def check_direction( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To reviewers: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file |
||
starting: list[int, int], via: list[int, int], target: list[int, int] | ||
) -> Direction: | ||
x0, y0 = starting | ||
x1, y1 = via | ||
x2, y2 = target | ||
via_angle = degrees(atan2(y1 - y0, x1 - x0)) | ||
if via_angle < 0: | ||
via_angle += 360 | ||
target_angle = degrees(atan2(y2 - y0, x2 - x0)) | ||
if target_angle < 0: | ||
target_angle += 360 | ||
# t- | ||
# \ \ | ||
# \ v | ||
# \| | ||
# s | ||
# via_angle is always lower than target_angle, if direction is left. | ||
# If they are same, it means they are on a same line of convex hull. | ||
if target_angle > via_angle: | ||
return Direction.left | ||
if target_angle == via_angle: | ||
return Direction.straight | ||
if target_angle < via_angle: | ||
return Direction.right | ||
|
||
stack = deque() | ||
stack.append(sorted_points[0]) | ||
stack.append(sorted_points[1]) | ||
stack.append(sorted_points[2]) | ||
# In any ways, the first 3 points line are towards left. | ||
# Because we sort them the angle from minx, miny. | ||
current_direction = Direction.left | ||
|
||
for i in range(3, len(sorted_points)): | ||
while True: | ||
starting = stack[-2] | ||
via = stack[-1] | ||
target = sorted_points[i] | ||
next_direction = check_direction(starting, via, target) | ||
|
||
if next_direction == Direction.left: | ||
current_direction = Direction.left | ||
break | ||
if next_direction == Direction.straight: | ||
if current_direction == Direction.left: | ||
# We keep current_direction as left. | ||
# Because if the straight line keeps as straight, | ||
# we want to know if this straight line is towards left. | ||
break | ||
elif current_direction == Direction.right: | ||
# If the straight line is towards right, | ||
# every previous points on those straigh line is not convex hull. | ||
stack.pop() | ||
if next_direction == Direction.right: | ||
stack.pop() | ||
stack.append(sorted_points[i]) | ||
return list(stack) |
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.
As there is no test file in this pull request nor any test function or class in the file
other/graham_scan.py
, please provide doctest for the functionangle_comparer
Please provide descriptive name for the parameter:
p
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.
To reviewers:
This is an inner function.
argument name is fixed here fdaacde