![]() |
VOOZH | about |
Prerequisites:Graham Scan's Convex Hull, Orientation.
Given a set of N points in a coordinates plane, the task is to find the maximum distance between any two points in the given set of planes.
Examples:
Input: n = 4, Points: (0, 3), (3, 0), (0, 0), (1, 1)
Output: Maximum Distance = 4.24264
Explanation:
Points having maximum distance between them are (0, 3) and (3, 0)Input: n = 5, Points: (4, 0), (0, 2), (-1, -7), (1, 10), (2, -3)
Output: Maximum Distance = 17.11724
Explanation:
Points having maximum distance between them are (-1, 7) and (1, 10)
Naive Approach: The naive idea is to try every possible pair of points from the given set and calculate the distances between each of them and print the maximum distance among all the pairs.
Below is the implementation of the above approach:
17.11724276862369
Time Complexity: O(N2), where N is the total number of points.
Auxiliary Space: O(1)
Efficient Approach: The above naive approach can be optimized using Rotating Caliper’s Method.
Rotating Calipers is a method for solving a number of problems from the field of computational geometry. It resembles the idea of rotating an adjustable caliper around the outside of a polygon’s convex hull. Originally, this method was invented to compute the diameter of convex polygons. It can also be used to compute the minimum and maximum distance between two convex polygons, the intersection of convex polygons, the maximum distance between two points in a polygon, and many things more.
To implement the above method we will use the concept of the Convex Hull. Before we begin a further discussion about the optimal approach, we need to know about the following:
Relative Area of Triangle = abs((x2-x1)*(y3-y2)-(x3-x2)*(y2-y1))
Below are the steps:
Below is the implementation of the above approach:
17.11724276862369
Time Complexity: O(N*log N)
Auxiliary Space: O(N)