MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / convex_hull_recursive

Function convex_hull_recursive

divide_and_conquer/convex_hull.py:295–362  ·  view source on GitHub ↗

Constructs the convex hull of a set of 2D points using a divide-and-conquer strategy 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

(points: list[Point])

Source from the content-addressed store, hash-verified

293
294
295def convex_hull_recursive(points: list[Point]) -> list[Point]:
296 """
297 Constructs the convex hull of a set of 2D points using a divide-and-conquer strategy
298 The algorithm exploits the geometric properties of the problem by repeatedly
299 partitioning the set of points into smaller hulls, and finding the convex hull of
300 these smaller hulls. The union of the convex hull from smaller hulls is the
301 solution to the convex hull of the larger problem.
302
303 Parameter
304 ---------
305 points: array-like of object of Points, lists or tuples.
306 The set of 2d points for which the convex-hull is needed
307
308 Runtime: O(n log n)
309
310 Returns
311 -------
312 convex_set: list, the convex-hull of points sorted in non-decreasing order.
313
314 Examples
315 ---------
316 >>> convex_hull_recursive([[0, 0], [1, 0], [10, 1]])
317 [(0.0, 0.0), (1.0, 0.0), (10.0, 1.0)]
318 >>> convex_hull_recursive([[0, 0], [1, 0], [10, 0]])
319 [(0.0, 0.0), (10.0, 0.0)]
320 >>> convex_hull_recursive([[-1, 1],[-1, -1], [0, 0], [0.5, 0.5], [1, -1], [1, 1],
321 ... [-0.75, 1]])
322 [(-1.0, -1.0), (-1.0, 1.0), (1.0, -1.0), (1.0, 1.0)]
323 >>> convex_hull_recursive([(0, 3), (2, 2), (1, 1), (2, 1), (3, 0), (0, 0), (3, 3),
324 ... (2, -1), (2, -4), (1, -3)])
325 [(0.0, 0.0), (0.0, 3.0), (1.0, -3.0), (2.0, -4.0), (3.0, 0.0), (3.0, 3.0)]
326
327 """
328 points = sorted(_validate_input(points))
329 n = len(points)
330
331 # divide all the points into an upper hull and a lower hull
332 # the left most point and the right most point are definitely
333 # members of the convex hull by definition.
334 # use these two anchors to divide all the points into two hulls,
335 # an upper hull and a lower hull.
336
337 # all points to the left (above) the line joining the extreme points belong to the
338 # upper hull
339 # all points to the right (below) the line joining the extreme points below to the
340 # lower hull
341 # ignore all points on the line joining the extreme points since they cannot be
342 # part of the convex hull
343
344 left_most_point = points[0]
345 right_most_point = points[n - 1]
346
347 convex_set = {left_most_point, right_most_point}
348 upper_hull = []
349 lower_hull = []
350
351 for i in range(1, n - 1):
352 det = _det(left_most_point, right_most_point, points[i])

Callers 1

mainFunction · 0.85

Calls 4

_validate_inputFunction · 0.85
_detFunction · 0.85
_construct_hullFunction · 0.85
appendMethod · 0.45

Tested by

no test coverage detected