Program to check points are forming convex hull or not in Python

Suppose we have outer points of a polygon in clockwise order. We have to check these points are forming a convex hull or not.

Convex Hull All interior angles ? 180° Interior Angle

From this diagram it is clear that for each three consecutive points the interior angle is not more than 180°. So if all angles are not more than 180° then the polygon is convex hull.

So, if the input is like points = [(3,4), (4,7),(7,8),(11,6),(12,3),(10,1),(5,2)], then the output will be True.

Algorithm

To solve this, we will follow these steps ?

  • n := size of points
  • for i in range 0 to size of points, do
    • p1 := points[i-2]
    • p2 := points[i-1]
    • p3 := points[i]
    • if angle between points (p1, p2, p3) > 180, then
      • return False
  • return True

Implementation

Let us see the following implementation to get better understanding ?

import math

def get_angle(a, b, c):
    angle = math.degrees(math.atan2(c[1]-b[1], c[0]-b[0]) - math.atan2(a[1]-b[1], a[0]-b[0]))
    return angle + 360 if angle < 0 else angle

def solve(points):
    n = len(points)
    for i in range(len(points)):
        p1 = points[i-2]
        p2 = points[i-1]
        p3 = points[i]
        if get_angle(p1, p2, p3) > 180:
            return False
    return True

points = [(3,4), (4,7),(7,8),(11,6),(12,3),(10,1),(5,2)]
print(solve(points))

The output of the above code is ?

True

How It Works

The algorithm checks each triplet of consecutive points to calculate the interior angle. The get_angle() function uses atan2() to compute the angle between two vectors formed by three points. If any interior angle exceeds 180°, the polygon is not convex.

Example with Non-Convex Points

Let's test with points that form a non-convex polygon ?

import math

def get_angle(a, b, c):
    angle = math.degrees(math.atan2(c[1]-b[1], c[0]-b[0]) - math.atan2(a[1]-b[1], a[0]-b[0]))
    return angle + 360 if angle < 0 else angle

def solve(points):
    n = len(points)
    for i in range(len(points)):
        p1 = points[i-2]
        p2 = points[i-1]
        p3 = points[i]
        if get_angle(p1, p2, p3) > 180:
            return False
    return True

# Non-convex polygon (has an inward dent)
non_convex_points = [(0,0), (4,0), (4,4), (2,2), (0,4)]
print("Non-convex result:", solve(non_convex_points))

# Convex polygon
convex_points = [(0,0), (4,0), (4,4), (0,4)]
print("Convex result:", solve(convex_points))
Non-convex result: False
Convex result: True

Conclusion

This algorithm efficiently checks if points form a convex hull by calculating interior angles between consecutive triplets. Any angle greater than 180° indicates a non-convex polygon, making this a reliable method for convex hull verification.

Updated on: 2026-03-26T15:48:06+05:30

881 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements