×

Convex hull Algorithm in C++

The intersection of all convex sets containing a certain subset of a Euclidean space, or alternatively, the set of all convex combinations of points in the subset, defines the convex hull. A rubber band wrapped around a bounded subset of the plane can be used to represent the convex hull as the shape it encloses. The algorithmic concerns of finding the convex hull of a constrained set of points in the plane or other low-dimensional Euclidean spaces, as well as its dual problem of intersecting half-spaces, are fundamental problems in computational geometry. In higher dimensions, they can be solved in a time that corresponds to the worst-case output complexity as stated by the upper limit theorem. They can be solved in time O(nlog n) for two- or three-dimensional point sets. In this article we are going to discuss about convex hull and its algorithm in detail.

What is Convex Hull?

If there are line segments connecting every pair of the points in a set of points in a Euclidean space, the set is said to be convex. The intersection of all half-spaces that contain a set of points S is the convex hull of that set of points. The collection of points on or to one side of a line constitutes a half space in two dimensions. Higher dimensions can be applied to this idea. The group of points on or to one side of a plane constitutes a half-space, and so on. Keep in mind that the convex hull of a set is a closed, "solid" area that contains all of the interior points. Since it is the boundary that we compute and that implies the region, the term is frequently used more loosely in computational geometry to indicate the boundary of this region.

Convex hull Algorithm in C++

Convex Hull Algorithms

For computing the convex hull of a finite set of points, multiple techniques with varying processing difficulties have been suggested in computational geometry. Constructing a clear and effective representation of the necessary convex shape entails computing the convex hull.

The algorithms are as follows:

  1. Chan's algorithm — O(n log h)
  2. Divide and conquer — O(n log n)
  3. Gift wrapping, a.k.a. Jarvis algorithm — O(nh)
  4. Graham scan — O(n log n)
  5. Incremental convex hull algorithm — O(n log n)
  6. Kirkpatrick–Seidel algorithm — O(n log h)
  7. Monotone chain, a.k.a. Andrew's algorithm— O(n log n)
  8. Quickhull- O(nlogn)

Algorithm

Jarvis's algorithm's basic concept is that we wrap points counterclockwise, starting with the leftmost point (or the point with the lowest x-coordinate value). Here, using orientation is the idea. The next point is chosen as the point that outperforms all others when oriented counterclockwise, i.e., the following point is q if "orientation(p, q, r) = counterclockwise" is true for any other point r. Make p the leftmost point at startup. The point q that causes the triplet (p, q, r) to rotate counterclockwise for any other point r is the following point. We just initialise q as the next point and then travel through all of the points to discover this. We update q as I for any point I if I is more counterclockwise or if orientation(p, I q) is counterclockwise. The most counterclockwise point will be our final value for q. In the convex output hull, place q after p. For the following iteration, set p to q.

C++ Example:

//CPP program
#include <bits/stdc++.h>
using namespace std;


struct Mypoint
{
	int x, y;
};
int orientation(Mypoint p, Mypoint q, Mypoint r)
{
	int val = (q.y - p.y) * (r.x - q.x) -
			(q.x - p.x) * (r.y - q.y);


	if (val == 0) return 0; 
	return (val > 0)? 1: 2; 
}
void convexHull(Mypoint pointval[], int n)
{
	if (n < 3) return;
	vector<Mypoint> hull;
	int l = 0;
	for (int i = 1; i < n; i++)
		if (pointval[i].x < pointval[l].x)
			l = i;
	int p = l, q;
	do
	{
		hull.push_back(pointval[p]);
		q = (p+1)%n;
		for (int i = 0; i < n; i++)
		{
		if (orientation(pointval[p], pointval[i], pointval[q]) == 2)
			q = i;
		}
		p = q;


	} while (p != l); 
	for (int i = 0; i < hull.size(); i++)
		cout << "(" << hull[i].x << ", "
			<< hull[i].y << ")\n";
}
int main()
{
	Mypoint pointval[] = {{1, 3}, {1, 2}, {4, 1}, {2, 5},
					{3, 1}, {2, 0}, {3, 2}};
	int n = sizeof(pointval)/sizeof(pointval[0]);
	convexHull(pointval, n);
	return 0;
}


//JAVA program
import java.util.*;


class Mypoint
{
	int x, y;
	Mypoint(int x, int y){
		this.x=x;
		this.y=y;
	}
}


class bn {
	
	public static int orientation(Mypoint p, Mypoint q, Mypoint r)
	{
		int val = (q.y - p.y) * (r.x - q.x) -
				(q.x - p.x) * (r.y - q.y);
	
		if (val == 0) return 0; 
		return (val > 0)? 1: 2; 
	}
	
