Convex Hull Using Jarvis' Algorithm or Wrapping - GeeksforGeeks
Convex Hull Using Jarvis' Algorithm or Wrapping - GeeksforGeeks
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.
The big question is, given a point p as current point, how to find the next point
in output?
The idea is to use orientation() here. Next point is selected as the point that
beats all other points at counterclockwise orientation, i.e., next point is q if for
any other point r, we have “orientation(p, q, r) = counterclockwise”.
Algorithm:
Step 1) Initialize p as leftmost point.
Step 2) Do following while we don’t come back to the first (or leftmost) point.
2.1) The next point q is the point, such that the triplet (p, q, r) is counter
clockwise for any other point r.
C++
struct Point
{
int x, y;
};
// Initialize Result
vector<Point> hull;
// Print Result
for (int i = 0; i < hull.size(); i++)
cout << "(" << hull[i].x << ", "
<< hull[i].y << ")\n";
}
Java
class Point
{
int x, y;
Point(int x, int y){
this.x=x;
this.y=y;
}
}
class GFG {
// Initialize Result
Vector<Point> hull = new Vector<Point>();
// Print Result
for (Point temp : hull)
System.out.println("(" + temp.x + ", " +
temp.y + ")");
}
int n = points.length;
convexHull(points, n);
}
}
Python3
# Python3 program to find convex hull of a set of points. Refer
# https://www.geeksforgeeks.org/orientation-3-ordered-points/
# for explanation of orientation()
def Left_index(points):
'''
Finding the left most point
'''
minn = 0
for i in range(1,len(points)):
if points[i].x < points[minn].x:
minn = i
elif points[i].x == points[minn].x:
if points[i].y > points[minn].y:
minn = i
return minn
if val == 0:
return 0
elif val > 0:
return 1
else:
return 2
hull = []
'''
Start from leftmost point, keep moving counterclockwise
until reach the start point again. This loop runs O(h)
times where h is number of points in result or output.
'''
p = l
q = 0
while(True):
'''
Search for a point 'q' such that orientation(p, q,
x) is counterclockwise for all points 'x'. The idea
is to keep track of last visited most counterclock-
wise point in q. If any point 'i' is more counterclock-
wise than q, then update q.
'''
q = (p + 1) % n
for i in range(n):
# If i is more counterclockwise
# than current q, then update q
if(orientation(points[p],
points[i], points[q]) == 2):
q = i
'''
Now q is the most counterclockwise with respect to p
Set p as q for next iteration, so that q is added to
result 'hull'
'''
p = q
# Print Result
for each in hull:
print(points[each].x, points[each].y)
# Driver Code
points = []
points.append(Point(0, 3))
points.append(Point(2, 2))
points.append(Point(1, 1))
points.append(Point(2, 1))
points.append(Point(3, 0))
points.append(Point(0, 0))
points.append(Point(3, 3))
convexHull(points, len(points))
C#
// Initialize Result
List<Point> hull = new List<Point>();
// Print Result
foreach (Point temp in hull)
Console.WriteLine("(" + temp.x + ", " +
temp.y + ")");
}
/* Driver code */
public static void Main(String[] args)
{
int n = points.Length;
convexHull(points, n);
}
}
Javascript
class Point
{
constructor(x, y)
{
this.x = x;
this.y = y;
}
}
// Initialize Result
let hull = [];
// Print Result
for (let temp of hull.values())
document.write("(" + temp.x + ", " +
temp.y + ")<br>");
}
let n = points.length;
convexHull(points, n);
(0, 3)
(0, 0)
(3, 0)
(3, 3)
Similar Reads
Dynamic Convex hull | Convex Hull using Divide and
Adding Points to an Existing Conquer Algorithm
Convex Hull
Previous Next
Article Contributed By :
GeeksforGeeks
Company Explore
About Us Job-A-Thon Hiring Challenge
Legal Hack-A-Thon
Careers GfG Weekly Contest
In Media Offline Classes (Delhi/NCR)
Contact Us DSA in JAVA/C++
Advertise with us Master System Design
Master CP
Commerce UPSC
Accountancy Polity Notes
Business Studies Geography Notes
Economics History Notes
Management Science and Technology Notes
Income Tax Economics Notes
Finance Important Topics in Ethics
Statistics for Economics UPSC Previous Year Papers