|
15 | 15 |
|
16 | 16 | from typing import Iterable, List, Set, Union
|
17 | 17 |
|
| 18 | + |
18 | 19 | class Point:
|
19 | 20 | """
|
20 | 21 | Defines a 2-d point for use by all convex-hull algorithms.
|
@@ -82,7 +83,9 @@ def __hash__(self):
|
82 | 83 | return hash(self.x)
|
83 | 84 |
|
84 | 85 |
|
85 |
| -def _construct_points(list_of_tuples: Union[List[Point], List[List[float]], Iterable[List[float]]]) -> List[Point]: |
| 86 | +def _construct_points( |
| 87 | + list_of_tuples: Union[List[Point], List[List[float]], Iterable[List[float]]] |
| 88 | +) -> List[Point]: |
86 | 89 | """
|
87 | 90 | constructs a list of points from an array-like object of numbers
|
88 | 91 |
|
@@ -111,19 +114,18 @@ def _construct_points(list_of_tuples: Union[List[Point], List[List[float]], Iter
|
111 | 114 | []
|
112 | 115 | """
|
113 | 116 |
|
114 |
| - points = [] |
115 |
| - if list_of_tuples: |
116 |
| - for p in list_of_tuples: |
117 |
| - if isinstance(p, Point): |
118 |
| - points.append(p) |
119 |
| - else: |
120 |
| - try: |
121 |
| - points.append(Point(p[0], p[1])) |
122 |
| - except (IndexError, TypeError): |
123 |
| - print( |
124 |
| - f"Ignoring deformed point {p}. All points" |
125 |
| - " must have at least 2 coordinates." |
126 |
| - ) |
| 117 | + points: List[Point] = [] |
| 118 | + for p in list_of_tuples: |
| 119 | + if isinstance(p, Point): |
| 120 | + points.append(p) |
| 121 | + else: |
| 122 | + try: |
| 123 | + points.append(Point(p[0], p[1])) |
| 124 | + except (IndexError, TypeError): |
| 125 | + print( |
| 126 | + f"Ignoring deformed point {p}. All points" |
| 127 | + " must have at least 2 coordinates." |
| 128 | + ) |
127 | 129 | return points
|
128 | 130 |
|
129 | 131 |
|
@@ -169,10 +171,15 @@ def _validate_input(points: Union[List[Point], List[List[float]]]) -> List[Point
|
169 | 171 | ValueError: Expecting an iterable object but got an non-iterable type 1
|
170 | 172 | """
|
171 | 173 |
|
| 174 | + if not hasattr(points, "__iter__"): |
| 175 | + raise ValueError( |
| 176 | + f"Expecting an iterable object but got an non-iterable type {points}" |
| 177 | + ) |
| 178 | + |
172 | 179 | if not points:
|
173 | 180 | raise ValueError(f"Expecting a list of points but got {points}")
|
174 | 181 |
|
175 |
| - return _construct_points(points) |
| 182 | + return _construct_points(points) |
176 | 183 |
|
177 | 184 |
|
178 | 185 | def _det(a: Point, b: Point, c: Point) -> float:
|
@@ -353,7 +360,9 @@ def convex_hull_recursive(points: List[Point]) -> List[Point]:
|
353 | 360 | return sorted(convex_set)
|
354 | 361 |
|
355 | 362 |
|
356 |
| -def _construct_hull(points: List[Point], left: Point, right: Point, convex_set: Set[Point]) -> None: |
| 363 | +def _construct_hull( |
| 364 | + points: List[Point], left: Point, right: Point, convex_set: Set[Point] |
| 365 | +) -> None: |
357 | 366 | """
|
358 | 367 |
|
359 | 368 | Parameters
|
|
0 commit comments