
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Convex Hull Jarvis’s Algorithm or Wrapping in C++
In this tutorial, we will be discussing a program to find the convex hull of a given set of points using Jarvis’s Algorithm.
Convex hull is the smallest polygon convex figure containing all the given points either on the boundary on inside the figure.
In Jarvis’s algorithm, we select the leftmost point and keep wrapping points moving in the clockwise direction.
Example
#include <bits/stdc++.h> using namespace std; //structure of the point struct Point{ int x, y; }; //calculating the position of the points int cal_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; //collinear return (val > 0)? 1: 2; //clock or counterclockwise } //printing convex hull void convexHull(Point points[], int n){ if (n < 3) return; vector<Point> hull; //calculating the leftmost point int l = 0; for (int i = 1; i < n; i++) if (points[i].x < points[l].x) l = i; //moving in the clockwise direction int p = l, q; do{ //adding current point to result hull.push_back(points[p]); q = (p+1)%n; for (int i = 0; i < n; i++){ if (cal_orientation(points[p], points[i], points[q]) == 2) q = i; } p = q; } while (p != l); //if didn't reached the first point for (int i = 0; i < hull.size(); i++) cout << "(" << hull[i].x << ", " << hull[i].y << ")\n"; } int main(){ Point points[] = {{0, 3}, {2, 2}, {1, 1}, {2, 1}, {3, 0}, {0, 0}, {3, 3}}; int n = sizeof(points)/sizeof(points[0]); convexHull(points, n); return 0; }
Output
(0, 3) (0, 0) (3, 0) (3, 3)
- Related Articles
- C++ Program to Implement Jarvis March to Find the Convex Hull
- Convex Hull Monotone chain algorithm in C++
- Convex Hull using Divide and Conquer Algorithm in C++
- Jarvis March Algorithm\n
- C++ Program to Implement Graham Scan Algorithm to Find the Convex Hull
- Convex Hull Example in Data Structures
- Convex Hull Graham Scan in C++
- Program to check points are forming convex hull or not in Python
- C++ Program to implement Gift Wrapping Algorithm in Two Dimensions
- How to find and draw Convex Hull of an image contour in OpenCV Python?
- Berkeley's Algorithm
- Dijkstra's algorithm in Javascript
- Prim's algorithm in Javascript
- Kruskal's algorithm in Javascript
- Kruskal's Minimum Spanning Tree Algorithm-Greedy algorithm in C++

Advertisements