	public static void convexHull(Mypoint pointval[], int n)
	{
		if (n < 3) return;
		Vector<Mypoint> hull = new Vector<Mypoint>();
	
		int l = 0;
		for (int i = 1; i < n; i++)
			if (pointval[i].x < pointval[l].x)
				l = i;
	
		int p = l, q;
		do
		{
			hull.add(pointval[p]);
	
			q = (p + 1) % n;
			
			for (int i = 0; i < n; i++)
			{
			if (orientation(pointval[p], pointval[i], pointval[q])
												== 2)
				q = i;
			}
	
			p = q;
	
		} while (p != l); 
	
		for (Mypoint temp : hull)
			System.out.println("(" + temp.x + ", " +
								temp.y + ")");
	}
	
	public static void main(String[] args)
	{


		Mypoint pointval[] = new Mypoint[7];
		pointval[0]=new Mypoint(0, 3);
		pointval[1]=new Mypoint(2, 3);
		pointval[2]=new Mypoint(1, 1);
		pointval[3]=new Mypoint(2, 1);
		pointval[4]=new Mypoint(3, 0);
		pointval[5]=new Mypoint(0, 0);
		pointval[6]=new Mypoint(3, 3);
		
		int n = pointval.length;
		convexHull(pointval, n);
		
	}
}


Output:

Convex hull Algorithm in C++

Related Topics

Array program in C++

What is an Array? An array is a set of identically typed elements that are organized into contiguous memory locations and each element can be independently accessed using an index. We can...

16 minutes read.

C++ Scope of Variables

In this tutorial, we will explore about the scope of variables in c++ programming language. And also, how it works in a program. What is Scope? The range of applications for something...

4 minutes read.

Pattern programs in C++

In this article, we are going to discuss different pattern programs in C++. Program for Printing * patterns: Right Angle Triangle* pattern #include <iostream> using namespace std; int main() {     int rows;     cout <<...

3 minutes read.

C++ Aggregation

C++ Aggregation Definition: In C++, aggregation is a process in which one class (as an entity reference) defines another class. It provides another way to reuse the class. It represents...

4 minutes read.

Decltype type Specifier in C++

The primary use of C++ decltype is to inspect the declaration type of an entity in an expression. The auto keyword can declare a particular type of variable, whereas the...

4 minutes read.

Quick Sort in C++

Quick sort is an efficient, in-place, comparison-based sorting algorithm that uses a divide-and-conquer strategy to sort an array or list of elements. First a pivot element is selected from the...

4 minutes read.

Armstrong Number using Do-While Loop in C++

What is Do-While Loop? An iterative loop that checks the condition at the end.The Do-While loop can be used whenever a test condition is specific, as the control enters the loop...

4 minutes read.

Upcasting and Downcasting in C++

With the help of various examples in the C++ programming language, this section will cover Upcasting and Downcasting. Upcasting and downcasting, on the other hand, are two forms of object typecasting. Consider...

3 minutes read.

CPP Templates

C++ provides a powerful feature called template, which allows the definition of generic classes and generic functions. Generic programming is a technique where different algorithms work in communion by using...

4 minutes read.

How to create the Processes with Fork in C++

 In this article, we are going to explain the different methods that help us to create processes with a fork(). There are two methods to do so. These methods are...

2 minutes read.

C++ Structs

We frequently encounter scenarios in which we must store a bunch of data, whether of comparable or dissimilar data kinds. Arrays are used to hold a group of data of...

6 minutes read.

How to make a password program in C++

Before understanding the password program, one must know about a password and why it is required. Password: A password is a word that permits access to somewhere or something. A password...

4 minutes read.

swap() function in C++

Swap() function: swap() function in C++: swap() function is a pre-define function in c++ present in STL( Standard template library ). It is used to swap two numbers. It takes two mandatory...

6 minutes read.

Iostream in C++

Using Iostream in C++, we can perform input and output operation capabilities. This represents input and output, and the stream is used to carry out this capability. A stream is...

4 minutes read.

DES in C++

The popularity of the Data Encryption Standard (DES) is slightly declining as a result of the discovery that DES is susceptible to very strong attacks. Since DES is a block cypher,...

3 minutes read.

How to Sort an Array in C++

What is Sorting? Sorting is a process of arranging elements in sequential order, either numerically or alphabetically. The sorting of a numerical array can be accomplished using a variety of algorithms,...

4 minutes read.

C++ Program to Print Fibonacci Triangle

Fibonacci Triangle Program in CPP Definition: Fibonacci Triangle as the name suggests is the same as the Fibonacci number series where the next element is the sum of the previous two elements....

3 minutes read.

C++ Program: Matrix Multiplication

Matrix Multiplication in C++ What is a Matrix? A matrix is a set of numbers in the form of rows and columns forming a rectangular array. It includes numbers, which are often...

4 minutes read.

Palindrome using Do-while loop in C++

What is Palindrome? A palindrome is a word, number, phrase, or other sequence of letters that reads the same backward as forward, such as 101 or MOM. Like other programming languages, C++...

5 minutes read.

Difference between Two Sets in C++

The distinction between the two sets is made up of the components that are present in the first set but absent from the second set. The function consistently duplicates the...

3 minutes read.