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

Input:points[] = {{0, 2}, {2, 1}, {3, 1}, {3, 7}}Output:15.067

**Approach:** Monotone chain algorithm constructs the convex hull in **O(n * log(n))** time. We have to sort the points first and then calculate the upper and lower hulls in **O(n)** time. The points will be sorted with respect to x-coordinates (with respect to y-coordinates in case of a tie in x-coordinates), we will then find the left most point and then try to rotate in clockwise direction and find the next point and then repeat the step until we reach the rightmost point and then again rotate in the the clockwise direction and find the lower hull.

We will then find the perimeter of the convex hull using the points on the convex hull which can be done in **O(n)** time as the points are already sorted in clockwise order.

Below is the implementation of the above approach:

`// C++ implementation of the approach` `#include <bits/stdc++.h>` `#define llu long long int` `using` `namespace` `std;` ` ` `struct` `Point {` ` ` ` ` `llu x, y;` ` ` ` ` `bool` `operator<(Point p)` ` ` `{` ` ` `return` `x < p.x || (x == p.x && y < p.y);` ` ` `}` `};` ` ` `// Cross product of two vectors OA and OB` `// returns positive for counter clockwise` `// turn and negative for clockwise turn` `llu cross_product(Point O, Point A, Point B)` `{` ` ` `return` `(A.x - O.x) * (B.y - O.y)` ` ` `- (A.y - O.y) * (B.x - O.x);` `}` ` ` `// Returns a list of points on the convex hull` `// in counter-clockwise order` `vector<Point> convex_hull(vector<Point> A)` `{` ` ` `int` `n = A.size(), k = 0;` ` ` ` ` `if` `(n <= 3)` ` ` `return` `A;` ` ` ` ` `vector<Point> ans(2 * n);` ` ` ` ` `// Sort points lexicographically` ` ` `sort(A.begin(), A.end());` ` ` ` ` `// Build lower hull` ` ` `for` `(` `int` `i = 0; i < n; ++i) {` ` ` ` ` `// If the point at K-1 position is not a part` ` ` `// of hull as vector from ans[k-2] to ans[k-1]` ` ` `// and ans[k-2] to A[i] has a clockwise turn` ` ` `while` `(k >= 2` ` ` `&& cross_product(ans[k - 2], ` ` ` `ans[k - 1], A[i]) <= 0)` ` ` `k--;` ` ` `ans[k++] = A[i];` ` ` `}` ` ` ` ` `// Build upper hull` ` ` `for` `(` `size_t` `i = n - 1, t = k + 1; i > 0; --i) {` ` ` ` ` `// If the point at K-1 position is not a part` ` ` `// of hull as vector from ans[k-2] to ans[k-1]` ` ` `// and ans[k-2] to A[i] has a clockwise turn` ` ` `while` `(k >= t` ` ` `&& cross_product(ans[k - 2], ` ` ` `ans[k - 1], A[i - 1]) <= 0)` ` ` `k--;` ` ` `ans[k++] = A[i - 1];` ` ` `}` ` ` ` ` `// Resize the array to desired size` ` ` `ans.resize(k - 1);` ` ` ` ` `return` `ans;` `}` ` ` `// Function to return the distance between two points` `double` `dist(Point a, Point b)` `{` ` ` `return` `sqrt` `((a.x - b.x) * (a.x - b.x)` ` ` `+ (a.y - b.y) * (a.y - b.y));` `}` ` ` `// Function to return the perimeter of the convex hull` `double` `perimeter(vector<Point> ans)` `{` ` ` `double` `perimeter = 0.0;` ` ` ` ` `// Find the distance between adjacent points` ` ` `for` `(` `int` `i = 0; i < ans.size() - 1; i++) {` ` ` `perimeter += dist(ans[i], ans[i + 1]);` ` ` `}` ` ` ` ` `// Add the distance between first and last point` ` ` `perimeter += dist(ans[0], ans[ans.size() - 1]);` ` ` ` ` `return` `perimeter;` `}` ` ` `// Driver code` `int` `main()` `{` ` ` `vector<Point> points;` ` ` ` ` `// Add points` ` ` `points.push_back({ 0, 3 });` ` ` `points.push_back({ 2, 2 });` ` ` `points.push_back({ 1, 1 });` ` ` `points.push_back({ 2, 1 });` ` ` `points.push_back({ 3, 0 });` ` ` `points.push_back({ 0, 0 });` ` ` `points.push_back({ 3, 3 });` ` ` ` ` `// Find the convex hull` ` ` `vector<Point> ans = convex_hull(points);` ` ` ` ` `// Find the perimeter of convex polygon` ` ` `cout << perimeter(ans);` ` ` ` ` `return` `0;` `}` |

**Output:**

12

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready. Get hold of all the important mathematical concepts for competitive programming with the **Essential Maths for CP Course** at a student-friendly price.

In case you wish to attend live classes with industry experts, please refer **Geeks Classes Live** and **Geeks Classes Live USA**