Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Implement Jarvis March to Find the Convex Hull
A convex hull is the smallest convex polygon with maximum area and minimum perimeter that encloses all the given points in a 2D plane. In this article, we will learn how to write C++ program to implement Jarvis March Algorithm to find a convex hull.
The objective of this problem is to take a set of x and y coordinates of a 2d plane as input, and display coordinate point from the set which are part of convex hull.
// Input
Set of points: {0, 0}, {1, 1}, {2, 2}, {2, 0},{1, 2}, {0, 2}
// Output
Boundary Points in Convex Hull:(0, 0)(2, 0)(2, 2)(0, 2)
Before moving to Jarvis March Algorithm, first of all let understand convex hull with an example shown below.
Example of Convex Hull
For a given points in a 2D plane, convex hull is a polygon drawn through the outermost points. The convex hull should cover maximum area, while taking minimum perimeter. The image below shows an example of convex hull polygon and a non convex hull polygon.
Overview of Jarvis March Algorithm
Jarvis March Algorithm is also known as Gift Wrapping Algorithm, used to find the convex hull of a set of points in a 2D plane. The algorithm works by:
- Start from the leftmost point in the plane.
- Wrap around the set of points by choosing the next point such that all other points are to the right of the line segment.
- Repeat the process until we return to the starting point.
Steps to Find Convex Hull Using Jarvis March Algorithm
Following are steps to find convex hull using Jarvis March Algorithm:
- Step 1: Find the leftmost point among the given points. This point must be part of the convex hull.
- Step 2: Initialize the current point as the leftmost point and add it to the result.
- Step 3: Search for a point such that all the remaining points are to the right of the line formed from current point to that point.
- Step 4: Set this new point as the current point and add it to the convex hull.
- Step 5: Repeat step 3 and 4 until we reach back to the starting leftmost point.
- Step 6: The points selected during this process form the convex hull in counter-clockwise order.
Example Code
The code below is C++ implement of Jarvis March Algorithm. It takes geometrical points into a vector of vector as input from user and outputs the convex hull for the points in the same format.
#include <iostream>
#include <vector>
using namespace std;
struct Point {
int x, y;
};
// Function to find orientation
int orientation(Point p, Point q, Point r) {
int val = (q.y - p.y) * (r.x - q.x) -
(q.x - p.x) * (r.y - q.y);
if (val == 0) return 0; // colinear
return (val > 0) ? 1 : 2; // clock or counterclockwise
}
// Function to implement Jarvis March
void jarvisMarch(vector<Point>& points) {
int n = points.size();
if (n < 3) {
cout << "Convex hull not possible.\n";
return;
}
vector<Point> hull;
// Find the leftmost point
int l = 0;
for (int i = 1; i < n; i++) {
if (points[i].x < points[l].x)
l = i;
}
int p = l, q;
do {
hull.push_back(points[p]);
q = (p + 1) % n;
for (int i = 0; i < n; i++) {
if (orientation(points[p], points[i], points[q]) == 2)
q = i;
}
p = q;
} while (p != l);
// Print convex hull
cout << "Points in Convex Hull:\n";
for (auto& pt : hull)
cout << "(" << pt.x << ", " << pt.y << ")\n";
}
// Example usage
int main() {
vector<Point> points = {
{0, 0}, {1, 1}, {2, 2}, {2, 0},
{1, 2}, {0, 2}
};
jarvisMarch(points);
return 0;
}
The output of the above code will be:
Points in Convex Hull:(0, 0)(2, 0)(2, 2)(0, 2)
Complexity of Jarvis March Algorithm
- Time Complexity: O(nh) where n is number of input points and h is number of points in the convex hull.
- Space Complexity: O(n) Linear space is used to store the hull points.