Convex Hull (Jarvis March)

beginner
14 min

Convex Hull (Jarvis March)

Welcome to our deep dive into one of the fascinating topics in computer science – Convex Hull! In this lesson, we'll explore the Convex Hull problem and learn about the Jarvis March algorithm, a popular and efficient method to solve it. Let's get started!

What is Convex Hull?

A convex hull is the smallest convex polygon that can enclose a set of points in a two-dimensional plane. In simpler terms, it's a shape created by connecting the extreme points of a given set of points, such that the resulting shape is always convex (the inside is never concave).

šŸ’” Pro Tip: A polygon is considered convex if every line segment connecting any two points in the polygon lies entirely within the polygon.

The Jarvis March Algorithm

The Jarvis March (also known as the Gift Wrapping Algorithm) is a simple and efficient method to find the convex hull of a set of points. The algorithm works by finding the extreme points on the boundary of the convex hull and connecting them in a counterclockwise order.

Here's how it works:

  1. Choose a point from the set of points as the initial point (usually the leftmost point).
  2. Find the extreme right point (highest y-coordinate) among the remaining points.
  3. Draw a line from the initial point to the rightmost point, and find the point where this line intersects the convex hull (let's call it ch_right).
  4. Repeat the following steps for ch_right:
    • Find the extreme left point (lowest y-coordinate) among the remaining points.
    • Draw a line from ch_right to the leftmost point, and find the point where this line intersects the convex hull (let's call it ch_left).
    • Check if the new point ch_left lies on the opposite side of the line connecting ch_right and the current point on the convex hull. If it does, replace the current point on the convex hull with ch_left.
  5. Once all points in the set have been processed, the remaining points on the convex hull form a convex polygon.

šŸ“ Note: The Jarvis March algorithm is guaranteed to find the convex hull in a counterclockwise direction for a two-dimensional plane.

Python Code Example

Now, let's implement the Jarvis March algorithm in Python:

python
def convex_hull(points): sorted_points = sorted(points, key=lambda x: x[1]) stack = [sorted_points[0], sorted_points[-1]] for p in sorted_points: while len(stack) > 1 and orient(stack[-1], stack[-2], p) < 0: stack.pop() stack.append(p) return stack def orient(a, b, c): val = (b[0] - a[0]) * (c[1] - b[1]) - (b[1] - a[1]) * (c[0] - b[0]) if val > 0: return 1 elif val < 0: return -1 else: return 0

šŸŽÆ Quiz Time!

Quick Quiz
Question 1 of 1

What is the convex hull of a set of points?

Summary

In this lesson, we explored the fascinating topic of Convex Hull and the Jarvis March algorithm, which is a simple yet powerful method to find the convex hull of a set of points. By following the steps outlined, you'll be able to implement the Jarvis March algorithm in your own projects and solutions. Happy coding! šŸ¤–šŸ’»šŸŒŸ