diff --git a/Jarvis March_Convex Hulls_C++ b/Jarvis March_Convex Hulls_C++ new file mode 100644 index 0000000..4f41843 --- /dev/null +++ b/Jarvis March_Convex Hulls_C++ @@ -0,0 +1,75 @@ +//Jarvis March Convex Hull + +#include +using namespace std; +#define INF 10000 + +struct Point +{ + int x; + int y; +}; + + +int orientation(Point p1, Point p2, Point p3) +{ + int val = (p2.y - p1.y) * (p3.x - p2.x) - (p2.x - p1.x) * (p3.y - p2.y); + + if (val == 0) + return 0; // colinear + else if(val > 0) + return 1; //clockwise + else + return 2; // counterclock wise +} + +// Prints convex hull of a set of n points. +void convexHull(Point points[], int n) +{ + int i; + + if (n < 3) + return; + + // Initialize Result + int next[n]; + for (i = 0; i < n; i++) + next[i] = -1; + + // Find the leftmost point + int l = 0; + for (i = 1; i < n; i++) + if (points[i].x < points[l].x) + l = i; + + int p = l, search_q; + do + { + search_q = (p + 1) % n; + for (i = 0; i < n; i++) + if (orientation(points[p], points[i], points[search_q]) == 2) + search_q = i; + + next[p] = search_q; + p = search_q; + } + while (p != l); + + // Print Result + for (i = 0; i < n; i++) + { + if (next[i] != -1) + cout << "(" << points[i].x << ", " << points[i].y << ")"<