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 Gift Wrapping Algorithm in Two Dimensions
Gift Wrapping Algorithm
The Gift Wrapping algorithm is also known as Jarvis's march. It is a method for calculating the convex hull of a set of points in a plane. It is essential to find the smallest convex polygon that encloses all the points.
Why We Use Gift Wrapping Algorithm?
Below are the following reasons to use this algorithm:
- Easy to Understand: It work like wrapping a string around points.
- Good for Small Data Sets: When fewer points make up the convex hull.
- Accurate: Never misses a point in the convex hull.
- Useful in Graphics & Robotic: Helps in pathfinding and object detection.
- Works in 3D too: It can be adapted for three-dimensional shapes.
Pseudocode of Gift Wrapping Algorithm
Following is the pseudocode of the gift-wrapping algorithm:
Algorithm Jarvis(S)
Input: S = set of points
Output: P = list of convex hull points in counter-clockwise order
pointOnHull := leftmost point in S
i := 0
repeat
P[i] := pointOnHull
endpoint := S[0]
for each point S[j] in S do
if (endpoint == pointOnHull) or (S[j] is to the left of the line from P[i] to endpoint) then
endpoint := S[j]
i := i + 1
pointOnHull := endpoint
until endpoint == P[0] // completed the hull
return P
Explanations
Leftmost point: The point that is guarantees to be a part of the convex hull and has smallest x-coordinate (and smallest y if there is a tie).
"is to the left": Makes sure we wrap around the outer edge of the points and responds to the orientation test (cross product > 0).
The algorithm iterates around the outermost points, choosing the "most counter-clockwise" point at each step until it reaches the starting point again.
C++ Implementation of Gift-wrapping Algorithm in Two Dimensions
Following is the C++ implementation of the gift-wrapping algorithm in two dimensions:
#include <iostream>
using namespace std;
#define INF 10000
struct P {
int x;
int y;
};
int orient(P a, P b, P c) {
int v = (b.y - a.y) * (c.x - b.x) - (b.x - a.x) * (c.y - b.y);
if (v == 0)
return 0; // colinear
return (v > 0) ? 1 : 2; // clock or counterclock wise
}
void convexHull(P points[], int m) {
if (m < 3) //at least three points required
return;
int n[m];
for (int i = 0; i < m; i++)
n[i] = -1;
int l = 0; //initialize result.
for (int i = 1; i < m; i++)
if (points[i].x < points[l].x)
l = i; //find left most point
int p = l, q;
do {
q = (p + 1) % m;
for (int i = 0; i < m; i++)
if (orient(points[p], points[i], points[q]) == 2)
q = i;
n[p] = q;
p = q;
} while (p != l);
for (int i = 0; i < m; i++) {
if (n[i] != -1)
cout << "(" << points[i].x << ", " << points[i].y << ")\n";
}
}
int main() {
P points[] = {
{0, 4}, {2, 1}, {2, 3}, {4, 1}, {3, 0}, {1, 1}, {7, 6}
};
cout << "The points in the convex hull are: ";
int n = sizeof(points) / sizeof(points[0]);
convexHull(points, n);
return 0;
}
Following is the output of the above code:
The points in the convex hull are: (0, 4) (4, 1) (3, 0) (1, 1) (7, 6)