Skip to Content

Convex Hull with the Graham Scan Algorithm in Java

25 April 2026 by
TechStora

Introduction to the Convex Hull Problem

The convex hull is a fundamental concept in computational geometry. It involves determining the smallest convex region that encompasses a set of scattered points in a two-dimensional plane. To visualize this, imagine stretching a rubber band around a cluster of points on graph paper. The points touched by the rubber band form the convex hull, while those inside remain excluded. This problem has numerous applications, ranging from computer graphics to geographic information systems.

One of the most widely used methods to solve this problem is the Graham Scan algorithm. It is particularly effective due to its balance of simplicity and efficiency. By leveraging the inherent properties of points and their angular relationships, the algorithm systematically identifies the outer boundary of the convex hull with minimal computational overhead.

How the Graham Scan Algorithm Works

The Graham Scan algorithm begins with a two-step process to set the stage for efficient computation. First, it selects a single starting point, known as the anchor point. This point acts as the reference for the rest of the operations. Second, the algorithm orders the remaining points based on how they rotate around the anchor. This sorting step ensures that subsequent operations can proceed in a logical sequence.

Once the points are sorted, the algorithm performs a counterclockwise sweep through the set. At each step, it evaluates the turns made by consecutive points. Only those turns that contribute to the convex boundary are retained, while others are discarded. This ensures that the resulting hull is both minimal and convex.

Choosing the Anchor Point

The choice of the anchor point is a critical aspect of the Graham Scan. The algorithm typically selects the point with the lowest y-coordinate, as this ensures a consistent starting position. If multiple points share the same y-coordinate, the one with the lowest x-coordinate is chosen to break the tie. This approach places the anchor near the bottom-left corner of the point set, providing a reliable reference for subsequent steps.

For example, consider the coordinates (1,1), (2,5), (4,6), (7,4), (6,1), (4,3), and (3,2). Among these, (1,1) is selected as the anchor because it has the smallest y-coordinate. Although (6,1) shares the same height, its x-coordinate is larger, making (1,1) the preferred choice.

Sorting Points by Angular Order

Once the anchor point is determined, the next step is to sort the remaining points. This sorting is based on the angle each point makes with the anchor and the horizontal axis. Points are arranged in an increasing order of these angles, ensuring a counterclockwise traversal during the scan.

This angular sorting guarantees that each point is visited in the correct sequence. Without this step, the algorithm would struggle to maintain the convexity of the hull. The computational overhead of this sorting process is typically the most time-consuming aspect of the Graham Scan, with a complexity of O(n log n).

Efficiently Scanning the Point Set

After sorting, the algorithm performs the actual scan to construct the convex hull. Starting from the anchor, it evaluates each point in the sorted list. At each step, the algorithm checks whether the turn formed by three consecutive points is a left turn or a right turn. Left turns are retained as part of the hull, while right turns are discarded.

This iterative process ensures that only the outermost points remain in the final hull. The scanning phase is highly efficient, operating in linear time, or O(n). Together with the sorting phase, the overall complexity of the Graham Scan is O(n log n), making it suitable for moderately large datasets.

Applications and Benefits

The Graham Scan algorithm has practical applications across various fields. In computer graphics, it aids in rendering convex shapes and bounding regions. In robotics, it helps in path planning and obstacle avoidance. Its efficiency and simplicity make it a preferred choice for solving convex hull problems in real-world scenarios.

Beyond its practical uses, the algorithm also illustrates key principles of computational geometry. By combining geometric reasoning with efficient computation, it serves as a robust example of how theoretical concepts can address practical challenges. Understanding its workings provides valuable insights into both algorithm design and problem-solving strategies.