Maximum distance between two points in coordinate plane using Rotating Caliper’s Method


In C++ we have a predefined function sqrt that returns the square root of any number. The Rotating Caliper’s Method is the technique used to solve the algorithmic or computational geometry.

Visual Representation of Rotating Caliper’s Method

The hand rotation shows the real example of a rotating caliper graph, whenever the hand rotates the perpendicular direction is shown. We can also understand this concept by using a polygon shape.

In this article, we are going to find the maximum distance of two coordinate points using Rotating Caliper’s Method.

Syntax

The following syntax used in the program −

vector<datatype> name

Parameters

  • vector − We begin with the keyword vector while initializing the vector in C++.

  • datatype − The type of data element represented by a vector.

  • name − The name of the vector.

Algorithm

  • We will start the program with header files namely iostream, vector, and cmath.

  • We are creating the structure name point that will store the coordinate of x and y.

  • We are defining a function definition distance() of double datatype to calculate the distance between two coordinate points. Here, Points p1 and Point p2 are the parameters that accept the coordinate values and return the distance by using predefined function sqrt and distance formula.

  • We are defining a function definition named CP() with double datatype which accepts parameters Point p1, Point p2, and Point p3 that calculates the cross product vector i.e, p2-p1 and p3-p1 w.r.t x and y coordinates.

  • Now we are creating a function definition rotatingCaliper() of double datatype that takes parameter as vector of points and maximize the distance between any two coordinate planes.

  • We initialize the variable result to 0 which will keep track to meet the calculation of maximum distance. To find the size of the point it will use the predefined function named size() and store it in the variable n.

  • We are initializing two variables j and k to 1 and do the following −

    • We are moving j to next point in the polygon and the cross-product CP of current edge ‘points[i], points[i+1] % n’ and next edge ‘points[j]’ is lesser than the cross-product CP of the current edge ‘points[i], points[(i + 1) % n]’ and the edge after the next point ‘points[(j + 1) % n]’. This verifies that current edge is perpendicular to next edge.

    • We are moving k to the next point in the polygon till the distance between current point ‘point[i]’ and next point ‘point[k]’ is lesser than the distance between current point ‘point[i]’ and point after the next point ‘points[(k+1)%n]’. This verifies that the next point is farthest away from the current point.

    • Now we are calculating the distance between points j, k, and the current point ‘point[i]’ to multiply all these points together and we will get the maximum value in result variable.

  • We start the main function and apply the values of coordinate plane to ‘vector <point> points’ variable.

  • Finally, we are calling the function name rotatingCaliper() and passing the ‘points’ values as a parameter to get the maximum distance of rotating caliper graph.

Example

In this program, we are going to execute the maximum distance between two points in coordinate plane using Rotating Caliper’s Method.

#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
struct Point {
    double x, y;
};
// In this function we are calculating the distance between two coordinate point.
double distance(Point p1, Point p2) {
   return sqrt((p2.x - p1.x) * (p2.x - p1.x) + (p2.y - p1.y) * (p2.y - p1.y));
}
// In this function we are calculating the cross-product of two vector
double CP(Point p1, Point p2, Point p3) // CP: cross-product {
   return (p2.x - p1.x) * (p3.y - p1.y) - (p2.y - p1.y) * (p3.x - p1.x);
}
// In this function we are calculating the Rotating Caliper
double rotatingCalipers(vector<Point> points) {
   double result = 0;
  int n = points.size();
    int j = 1, k = 1;
   for (int i = 0; i < n; i++) {
       while (CP(points[i], points[(i + 1) % n], points[j]) < CP(points[i], points[(i + 1) % n], points[(j + 1) % n])) 
       {
           j = (j + 1) % n;
       }
       while (distance(points[i], points[k]) < distance(points[i], points[(k + 1) % n])) {
          k = (k + 1) % n;
       }
     // calculate the max distance
        result = max(result, distance(points[i], points[j]) * distance(points[i], points[k]));
   }
   return result;
}
int main() {
    vector<Point> points = {{0, 0}, {1, 1}, {1, 2}, {2, 2}, {2, 3}, {3, 3}, {3, 4}, {4, 4}, {4, 5}, {5, 5},{5,6}};
    cout << "Maximum distance between two coordinate points: "<<rotatingCalipers(points) << endl;
    return 0;
}

Output

Maximum distance between two coordinate points: 39.0512

Conclusion

We learned the concept of Rotating Caliper’s Method by calculating the maximum distance between two coordinate points. The real application of this method such as aperture angle optimization and machine learning classification.

Updated on: 24-Jul-2023

86 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements