Dynamic Convex hull | Adding Points to an Existing Convex Hull
Last Updated :
14 Sep, 2023
Given a convex hull, we need to add a given number of points to the convex hull and print the convex hull after every point addition. The points should be in anti-clockwise order after addition of every point.
Examples:
Input :
Convex Hull : (0, 0), (3, -1), (4, 5), (-1, 4)
Point to add : (100, 100)
Output :
New convex hull : (-1, 4) (0, 0) (3, -1) (100, 100)
We first check whether the point is inside the given convex hull or not. If it is, then nothing has to be done we directly return the given convex hull. If the point is outside the convex hull, we find the lower and upper tangents, and then merge the point with the given convex hull to find the new convex hull, as shown in the figure.

The red outline shows the new convex hull after merging the point and the given convex hull.
To find the upper tangent, we first choose a point on the hull that is nearest to the given point. Then while the line joining the point on the convex hull and the given point crosses the convex hull, we move anti-clockwise till we get the tangent line.

The figure shows the moving of the point on the convex hull for finding the upper tangent.
Note: It is assumed here that the input of the initial convex hull is in the anti-clockwise order, otherwise we have to first sort them in anti-clockwise order then apply the following code.
Code:
CPP
// C++ program to add given a point p to a given
// convex hull. The program assumes that the
// point of given convex hull are in anti-clockwise
// order.
#include<bits/stdc++.h>
using namespace std;
// checks whether the point crosses the convex hull
// or not
int orientation(pair<int, int> a, pair<int, int> b,
pair<int, int> c)
{
int res = (b.second-a.second)*(c.first-b.first) -
(c.second-b.second)*(b.first-a.first);
if (res == 0)
return 0;
if (res > 0)
return 1;
return -1;
}
// Returns the square of distance between two input points
int sqDist(pair<int, int> p1, pair<int, int> p2)
{
return (p1.first-p2.first)*(p1.first-p2.first) +
(p1.second-p2.second)*(p1.second-p2.second);
}
// Checks whether the point is inside the convex hull or not
bool inside(vector<pair<int, int>> a, pair<int, int> p)
{
// Initialize the centroid of the convex hull
pair<int, int> mid = {0, 0};
int n = a.size();
// Multiplying with n to avoid floating point
// arithmetic.
p.first *= n;
p.second *= n;
for (int i=0; i<n; i++)
{
mid.first += a[i].first;
mid.second += a[i].second;
a[i].first *= n;
a[i].second *= n;
}
// if the mid and the given point lies always
// on the same side w.r.t every edge of the
// convex hull, then the point lies inside
// the convex hull
for (int i=0, j; i<n; i++)
{
j = (i+1)%n;
int x1 = a[i].first, x2 = a[j].first;
int y1 = a[i].second, y2 = a[j].second;
int a1 = y1-y2;
int b1 = x2-x1;
int c1 = x1*y2-y1*x2;
int for_mid = a1*mid.first+b1*mid.second+c1;
int for_p = a1*p.first+b1*p.second+c1;
if (for_mid*for_p < 0)
return false;
}
return true;
}
// Adds a point p to given convex hull a[]
void addPoint(vector<pair<int, int>> &a, pair<int, int> p)
{
// If point is inside p
if (inside(a, p))
return;
// point having minimum distance from the point p
int ind = 0;
int n = a.size();
for (int i=1; i<n; i++)
if (sqDist(p, a[i]) < sqDist(p, a[ind]))
ind = i;
// Find the upper tangent
int up = ind;
while (orientation(p, a[up], a[(up+1)%n])>=0)
up = (up + 1) % n;
// Find the lower tangent
int low = ind;
while (orientation(p, a[low], a[(n+low-1)%n])<=0)
low = (n+low - 1) % n;
// Initialize result
vector<pair<int, int>>ret;
// making the final hull by traversing points
// from up to low of given convex hull.
int curr = up;
ret.push_back(a[curr]);
while (curr != low)
{
curr = (curr+1)%n;
ret.push_back(a[curr]);
}
// Modify the original vector
ret.push_back(p);
a.clear();
for (int i=0; i<ret.size(); i++)
a.push_back(ret[i]);
}
// Driver code
int main()
{
// the set of points in the convex hull
vector<pair<int, int> > a;
a.push_back({0, 0});
a.push_back({3, -1});
a.push_back({4, 5});
a.push_back({-1, 4});
int n = a.size();
pair<int, int> p = {100, 100};
addPoint(a, p);
// Print the modified Convex Hull
for (auto e : a)
cout << "(" << e.first << ", "
<< e.second << ") ";
return 0;
}
Java
// Java program to add given a point p to a given
// convex hull. The program assumes that the
// point of given convex hull are in anti-clockwise
// order.
import java.io.*;
import java.util.*;
class GFG
{
// checks whether the point crosses the convex hull
// or not
static int orientation(ArrayList<Integer> a,
ArrayList<Integer> b,
ArrayList<Integer> c)
{
int res = (b.get(1) - a.get(1)) * (c.get(0) - b.get(0)) -
(c.get(1) - b.get(1)) * (b.get(0)-a.get(0));
if (res == 0)
return 0;
if (res > 0)
return 1;
return -1;
}
// Returns the square of distance between two input points
static int sqDist(ArrayList<Integer>p1, ArrayList<Integer>p2)
{
return (p1.get(0) - p2.get(0)) * (p1.get(0) - p2.get(0)) +
(p1.get(1) - p2.get(1)) * (p1.get(1) - p2.get(1));
}
// Checks whether the point is inside the convex hull or not
static boolean inside(ArrayList<ArrayList<Integer>> A,ArrayList<Integer>p)
{
// Initialize the centroid of the convex hull
ArrayList<Integer> mid = new ArrayList<Integer>(Arrays.asList(0,0));
int n = A.size();
for (int i = 0; i < n; i++)
{
mid.set(0,mid.get(0) + A.get(i).get(0));
mid.set(1,mid.get(1) + A.get(i).get(1));
}
// if the mid and the given point lies always
// on the same side w.r.t every edge of the
// convex hull, then the point lies inside
// the convex hull
for (int i = 0, j; i < n; i++)
{
j = (i + 1) % n;
int x1 = A.get(i).get(0)*n, x2 = A.get(j).get(0)*n;
int y1 = A.get(i).get(1)*n, y2 = A.get(j).get(1)*n;
int a1 = y1 - y2;
int b1 = x2 - x1;
int c1 = x1 * y2 - y1 * x2;
int for_mid = a1 * mid.get(0) + b1 * mid.get(1) + c1;
int for_p = a1 * p.get(0) * n + b1 * p.get(1) * n + c1;
if (for_mid*for_p < 0)
return false;
}
return true;
}
// Adds a point p to given convex hull a[]
static void addPoint(ArrayList<ArrayList<Integer>> a,ArrayList<Integer> p)
{
// If point is inside p
if (inside(a, p))
return;
// point having minimum distance from the point p
int ind = 0;
int n = a.size();
for (int i = 1; i < n; i++)
{
if (sqDist(p, a.get(i)) < sqDist(p, a.get(ind)))
{
ind = i;
}
}
// Find the upper tangent
int up = ind;
while (orientation(p, a.get(up), a.get((up+1)%n))>=0)
up = (up + 1) % n;
// Find the lower tangent
int low = ind;
while (orientation(p, a.get(low), a.get((n+low-1)%n))<=0)
low = (n+low - 1) % n;
// Initialize result
ArrayList<ArrayList<Integer>> ret = new ArrayList<ArrayList<Integer>>();
// making the final hull by traversing points
// from up to low of given convex hull.
int curr = up;
ret.add(a.get(curr));
while (curr != low)
{
curr = (curr + 1) % n;
ret.add(a.get(curr));
}
// Modify the original vector
ret.add(p);
a.clear();
for (int i = 0; i < ret.size(); i++)
{
a.add(ret.get(i));
}
}
// Driver code
public static void main (String[] args)
{
// the set of points in the convex hull
ArrayList<ArrayList<Integer>> a = new ArrayList<ArrayList<Integer>>();
a.add(new ArrayList<Integer>(Arrays.asList(0, 0)));
a.add(new ArrayList<Integer>(Arrays.asList(3, -1)));
a.add(new ArrayList<Integer>(Arrays.asList(4, 5)));
a.add(new ArrayList<Integer>(Arrays.asList(-1, 4)));
int n = a.size();
ArrayList<Integer> p = new ArrayList<Integer>(Arrays.asList(100,100));
addPoint(a, p);
// Print the modified Convex Hull
for(ArrayList<Integer> e:a )
{
System.out.print("(" + e.get(0) + ", " + e.get(1) + ") ");
}
}
}
// This code is contributed by rag2127
Python3
# Python 3 program to add given a point p to a given
# convex hull. The program assumes that the
# point of given convex hull are in anti-clockwise
# order.
import copy
# checks whether the point crosses the convex hull
# or not
def orientation(a, b, c):
res = ((b[1] - a[1]) * (c[0] - b[0]) -
(c[1] - b[1]) * (b[0] - a[0]))
if (res == 0):
return 0;
if (res > 0):
return 1;
return -1;
# Returns the square of distance between two input points
def sqDist(p1, p2):
return ((p1[0] - p2[0]) * (p1[0] - p2[0]) +
(p1[1] - p2[1]) * (p1[1] - p2[1]));
# Checks whether the point is inside the convex hull or not
def inside( a, p ):
# Initialize the centroid of the convex hull
mid = [0, 0]
n = len(a)
# Multiplying with n to avoid floating point
# arithmetic.
p[0] *= n;
p[1] *= n;
for i in range(n):
mid[0] += a[i][0];
mid[1] += a[i][1];
a[i][0] *= n;
a[i][1] *= n;
# if the mid and the given point lies always
# on the same side w.r.t every edge of the
# convex hull, then the point lies inside
# the convex hull
for i in range( n ):
j = (i + 1) % n;
x1 = a[i][0]
x2 = a[j][0]
y1 = a[i][1]
y2 = a[j][1]
a1 = y1 - y2;
b1 = x2 - x1;
c1 = x1 * y2 - y1 * x2;
for_mid = a1 * mid[0] + b1 * mid[1] + c1;
for_p = a1 * p[0] + b1*p[1]+c1;
if (for_mid*for_p < 0):
return False;
return True;
# Adds a point p to given convex hull a[]
def addPoint( a, p):
# If point is inside p
arr= copy.deepcopy(a)
prr =p.copy()
if (inside(arr, prr)):
return;
# point having minimum distance from the point p
ind = 0;
n = len(a)
for i in range(1, n):
if (sqDist(p, a[i]) < sqDist(p, a[ind])):
ind = i
# Find the upper tangent
up = ind;
while (orientation(p, a[up], a[(up + 1) % n]) >= 0):
up = (up + 1) % n;
# Find the lower tangent
low = ind;
while (orientation(p, a[low], a[(n + low - 1) % n]) <= 0):
low = (n + low - 1) % n
# Initialize result
ret = []
# making the final hull by traversing points
# from up to low of given convex hull.
curr = up;
ret.append(a[curr]);
while (curr != low):
curr = (curr + 1) % n;
ret.append(a[curr]);
# Modify the original vector
ret.append(p);
a.clear();
for i in range(len(ret)):
a.append(ret[i]);
# Driver code
if __name__ == "__main__":
# the set of points in the convex hull
a = []
a.append([0, 0]);
a.append([3, -1]);
a.append([4, 5]);
a.append([-1, 4]);
n = len(a)
p = [100, 100]
addPoint(a, p);
# Print the modified Convex Hull
for e in a :
print("(" , e[0], ", ",
e[1] , ") ",end=" ")
# This code is contributed by chitranayal
C#
// C# program to add given a point p to a given
// convex hull. The program assumes that the
// point of given convex hull are in anti-clockwise
// order.
using System;
using System.Collections.Generic;
public class GFG{
// checks whether the point crosses the convex hull
// or not
static int orientation(List<int> a,List<int> b,List<int> c)
{
int res=(b[1]-a[1]) * (c[0]-b[0]) - (c[1]-b[1]) * (b[0]-a[0]);
if (res == 0)
return 0;
if (res > 0)
return 1;
return -1;
}
// Returns the square of distance between two input points
static int sqDist(List<int>p1, List<int>p2)
{
return (p1[0] - p2[0]) * (p1[0] - p2[0]) +
(p1[1] - p2[1]) * (p1[1] - p2[1]);
}
// Checks whether the point is inside the convex hull or not
static bool inside(List<List<int>> A,List<int>p)
{
// Initialize the centroid of the convex hull
List<int> mid = new List<int>(){0,0};
int n = A.Count;
for (int i = 0; i < n; i++)
{
mid[0]+=A[i][0];
mid[1]+=A[i][1];
}
// if the mid and the given point lies always
// on the same side w.r.t every edge of the
// convex hull, then the point lies inside
// the convex hull
for (int i = 0, j; i < n; i++)
{
j = (i + 1) % n;
int x1 = A[i][0]*n, x2 = A[j][0]*n;
int y1 = A[i][1]*n, y2 = A[j][1]*n;
int a1 = y1 - y2;
int b1 = x2 - x1;
int c1 = x1 * y2 - y1 * x2;
int for_mid = a1 * mid[0] + b1 * mid[1] + c1;
int for_p = a1 * p[0] * n + b1 * p[1] * n + c1;
if (for_mid*for_p < 0)
return false;
}
return true;
}
// Adds a point p to given convex hull a[]
static void addPoint(List<List<int>> a,List<int> p)
{
// If point is inside p
if (inside(a, p))
return;
// point having minimum distance from the point p
int ind = 0;
int n = a.Count;
for (int i = 1; i < n; i++)
{
if (sqDist(p, a[i]) < sqDist(p, a[ind]))
{
ind = i;
}
}
// Find the upper tangent
int up = ind;
while (orientation(p, a[up], a[(up+1)%n])>=0)
up = (up + 1) % n;
// Find the lower tangent
int low = ind;
while (orientation(p, a[low], a[(n+low-1)%n])<=0)
low = (n+low - 1) % n;
// Initialize result
List<List<int>> ret = new List<List<int>>();
// making the final hull by traversing points
// from up to low of given convex hull.
int curr = up;
ret.Add(a[curr]);
while (curr != low)
{
curr = (curr + 1) % n;
ret.Add(a[curr]);
}
// Modify the original vector
ret.Add(p);
a.Clear();
for (int i = 0; i < ret.Count; i++)
{
a.Add(ret[i]);
}
}
// Driver code
static public void Main (){
// the set of points in the convex hull
List<List<int>> a = new List<List<int>>();
a.Add(new List<int>(){0,0});
a.Add(new List<int>(){3,-1});
a.Add(new List<int>(){4,5});
a.Add(new List<int>(){-1,4});
int n=a.Count;
List<int> p = new List<int>(){100,100};
addPoint(a, p);
// Print the modified Convex Hull
foreach(List<int> e in a)
{
Console.Write("(" + e[0] + ", " + e[1] + ") ");
}
}
}
// This code is contributed by avanitrachhadiya2155
JavaScript
<script>
// Javascript program to add given a point p to a given
// convex hull. The program assumes that the
// point of given convex hull are in anti-clockwise
// order.
// checks whether the point crosses the convex hull
// or not
function orientation(a,b,c)
{
let res = (b[1] - a[1]) * (c[0] - b[0]) -
(c[1] - b[1]) * (b[0]-a[0]);
if (res == 0)
return 0;
if (res > 0)
return 1;
return -1;
}
// Returns the square of distance between two input points
function sqDist(p1,p2)
{
return (p1[0] - p2[0]) * (p1[0] - p2[0]) +
(p1[1] - p2[1]) * (p1[1] - p2[1]);
}
// Checks whether the point is inside the convex hull or not
function inside(A,p)
{
// Initialize the centroid of the convex hull
let mid = [0,0];
let n = A.length;
for (let i = 0; i < n; i++)
{
mid[0]+=A[i][0];
mid[1]+=A[i][1];
}
// if the mid and the given point lies always
// on the same side w.r.t every edge of the
// convex hull, then the point lies inside
// the convex hull
for (let i = 0, j; i < n; i++)
{
j = (i + 1) % n;
let x1 = A[i][0]*n, x2 = A[j][0]*n;
let y1 = A[i][1]*n, y2 = A[j][1]*n;
let a1 = y1 - y2;
let b1 = x2 - x1;
let c1 = x1 * y2 - y1 * x2;
let for_mid = a1 * mid[0] + b1 * mid[1] + c1;
let for_p = a1 * p[0] * n + b1 * p[1] * n + c1;
if (for_mid*for_p < 0)
return false;
}
return true;
}
// Adds a point p to given convex hull a[]
function addPoint(a,p)
{
// If point is inside p
if (inside(a, p))
return;
// point having minimum distance from the point p
let ind = 0;
let n = a.length;
for (let i = 1; i < n; i++)
{
if (sqDist(p, a[i]) < sqDist(p, a[ind]))
{
ind = i;
}
}
// Find the upper tangent
let up = ind;
while (orientation(p, a[up], a[(up+1)%n])>=0)
up = (up + 1) % n;
// Find the lower tangent
let low = ind;
while (orientation(p, a[low], a[(n+low-1)%n])<=0)
low = (n+low - 1) % n;
// Initialize result
let ret = [];
// making the final hull by traversing points
// from up to low of given convex hull.
let curr = up;
ret.push(a[curr]);
while (curr != low)
{
curr = (curr + 1) % n;
ret.push(a[curr]);
}
// Modify the original vector
ret.push(p);
a=[];
for (let i = 0; i < ret.length; i++)
{
a.push(ret[i]);
}
return a;
}
// Driver code
// the set of points in the convex hull
let a = []
a.push([0, 0]);
a.push([3, -1]);
a.push([4, 5]);
a.push([-1, 4]);
let n=a.length;
let p=[100,100];
a=addPoint(a, p);
// Print the modified Convex Hull
for(let e=0;e<a.length;e++ )
{
document.write("(" + a[e][0] + ", " + a[e][1] + ") ");
}
// This code is contributed by ab2127
</script>
Output:
(-1, 4) (0, 0) (3, -1) (100, 100)
Time Complexity:
The time complexity of the above algorithm is O(n*q), where q is the number of points to be added.
Auxiliary Space: O(n), since n extra space has been taken.
This article is contributed by Aarti_Rathi and Amritya Vagmi and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to [email protected].
Similar Reads
Convex Hull Algorithm
The Convex Hull Algorithm is used to find the convex hull of a set of points in computational geometry. The convex hull is the smallest convex set that encloses all the points, forming a convex polygon. This algorithm is important in various applications such as image processing, route planning, and
8 min read
Convex Hull using Divide and Conquer Algorithm
In computational geometry, a convex hull is the smallest convex polygon that contains a given set of points. It is a fundamental concept with applications in various fields such as computer graphics, robotics, and image processing. Importance of Convex Hull:Convex hulls are important in computationa
15 min read
Convex Hull using Jarvis' Algorithm or Wrapping
Given a set of points in the plane. the convex hull of the set is the smallest convex polygon that contains all the points of it.We strongly recommend to see the following post first. How to check if two given line segments intersect?The idea of Jarvis's Algorithm is simple, we start from the leftmo
13 min read
Convex Hull using Graham Scan
A convex hull is the smallest convex polygon that contains a given set of points. It is a useful concept in computational geometry and has applications in various fields such as computer graphics, image processing, and collision detection.A convex polygon is a polygon in which all interior angles ar
15+ min read
Convex Hull | Monotone chain algorithm
Given a set of points, the task is to find the convex hull of the given points. The convex hull is the smallest convex polygon that contains all the points. Please check this article first: Convex Hull | Set 1 (Jarvisâs Algorithm or Wrapping) Examples: Input: Points[] = {{0, 3}, {2, 2}, {1, 1}, {2,
11 min read
Quickhull Algorithm for Convex Hull
Given a set of points, a Convex hull is the smallest convex polygon containing all the given points. Input : points[] = {{0, 3}, {1, 1}, {2, 2}, {4, 4}, {0, 0}, {1, 2}, {3, 1}, {3, 3}};Output : The points in convex hull are: (0, 0) (0, 3) (3, 1) (4, 4)Input : points[] = {{0, 3}, {1, 1}Output : Not P
14 min read
Problems on Convex Hull
Dynamic Convex hull | Adding Points to an Existing Convex Hull
Given a convex hull, we need to add a given number of points to the convex hull and print the convex hull after every point addition. The points should be in anti-clockwise order after addition of every point. Examples: Input : Convex Hull : (0, 0), (3, -1), (4, 5), (-1, 4) Point to add : (100, 100)
15 min read
Deleting points from Convex Hull
Given a fixed set of points. We need to find convex hull of given set. We also need to find convex hull when a point is removed from the set. Example: Initial Set of Points: (-2, 8) (-1, 2) (0, 1) (1, 0) (-3, 0) (-1, -9) (2, -6) (3, 0) (5, 3) (2, 5) Initial convex hull:- (-2, 8) (-3, 0) (-1, -9) (2,
15+ min read
Perimeter of Convex hull for a given set of points
Given n 2-D points points[], the task is to find the perimeter of the convex hull for the set of points. A convex hull for a set of points is the smallest convex polygon that contains all the points. Examples: Input: points[] = {{0, 3}, {2, 2}, {1, 1}, {2, 1}, {3, 0}, {0, 0}, {3, 3}} Output: 12 Inpu
10 min read
Check if the given point lies inside given N points of a Convex Polygon
Given coordinates of the N points of a Convex Polygon. The task is to check if the given point (X, Y) lies inside the polygon. Examples:Input: N = 7, Points: {(1, 1), (2, 1), (3, 1), (4, 1), (4, 2), (4, 3), (4, 4)}, Query: X = 3, Y = 2 Below is the image of plotting of the given points: Output: YES
15 min read
Tangents between two Convex Polygons
Given two convex polygons, we aim to identify the lower and upper tangents connecting them. As shown in the figure below, TRL and TLR represent the upper and lower tangents, respectively. Examples: Input: First Polygon : [[2, 2], [3, 3], [5, 2], [4, 0], [3, 1]] Second Polygon : [[-1, 0], [0, 1], [1,
15 min read
Check if given polygon is a convex polygon or not
Given a 2D array point[][] with each row of the form {X, Y}, representing the co-ordinates of a polygon in either clockwise or counterclockwise sequence, the task is to check if the polygon is a convex polygon or not. If found to be true, then print "Yes" . Otherwise, print "No".In a convex polygon,
9 min read
Check whether two convex regular polygon have same center or not
Given two positive integers N and M which denotes the sides of the convex regular polygon where N < M, the task is to check whether polygons have the same center or not if N-sided polygon was inscribed in an M-sided polygon.Center of Polygon: Point inside a polygon which is equidistant from each
3 min read
Minimum Enclosing Circle
Prerequisites: Equation of circle when three points on the circle are given, Convex HullGiven an array arr[][] containing N points in a 2-D plane with integer coordinates. The task is to find the centre and the radius of the minimum enclosing circle(MEC). A minimum enclosing circle is a circle in wh
15+ min read
How to Highlight Groups with Convex Hull in ggplot2 in R?
In this article, we are going to see how to highlight groups with the convex hull in ggplot2 using R Programming Language. Convex hull polygon refers to the draw a line bounding box around the outermost points in each group. Creating scatterplot for demonstration Here we will use the iris dataset t
2 min read