2019-08-17 15:36:31 +00:00
|
|
|
"""
|
2019-08-19 13:37:49 +00:00
|
|
|
The convex hull problem is problem of finding all the vertices of convex polygon, P of
|
2019-08-17 15:36:31 +00:00
|
|
|
a set of points in a plane such that all the points are either on the vertices of P or
|
2019-08-19 13:37:49 +00:00
|
|
|
inside P. TH convex hull problem has several applications in geometrical problems,
|
|
|
|
computer graphics and game development.
|
2019-08-17 15:36:31 +00:00
|
|
|
|
2019-08-19 13:37:49 +00:00
|
|
|
Two algorithms have been implemented for the convex hull problem here.
|
2019-08-17 15:36:31 +00:00
|
|
|
1. A brute-force algorithm which runs in O(n^3)
|
2019-09-04 20:06:44 +00:00
|
|
|
2. A divide-and-conquer algorithm which runs in O(n log(n))
|
2019-08-17 15:36:31 +00:00
|
|
|
|
|
|
|
There are other several other algorithms for the convex hull problem
|
2019-08-19 13:37:49 +00:00
|
|
|
which have not been implemented here, yet.
|
2019-08-17 15:36:31 +00:00
|
|
|
|
|
|
|
"""
|
2021-09-07 11:37:03 +00:00
|
|
|
from __future__ import annotations
|
2019-08-17 15:36:31 +00:00
|
|
|
|
2021-09-07 11:37:03 +00:00
|
|
|
from typing import Iterable
|
2020-10-29 00:46:16 +00:00
|
|
|
|
2019-08-17 15:36:31 +00:00
|
|
|
|
|
|
|
class Point:
|
|
|
|
"""
|
|
|
|
Defines a 2-d point for use by all convex-hull algorithms.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
x: an int or a float, the x-coordinate of the 2-d point
|
|
|
|
y: an int or a float, the y-coordinate of the 2-d point
|
|
|
|
|
|
|
|
Examples
|
|
|
|
--------
|
|
|
|
>>> Point(1, 2)
|
2019-12-08 21:42:17 +00:00
|
|
|
(1.0, 2.0)
|
2019-08-17 15:36:31 +00:00
|
|
|
>>> Point("1", "2")
|
|
|
|
(1.0, 2.0)
|
|
|
|
>>> Point(1, 2) > Point(0, 1)
|
|
|
|
True
|
|
|
|
>>> Point(1, 1) == Point(1, 1)
|
|
|
|
True
|
|
|
|
>>> Point(-0.5, 1) == Point(0.5, 1)
|
|
|
|
False
|
|
|
|
>>> Point("pi", "e")
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
2019-12-08 22:15:17 +00:00
|
|
|
ValueError: could not convert string to float: 'pi'
|
2019-12-08 21:42:17 +00:00
|
|
|
"""
|
2019-08-17 15:36:31 +00:00
|
|
|
|
|
|
|
def __init__(self, x, y):
|
2019-12-08 22:15:17 +00:00
|
|
|
self.x, self.y = float(x), float(y)
|
2019-08-17 15:36:31 +00:00
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
return self.x == other.x and self.y == other.y
|
|
|
|
|
|
|
|
def __ne__(self, other):
|
|
|
|
return not self == other
|
|
|
|
|
|
|
|
def __gt__(self, other):
|
|
|
|
if self.x > other.x:
|
|
|
|
return True
|
|
|
|
elif self.x == other.x:
|
|
|
|
return self.y > other.y
|
|
|
|
return False
|
|
|
|
|
|
|
|
def __lt__(self, other):
|
|
|
|
return not self > other
|
|
|
|
|
|
|
|
def __ge__(self, other):
|
|
|
|
if self.x > other.x:
|
|
|
|
return True
|
|
|
|
elif self.x == other.x:
|
|
|
|
return self.y >= other.y
|
|
|
|
return False
|
|
|
|
|
|
|
|
def __le__(self, other):
|
|
|
|
if self.x < other.x:
|
|
|
|
return True
|
|
|
|
elif self.x == other.x:
|
|
|
|
return self.y <= other.y
|
|
|
|
return False
|
|
|
|
|
|
|
|
def __repr__(self):
|
2019-12-07 05:39:59 +00:00
|
|
|
return f"({self.x}, {self.y})"
|
2019-08-17 15:36:31 +00:00
|
|
|
|
|
|
|
def __hash__(self):
|
|
|
|
return hash(self.x)
|
|
|
|
|
|
|
|
|
2020-10-29 00:46:16 +00:00
|
|
|
def _construct_points(
|
2021-09-07 11:37:03 +00:00
|
|
|
list_of_tuples: list[Point] | list[list[float]] | Iterable[list[float]],
|
|
|
|
) -> list[Point]:
|
2019-08-17 15:36:31 +00:00
|
|
|
"""
|
|
|
|
constructs a list of points from an array-like object of numbers
|
|
|
|
|
|
|
|
Arguments
|
|
|
|
---------
|
|
|
|
|
|
|
|
list_of_tuples: array-like object of type numbers. Acceptable types so far
|
|
|
|
are lists, tuples and sets.
|
|
|
|
|
|
|
|
Returns
|
|
|
|
--------
|
|
|
|
points: a list where each item is of type Point. This contains only objects
|
|
|
|
which can be converted into a Point.
|
|
|
|
|
|
|
|
Examples
|
|
|
|
-------
|
|
|
|
>>> _construct_points([[1, 1], [2, -1], [0.3, 4]])
|
2019-12-08 22:15:17 +00:00
|
|
|
[(1.0, 1.0), (2.0, -1.0), (0.3, 4.0)]
|
2019-08-17 15:36:31 +00:00
|
|
|
>>> _construct_points([1, 2])
|
|
|
|
Ignoring deformed point 1. All points must have at least 2 coordinates.
|
|
|
|
Ignoring deformed point 2. All points must have at least 2 coordinates.
|
|
|
|
[]
|
|
|
|
>>> _construct_points([])
|
|
|
|
[]
|
|
|
|
>>> _construct_points(None)
|
|
|
|
[]
|
|
|
|
"""
|
|
|
|
|
2021-09-07 11:37:03 +00:00
|
|
|
points: list[Point] = []
|
2019-08-17 15:36:31 +00:00
|
|
|
if list_of_tuples:
|
|
|
|
for p in list_of_tuples:
|
2020-10-29 00:46:16 +00:00
|
|
|
if isinstance(p, Point):
|
|
|
|
points.append(p)
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
points.append(Point(p[0], p[1]))
|
|
|
|
except (IndexError, TypeError):
|
|
|
|
print(
|
|
|
|
f"Ignoring deformed point {p}. All points"
|
|
|
|
" must have at least 2 coordinates."
|
|
|
|
)
|
2019-08-17 15:36:31 +00:00
|
|
|
return points
|
|
|
|
|
|
|
|
|
2021-09-07 11:37:03 +00:00
|
|
|
def _validate_input(points: list[Point] | list[list[float]]) -> list[Point]:
|
2019-08-17 15:36:31 +00:00
|
|
|
"""
|
|
|
|
validates an input instance before a convex-hull algorithms uses it
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
---------
|
|
|
|
points: array-like, the 2d points to validate before using with
|
|
|
|
a convex-hull algorithm. The elements of points must be either lists, tuples or
|
|
|
|
Points.
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
points: array_like, an iterable of all well-defined Points constructed passed in.
|
|
|
|
|
|
|
|
|
|
|
|
Exception
|
|
|
|
---------
|
2020-06-16 08:09:19 +00:00
|
|
|
ValueError: if points is empty or None, or if a wrong data structure like a scalar
|
|
|
|
is passed
|
2019-08-17 15:36:31 +00:00
|
|
|
|
|
|
|
TypeError: if an iterable but non-indexable object (eg. dictionary) is passed.
|
|
|
|
The exception to this a set which we'll convert to a list before using
|
|
|
|
|
|
|
|
|
|
|
|
Examples
|
|
|
|
-------
|
|
|
|
>>> _validate_input([[1, 2]])
|
2019-12-08 22:15:17 +00:00
|
|
|
[(1.0, 2.0)]
|
2019-08-17 15:36:31 +00:00
|
|
|
>>> _validate_input([(1, 2)])
|
2019-12-08 22:15:17 +00:00
|
|
|
[(1.0, 2.0)]
|
2019-08-17 15:36:31 +00:00
|
|
|
>>> _validate_input([Point(2, 1), Point(-1, 2)])
|
2019-12-08 22:15:17 +00:00
|
|
|
[(2.0, 1.0), (-1.0, 2.0)]
|
2019-08-17 15:36:31 +00:00
|
|
|
>>> _validate_input([])
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
ValueError: Expecting a list of points but got []
|
|
|
|
>>> _validate_input(1)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
ValueError: Expecting an iterable object but got an non-iterable type 1
|
|
|
|
"""
|
|
|
|
|
2020-10-29 00:46:16 +00:00
|
|
|
if not hasattr(points, "__iter__"):
|
|
|
|
raise ValueError(
|
|
|
|
f"Expecting an iterable object but got an non-iterable type {points}"
|
|
|
|
)
|
|
|
|
|
2019-08-17 15:36:31 +00:00
|
|
|
if not points:
|
2019-12-07 05:39:59 +00:00
|
|
|
raise ValueError(f"Expecting a list of points but got {points}")
|
2019-08-17 15:36:31 +00:00
|
|
|
|
2020-10-29 00:46:16 +00:00
|
|
|
return _construct_points(points)
|
2019-08-17 15:36:31 +00:00
|
|
|
|
|
|
|
|
2020-10-29 00:46:16 +00:00
|
|
|
def _det(a: Point, b: Point, c: Point) -> float:
|
2019-08-17 15:36:31 +00:00
|
|
|
"""
|
|
|
|
Computes the sign perpendicular distance of a 2d point c from a line segment
|
|
|
|
ab. The sign indicates the direction of c relative to ab.
|
|
|
|
A Positive value means c is above ab (to the left), while a negative value
|
|
|
|
means c is below ab (to the right). 0 means all three points are on a straight line.
|
|
|
|
|
|
|
|
As a side note, 0.5 * abs|det| is the area of triangle abc
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
a: point, the point on the left end of line segment ab
|
|
|
|
b: point, the point on the right end of line segment ab
|
|
|
|
c: point, the point for which the direction and location is desired.
|
|
|
|
|
|
|
|
Returns
|
|
|
|
--------
|
|
|
|
det: float, abs(det) is the distance of c from ab. The sign
|
|
|
|
indicates which side of line segment ab c is. det is computed as
|
|
|
|
(a_xb_y + c_xa_y + b_xc_y) - (a_yb_x + c_ya_x + b_yc_x)
|
|
|
|
|
|
|
|
Examples
|
|
|
|
----------
|
|
|
|
>>> _det(Point(1, 1), Point(1, 2), Point(1, 5))
|
2019-12-08 22:15:17 +00:00
|
|
|
0.0
|
2019-08-17 15:36:31 +00:00
|
|
|
>>> _det(Point(0, 0), Point(10, 0), Point(0, 10))
|
2019-12-08 22:15:17 +00:00
|
|
|
100.0
|
2019-08-17 15:36:31 +00:00
|
|
|
>>> _det(Point(0, 0), Point(10, 0), Point(0, -10))
|
2019-12-08 22:15:17 +00:00
|
|
|
-100.0
|
2019-08-17 15:36:31 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
det = (a.x * b.y + b.x * c.y + c.x * a.y) - (a.y * b.x + b.y * c.x + c.y * a.x)
|
|
|
|
return det
|
|
|
|
|
|
|
|
|
2021-09-07 11:37:03 +00:00
|
|
|
def convex_hull_bf(points: list[Point]) -> list[Point]:
|
2019-08-17 15:36:31 +00:00
|
|
|
"""
|
|
|
|
Constructs the convex hull of a set of 2D points using a brute force algorithm.
|
|
|
|
The algorithm basically considers all combinations of points (i, j) and uses the
|
2020-06-16 08:09:19 +00:00
|
|
|
definition of convexity to determine whether (i, j) is part of the convex hull or
|
|
|
|
not. (i, j) is part of the convex hull if and only iff there are no points on both
|
|
|
|
sides of the line segment connecting the ij, and there is no point k such that k is
|
|
|
|
on either end of the ij.
|
2019-08-17 15:36:31 +00:00
|
|
|
|
|
|
|
Runtime: O(n^3) - definitely horrible
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
---------
|
|
|
|
points: array-like of object of Points, lists or tuples.
|
|
|
|
The set of 2d points for which the convex-hull is needed
|
|
|
|
|
|
|
|
Returns
|
|
|
|
------
|
|
|
|
convex_set: list, the convex-hull of points sorted in non-decreasing order.
|
|
|
|
|
|
|
|
See Also
|
|
|
|
--------
|
|
|
|
convex_hull_recursive,
|
|
|
|
|
|
|
|
Examples
|
|
|
|
---------
|
|
|
|
>>> convex_hull_bf([[0, 0], [1, 0], [10, 1]])
|
2019-12-08 22:15:17 +00:00
|
|
|
[(0.0, 0.0), (1.0, 0.0), (10.0, 1.0)]
|
2019-08-17 15:36:31 +00:00
|
|
|
>>> convex_hull_bf([[0, 0], [1, 0], [10, 0]])
|
2019-12-08 22:15:17 +00:00
|
|
|
[(0.0, 0.0), (10.0, 0.0)]
|
2020-06-16 08:09:19 +00:00
|
|
|
>>> convex_hull_bf([[-1, 1],[-1, -1], [0, 0], [0.5, 0.5], [1, -1], [1, 1],
|
|
|
|
... [-0.75, 1]])
|
2019-12-08 22:15:17 +00:00
|
|
|
[(-1.0, -1.0), (-1.0, 1.0), (1.0, -1.0), (1.0, 1.0)]
|
2020-06-16 08:09:19 +00:00
|
|
|
>>> convex_hull_bf([(0, 3), (2, 2), (1, 1), (2, 1), (3, 0), (0, 0), (3, 3),
|
|
|
|
... (2, -1), (2, -4), (1, -3)])
|
2019-12-08 22:15:17 +00:00
|
|
|
[(0.0, 0.0), (0.0, 3.0), (1.0, -3.0), (2.0, -4.0), (3.0, 0.0), (3.0, 3.0)]
|
2019-08-17 15:36:31 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
points = sorted(_validate_input(points))
|
|
|
|
n = len(points)
|
|
|
|
convex_set = set()
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
for i in range(n - 1):
|
2019-08-17 15:36:31 +00:00
|
|
|
for j in range(i + 1, n):
|
|
|
|
points_left_of_ij = points_right_of_ij = False
|
|
|
|
ij_part_of_convex_hull = True
|
|
|
|
for k in range(n):
|
|
|
|
if k != i and k != j:
|
|
|
|
det_k = _det(points[i], points[j], points[k])
|
|
|
|
|
|
|
|
if det_k > 0:
|
|
|
|
points_left_of_ij = True
|
|
|
|
elif det_k < 0:
|
|
|
|
points_right_of_ij = True
|
|
|
|
else:
|
|
|
|
# point[i], point[j], point[k] all lie on a straight line
|
|
|
|
# if point[k] is to the left of point[i] or it's to the
|
|
|
|
# right of point[j], then point[i], point[j] cannot be
|
|
|
|
# part of the convex hull of A
|
|
|
|
if points[k] < points[i] or points[k] > points[j]:
|
|
|
|
ij_part_of_convex_hull = False
|
|
|
|
break
|
|
|
|
|
|
|
|
if points_left_of_ij and points_right_of_ij:
|
|
|
|
ij_part_of_convex_hull = False
|
|
|
|
break
|
|
|
|
|
|
|
|
if ij_part_of_convex_hull:
|
|
|
|
convex_set.update([points[i], points[j]])
|
|
|
|
|
|
|
|
return sorted(convex_set)
|
|
|
|
|
|
|
|
|
2021-09-07 11:37:03 +00:00
|
|
|
def convex_hull_recursive(points: list[Point]) -> list[Point]:
|
2019-08-17 15:36:31 +00:00
|
|
|
"""
|
|
|
|
Constructs the convex hull of a set of 2D points using a divide-and-conquer strategy
|
2020-06-16 08:09:19 +00:00
|
|
|
The algorithm exploits the geometric properties of the problem by repeatedly
|
|
|
|
partitioning the set of points into smaller hulls, and finding the convex hull of
|
|
|
|
these smaller hulls. The union of the convex hull from smaller hulls is the
|
|
|
|
solution to the convex hull of the larger problem.
|
2019-08-17 15:36:31 +00:00
|
|
|
|
|
|
|
Parameter
|
|
|
|
---------
|
|
|
|
points: array-like of object of Points, lists or tuples.
|
|
|
|
The set of 2d points for which the convex-hull is needed
|
|
|
|
|
|
|
|
Runtime: O(n log n)
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
convex_set: list, the convex-hull of points sorted in non-decreasing order.
|
|
|
|
|
|
|
|
Examples
|
|
|
|
---------
|
|
|
|
>>> convex_hull_recursive([[0, 0], [1, 0], [10, 1]])
|
2019-12-08 22:15:17 +00:00
|
|
|
[(0.0, 0.0), (1.0, 0.0), (10.0, 1.0)]
|
2019-08-17 15:36:31 +00:00
|
|
|
>>> convex_hull_recursive([[0, 0], [1, 0], [10, 0]])
|
2019-12-08 22:15:17 +00:00
|
|
|
[(0.0, 0.0), (10.0, 0.0)]
|
2020-06-16 08:09:19 +00:00
|
|
|
>>> convex_hull_recursive([[-1, 1],[-1, -1], [0, 0], [0.5, 0.5], [1, -1], [1, 1],
|
Fix long line, tests (#2123)
* Fix long line
* updating DIRECTORY.md
* Add doctest
* ...
* ...
* Update tabu_search.py
* space
* Fix doctest
>>> find_neighborhood(['a','c','b','d','e','a']) # doctest: +NORMALIZE_WHITESPACE
[['a','e','b','d','c','a',90], [['a','c','d','b','e','a',90],
['a','d','b','c','e','a',93], ['a','c','b','e','d','a',102],
['a','c','e','d','b','a',113], ['a','b','c','d','e','a',93]]
Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
Co-authored-by: John Law <johnlaw.po@gmail.com>
2020-06-16 12:29:13 +00:00
|
|
|
... [-0.75, 1]])
|
2019-12-08 22:15:17 +00:00
|
|
|
[(-1.0, -1.0), (-1.0, 1.0), (1.0, -1.0), (1.0, 1.0)]
|
2020-06-16 08:09:19 +00:00
|
|
|
>>> convex_hull_recursive([(0, 3), (2, 2), (1, 1), (2, 1), (3, 0), (0, 0), (3, 3),
|
Fix long line, tests (#2123)
* Fix long line
* updating DIRECTORY.md
* Add doctest
* ...
* ...
* Update tabu_search.py
* space
* Fix doctest
>>> find_neighborhood(['a','c','b','d','e','a']) # doctest: +NORMALIZE_WHITESPACE
[['a','e','b','d','c','a',90], [['a','c','d','b','e','a',90],
['a','d','b','c','e','a',93], ['a','c','b','e','d','a',102],
['a','c','e','d','b','a',113], ['a','b','c','d','e','a',93]]
Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
Co-authored-by: John Law <johnlaw.po@gmail.com>
2020-06-16 12:29:13 +00:00
|
|
|
... (2, -1), (2, -4), (1, -3)])
|
2019-12-08 22:15:17 +00:00
|
|
|
[(0.0, 0.0), (0.0, 3.0), (1.0, -3.0), (2.0, -4.0), (3.0, 0.0), (3.0, 3.0)]
|
2019-08-17 15:36:31 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
points = sorted(_validate_input(points))
|
|
|
|
n = len(points)
|
|
|
|
|
|
|
|
# divide all the points into an upper hull and a lower hull
|
|
|
|
# the left most point and the right most point are definitely
|
|
|
|
# members of the convex hull by definition.
|
|
|
|
# use these two anchors to divide all the points into two hulls,
|
|
|
|
# an upper hull and a lower hull.
|
|
|
|
|
2020-06-16 08:09:19 +00:00
|
|
|
# all points to the left (above) the line joining the extreme points belong to the
|
|
|
|
# upper hull
|
|
|
|
# all points to the right (below) the line joining the extreme points below to the
|
|
|
|
# lower hull
|
|
|
|
# ignore all points on the line joining the extreme points since they cannot be
|
|
|
|
# part of the convex hull
|
2019-08-17 15:36:31 +00:00
|
|
|
|
|
|
|
left_most_point = points[0]
|
2019-10-05 05:14:13 +00:00
|
|
|
right_most_point = points[n - 1]
|
2019-08-17 15:36:31 +00:00
|
|
|
|
|
|
|
convex_set = {left_most_point, right_most_point}
|
2020-03-04 12:40:28 +00:00
|
|
|
upper_hull = []
|
|
|
|
lower_hull = []
|
2019-08-17 15:36:31 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
for i in range(1, n - 1):
|
2019-08-17 15:36:31 +00:00
|
|
|
det = _det(left_most_point, right_most_point, points[i])
|
|
|
|
|
|
|
|
if det > 0:
|
2020-03-04 12:40:28 +00:00
|
|
|
upper_hull.append(points[i])
|
2019-08-17 15:36:31 +00:00
|
|
|
elif det < 0:
|
2020-03-04 12:40:28 +00:00
|
|
|
lower_hull.append(points[i])
|
2019-08-17 15:36:31 +00:00
|
|
|
|
2020-03-04 12:40:28 +00:00
|
|
|
_construct_hull(upper_hull, left_most_point, right_most_point, convex_set)
|
|
|
|
_construct_hull(lower_hull, right_most_point, left_most_point, convex_set)
|
2019-08-17 15:36:31 +00:00
|
|
|
|
|
|
|
return sorted(convex_set)
|
|
|
|
|
|
|
|
|
2020-10-29 00:46:16 +00:00
|
|
|
def _construct_hull(
|
2021-09-07 11:37:03 +00:00
|
|
|
points: list[Point], left: Point, right: Point, convex_set: set[Point]
|
2020-10-29 00:46:16 +00:00
|
|
|
) -> None:
|
2019-08-17 15:36:31 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
---------
|
2020-06-16 08:09:19 +00:00
|
|
|
points: list or None, the hull of points from which to choose the next convex-hull
|
|
|
|
point
|
2019-08-17 15:36:31 +00:00
|
|
|
left: Point, the point to the left of line segment joining left and right
|
|
|
|
right: The point to the right of the line segment joining left and right
|
2020-06-16 08:09:19 +00:00
|
|
|
convex_set: set, the current convex-hull. The state of convex-set gets updated by
|
|
|
|
this function
|
2019-08-17 15:36:31 +00:00
|
|
|
|
|
|
|
Note
|
|
|
|
----
|
|
|
|
For the line segment 'ab', 'a' is on the left and 'b' on the right.
|
|
|
|
but the reverse is true for the line segment 'ba'.
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
Nothing, only updates the state of convex-set
|
|
|
|
"""
|
|
|
|
if points:
|
|
|
|
extreme_point = None
|
2019-10-05 05:14:13 +00:00
|
|
|
extreme_point_distance = float("-inf")
|
2019-08-17 15:36:31 +00:00
|
|
|
candidate_points = []
|
|
|
|
|
|
|
|
for p in points:
|
|
|
|
det = _det(left, right, p)
|
|
|
|
|
|
|
|
if det > 0:
|
|
|
|
candidate_points.append(p)
|
|
|
|
|
|
|
|
if det > extreme_point_distance:
|
|
|
|
extreme_point_distance = det
|
|
|
|
extreme_point = p
|
|
|
|
|
|
|
|
if extreme_point:
|
|
|
|
_construct_hull(candidate_points, left, extreme_point, convex_set)
|
|
|
|
convex_set.add(extreme_point)
|
|
|
|
_construct_hull(candidate_points, extreme_point, right, convex_set)
|
|
|
|
|
|
|
|
|
2021-09-07 11:37:03 +00:00
|
|
|
def convex_hull_melkman(points: list[Point]) -> list[Point]:
|
2020-10-29 00:46:16 +00:00
|
|
|
"""
|
|
|
|
Constructs the convex hull of a set of 2D points using the melkman algorithm.
|
|
|
|
The algorithm works by iteratively inserting points of a simple polygonal chain
|
|
|
|
(meaning that no line segments between two consecutive points cross each other).
|
|
|
|
Sorting the points yields such a polygonal chain.
|
|
|
|
|
|
|
|
For a detailed description, see http://cgm.cs.mcgill.ca/~athens/cs601/Melkman.html
|
|
|
|
|
|
|
|
Runtime: O(n log n) - O(n) if points are already sorted in the input
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
---------
|
|
|
|
points: array-like of object of Points, lists or tuples.
|
|
|
|
The set of 2d points for which the convex-hull is needed
|
|
|
|
|
|
|
|
Returns
|
|
|
|
------
|
|
|
|
convex_set: list, the convex-hull of points sorted in non-decreasing order.
|
|
|
|
|
|
|
|
See Also
|
|
|
|
--------
|
|
|
|
|
|
|
|
Examples
|
|
|
|
---------
|
|
|
|
>>> convex_hull_melkman([[0, 0], [1, 0], [10, 1]])
|
|
|
|
[(0.0, 0.0), (1.0, 0.0), (10.0, 1.0)]
|
|
|
|
>>> convex_hull_melkman([[0, 0], [1, 0], [10, 0]])
|
|
|
|
[(0.0, 0.0), (10.0, 0.0)]
|
|
|
|
>>> convex_hull_melkman([[-1, 1],[-1, -1], [0, 0], [0.5, 0.5], [1, -1], [1, 1],
|
|
|
|
... [-0.75, 1]])
|
|
|
|
[(-1.0, -1.0), (-1.0, 1.0), (1.0, -1.0), (1.0, 1.0)]
|
|
|
|
>>> convex_hull_melkman([(0, 3), (2, 2), (1, 1), (2, 1), (3, 0), (0, 0), (3, 3),
|
|
|
|
... (2, -1), (2, -4), (1, -3)])
|
|
|
|
[(0.0, 0.0), (0.0, 3.0), (1.0, -3.0), (2.0, -4.0), (3.0, 0.0), (3.0, 3.0)]
|
|
|
|
"""
|
|
|
|
points = sorted(_validate_input(points))
|
|
|
|
n = len(points)
|
|
|
|
|
|
|
|
convex_hull = points[:2]
|
|
|
|
for i in range(2, n):
|
|
|
|
det = _det(convex_hull[1], convex_hull[0], points[i])
|
|
|
|
if det > 0:
|
|
|
|
convex_hull.insert(0, points[i])
|
|
|
|
break
|
|
|
|
elif det < 0:
|
|
|
|
convex_hull.append(points[i])
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
convex_hull[1] = points[i]
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
for i in range(i, n):
|
|
|
|
if (
|
|
|
|
_det(convex_hull[0], convex_hull[-1], points[i]) > 0
|
|
|
|
and _det(convex_hull[-1], convex_hull[0], points[1]) < 0
|
|
|
|
):
|
|
|
|
# The point lies within the convex hull
|
|
|
|
continue
|
|
|
|
|
|
|
|
convex_hull.insert(0, points[i])
|
|
|
|
convex_hull.append(points[i])
|
|
|
|
while _det(convex_hull[0], convex_hull[1], convex_hull[2]) >= 0:
|
|
|
|
del convex_hull[1]
|
|
|
|
while _det(convex_hull[-1], convex_hull[-2], convex_hull[-3]) <= 0:
|
|
|
|
del convex_hull[-2]
|
|
|
|
|
|
|
|
# `convex_hull` is contains the convex hull in circular order
|
|
|
|
return sorted(convex_hull[1:] if len(convex_hull) > 3 else convex_hull)
|
|
|
|
|
|
|
|
|
2019-08-17 15:36:31 +00:00
|
|
|
def main():
|
2019-10-05 05:14:13 +00:00
|
|
|
points = [
|
|
|
|
(0, 3),
|
|
|
|
(2, 2),
|
|
|
|
(1, 1),
|
|
|
|
(2, 1),
|
|
|
|
(3, 0),
|
|
|
|
(0, 0),
|
|
|
|
(3, 3),
|
|
|
|
(2, -1),
|
|
|
|
(2, -4),
|
|
|
|
(1, -3),
|
|
|
|
]
|
2019-08-17 15:36:31 +00:00
|
|
|
# the convex set of points is
|
|
|
|
# [(0, 0), (0, 3), (1, -3), (2, -4), (3, 0), (3, 3)]
|
|
|
|
results_bf = convex_hull_bf(points)
|
2020-10-29 00:46:16 +00:00
|
|
|
|
|
|
|
results_recursive = convex_hull_recursive(points)
|
2019-08-17 15:36:31 +00:00
|
|
|
assert results_bf == results_recursive
|
|
|
|
|
2020-10-29 00:46:16 +00:00
|
|
|
results_melkman = convex_hull_melkman(points)
|
|
|
|
assert results_bf == results_melkman
|
|
|
|
|
2019-08-17 15:36:31 +00:00
|
|
|
print(results_bf)
|
|
|
|
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
if __name__ == "__main__":
|
2019-08-17 15:36:31 +00:00
|
|
|
main()
|