search
HomeBackend DevelopmentC++Calculate the maximum distance between two points on the coordinate plane using the rotating caliper method

Calculate the maximum distance between two points on the coordinate plane using the rotating caliper method

Sep 12, 2023 am 09:33 AM
rotating caliper methodcoordinate planemaximum distance

In C, we have a predefined function sqrt which returns the square root of any number. The rotating caliper method is a technique used in solving algorithms or computational geometry.

Visual Representation of the Rotating Caliper Method

Hand Rotation shows a real-world example of a rotating caliper plot, showing the vertical orientation whenever the hand is rotated. We can also understand this concept by using polygons.

Calculate the maximum distance between two points on the coordinate plane using the rotating caliper method

In this article, we will use the rotating caliper method to find the maximum distance between two coordinate points. 跨度>

grammar

Use the following syntax in the program -

vector<datatype> name

parameter

  • Vectors - We start with keyword vectors and initialize the vectors in C.

  • datatype - The type of data element represented by the vector.

  • name - The name of the vector.

algorithm

  • We will use the header files iostream, vector and cmath to start the program.

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

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

  • We are defining a function definition called CP() whose double data type accepts parameters Point p1, Point p2 and Point p3 b> calculation Cross product vectors, i.e. p2-p1 and p3-p1 w.r.t x and y coordinates.

  • Now we are creating a function definition of double precision data type rotatingCaliper() that takes the argument as a point vector and maximizes the distance between any two coordinate planes.

  • We initialize the variable result to 0, which will be tracked to satisfy the calculation of the maximum distance. To find the size of the point, it will use a predefined function called size() and store it in the variable n.

  • We initialize the two variables j and k to 1 and perform the following operations -

    • We are moving j to the next point in the polygon and the cross product of the current edge 'points[i], points[CP i 1] % n'And the next edge 'points[j]' is less than the cross product CP of the current edge 'points[i]', points[ (i 1) % n]' and the edge after the next point 'point [(j 1) % n]'. This will verify that the current edge is perpendicular to the next edge.

    • We move k to the next point in the polygon until the distance between the current point 'point[i]' and the next point ' The distance point[k]' is less than the distance between the current point 'point[i]' and the next point 'points[(k 1)%n]'. This will verify that the next point is furthest from the current point.

    • Now we are calculating the distance between point j, k, and the current point 'point[i]', multiplying all these points, and then we Will get the maximum value in the result variable.

  • We start the main function and apply the value of the coordinate plane to the "vector point" variable.

  • Finally, we call the function name rotatingCaliper() and pass the 'points' value as parameter to get the maximum distance of the rotating caliper plot.

Example

In this program, we will use the rotating caliper method to perform the maximum distance between two points in the coordinate plane.

#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

in conclusion

We understand the concept of the rotating caliper method by calculating the maximum distance between two coordinate points. Practical applications of this method include aperture angle optimization, machine learning classification, etc.

The above is the detailed content of Calculate the maximum distance between two points on the coordinate plane using the rotating caliper method. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:tutorialspoint. If there is any infringement, please contact admin@php.cn delete
C# vs. C  : Learning Curves and Developer ExperienceC# vs. C : Learning Curves and Developer ExperienceApr 18, 2025 am 12:13 AM

There are significant differences in the learning curves of C# and C and developer experience. 1) The learning curve of C# is relatively flat and is suitable for rapid development and enterprise-level applications. 2) The learning curve of C is steep and is suitable for high-performance and low-level control scenarios.

C# vs. C  : Object-Oriented Programming and FeaturesC# vs. C : Object-Oriented Programming and FeaturesApr 17, 2025 am 12:02 AM

There are significant differences in how C# and C implement and features in object-oriented programming (OOP). 1) The class definition and syntax of C# are more concise and support advanced features such as LINQ. 2) C provides finer granular control, suitable for system programming and high performance needs. Both have their own advantages, and the choice should be based on the specific application scenario.

From XML to C  : Data Transformation and ManipulationFrom XML to C : Data Transformation and ManipulationApr 16, 2025 am 12:08 AM

Converting from XML to C and performing data operations can be achieved through the following steps: 1) parsing XML files using tinyxml2 library, 2) mapping data into C's data structure, 3) using C standard library such as std::vector for data operations. Through these steps, data converted from XML can be processed and manipulated efficiently.

C# vs. C  : Memory Management and Garbage CollectionC# vs. C : Memory Management and Garbage CollectionApr 15, 2025 am 12:16 AM

C# uses automatic garbage collection mechanism, while C uses manual memory management. 1. C#'s garbage collector automatically manages memory to reduce the risk of memory leakage, but may lead to performance degradation. 2.C provides flexible memory control, suitable for applications that require fine management, but should be handled with caution to avoid memory leakage.

Beyond the Hype: Assessing the Relevance of C   TodayBeyond the Hype: Assessing the Relevance of C TodayApr 14, 2025 am 12:01 AM

C still has important relevance in modern programming. 1) High performance and direct hardware operation capabilities make it the first choice in the fields of game development, embedded systems and high-performance computing. 2) Rich programming paradigms and modern features such as smart pointers and template programming enhance its flexibility and efficiency. Although the learning curve is steep, its powerful capabilities make it still important in today's programming ecosystem.

The C   Community: Resources, Support, and DevelopmentThe C Community: Resources, Support, and DevelopmentApr 13, 2025 am 12:01 AM

C Learners and developers can get resources and support from StackOverflow, Reddit's r/cpp community, Coursera and edX courses, open source projects on GitHub, professional consulting services, and CppCon. 1. StackOverflow provides answers to technical questions; 2. Reddit's r/cpp community shares the latest news; 3. Coursera and edX provide formal C courses; 4. Open source projects on GitHub such as LLVM and Boost improve skills; 5. Professional consulting services such as JetBrains and Perforce provide technical support; 6. CppCon and other conferences help careers

C# vs. C  : Where Each Language ExcelsC# vs. C : Where Each Language ExcelsApr 12, 2025 am 12:08 AM

C# is suitable for projects that require high development efficiency and cross-platform support, while C is suitable for applications that require high performance and underlying control. 1) C# simplifies development, provides garbage collection and rich class libraries, suitable for enterprise-level applications. 2)C allows direct memory operation, suitable for game development and high-performance computing.

The Continued Use of C  : Reasons for Its EnduranceThe Continued Use of C : Reasons for Its EnduranceApr 11, 2025 am 12:02 AM

C Reasons for continuous use include its high performance, wide application and evolving characteristics. 1) High-efficiency performance: C performs excellently in system programming and high-performance computing by directly manipulating memory and hardware. 2) Widely used: shine in the fields of game development, embedded systems, etc. 3) Continuous evolution: Since its release in 1983, C has continued to add new features to maintain its competitiveness.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment