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.
Input: n = 4, Points: (0, 3), (3, 0), (0, 0), (1, 1)
Output: Maximum Distance = 4.24264
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
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:
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:
- Unsigned Area Of Triangle: If we are given three points P1(x1, y1), P2(x2, y2) and P3(x3, y3) then is the signed area of triangle. If the area is positive then three points are in the clockwise order, Else they are in anti-clockwise order and if the area equals to zero then, the points are co-linear. If we take absolute value, then this will represent the unsigned area of the triangle. Here, unsigned basically means area without direction i.e., we just need the relative absolute value of the area. Therefore, we can remove (1/2) from the formula. Hence,
Relative Area of Triangle = abs((x2-x1)*(y3-y2)-(x3-x2)*(y2-y1))
- Antipodal Points: It is those points which are diametrically opposite to each other. But for us, antipodal points are those which are farthest from each other in the convex polygon. If we choose one point from the given set, then this point can only achieve it’s maximum distance if and only if we can find it’s antipodal point from the given set.
Below are the steps:
- Two points having maximum distance must lie on the boundary of the convex polygon formed from the given set. Therefore, use Graham Scan’s convex hull method to arrange points in counter-clockwise order.
- We have N points, Initially start from point P1 and include those points from set of given points such that area of region always increases by including any points from the set.
- Starting from point P1, Choose K = 2 and increment K while area(PN, P1, PK) is increasing and stop before it starts decreasing. Now the current point PK might be the antipodal point for P1. Similarly, find antipodal point for p2 by finding area(P1, P2, PK) and incrementing K form where we previously stopped and so on.
- Keep updating the maximum distance for each antipodal points occurs in the above steps as the distance between intial point and point by including area was maximum.
Below is the implementation of the above approach:
Time Complexity: O(N*log N)
Auxiliary Space: O(N)
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.
- Hammered distance between N points in a 2-D plane
- Ways to choose three points with distance between the most distant points <= L
- Count of Squares that are parallel to the coordinate axis from the given set of N points
- Number of triangles in a plane if no more than two points are collinear
- Find the equation of plane which passes through two points and parallel to a given axis
- Shortest distance between a Line and a Point in a 3-D plane
- Distance between a point and a Plane in 3 D
- Number of Integral Points between Two Points
- Program to find equation of a plane passing through 3 points
- Program to check whether 4 points in a 3-D plane are Coplanar
- Program for distance between two points on earth
- Haversine formula to find distance between two points on a sphere
- Check whether it is possible to join two points given on circle such that distance between them is k
- Find the maximum possible distance from origin using given points
- Queries to find maximum sum contiguous subarrays of given length in a rotating array
- Count of obtuse angles in a circle with 'k' equidistant points between 2 given points
- Distance between end points of Hour and minute hand at given time
- Find integral points with minimum distance from given set of integers using BFS
- Lexicographically Kth smallest way to reach given coordinate from origin
- Finding Quadrant of a Coordinate with respect to a Circle
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : mohit kumar 29