Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2215559

Browse files
Merge pull request #9 from Malvikabhalla99/pr/Convexhull
Added C++ program to find Convex hull of a set of points
2 parents 82f02a7 + 2b8a69e commit 2215559

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

‎src/convexhull.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/// A C++ program to find convex hull of a set of points.
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
class Point
6+
{
7+
public:
8+
int x, y;
9+
};
10+
11+
int find_orientation(Point p, Point q, Point r)
12+
{
13+
int val = (q.y - p.y) * (r.x - q.x) -
14+
(q.x - p.x) * (r.y - q.y);
15+
16+
if (val == 0) return 0;
17+
return (val > 0)? 1: 2;
18+
}
19+
20+
void convexHull(Point points[], int n)
21+
{
22+
if (n < 3) return;
23+
vector<Point> hull;
24+
int l = 0;
25+
for (int i = 1; i < n; i++)
26+
if (points[i].x < points[l].x)
27+
l = i;
28+
int p = l, q;
29+
do
30+
{
31+
hull.push_back(points[p]);
32+
q = (p+1)%n;
33+
for (int i = 0; i < n; i++)
34+
{
35+
if (find_orientation(points[p], points[i], points[q]) == 2)
36+
q = i;
37+
}
38+
p = q;
39+
40+
} while (p != l);
41+
for (int i = 0; i < hull.size(); i++)
42+
cout << "(" << hull[i].x << ", "
43+
<< hull[i].y << ")\n";
44+
}
45+
46+
int main()
47+
{
48+
Point points[] = {{0, 3}, {2, 2}, {1, 1}, {2, 1},
49+
{3, 0}, {0, 0}, {3, 3}};
50+
int n = sizeof(points)/sizeof(points[0]);
51+
convexHull(points, n);
52+
return 0;
53+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /