There are n points on the plane how to find the two points farthest from the distance

Source: Internet
Author: User
Tags sort

I. Description of the problem

There are n points on the plane, how to find the two points farthest away?

Ii. Thinking of solving problems

The first step is to look for the convex package (since the two points of the farthest distance must be on the convex package)

The second step is to use the rotating card (QIA) Shell to find the point with the largest distance

Convex and rotational jamming algorithms see http://blog.csdn.net/kaytowin/article/details/5140111

Third, code implementation

#include <iostream> #include <vector> #include <algorithm> #include <cmath> #include <stack
    
> #define INF 100000000 using namespace std;
    struct point{int x;
    int y;
Point (int x1,int y1): X (x1), Y (y1) {} () {}};
    struct maxls{point P1;
    Point P2;
    int dist;
Maxls (Point pp1,point pp2,int dist1):p 1 (PP1), p2 (PP2), dist (dist1) {} Maxls () {}};
int Xmult (point p1,point p2,point p0) {return (p1.x-p0.x) * (P2.Y-P0.Y)-(p2.x-p0.x) * (P1.Y-P0.Y);} point P0 (Inf,inf);
Vector<point> points;
Vector<point> Pstack;
            void sort (vector<point>& points1) {for (int j=points.size () -1;j>=1;j--) {for (int i=1;i<j;i++) {
    
            int Xm=xmult (POINTS[I],POINTS[I+1],P0);
                if (xm<0) {point tmp=points[i];
                POINTS[I]=POINTS[I+1];
            points[i+1]=tmp; }}//More highlights: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/void Printpoints (vector<point> PS) {for (int i=0;i<ps.size (); i++) {cout<< "<" &LT;&LT;PS[I].X&LT;&L
    t; "," <<ps[i].y<< ">" <<endl;
    } void Convexhull () {cout<< "Print input point set:" <<endl;
    Printpoints (points);
    Sort (points);
    cout<< "Print point set by polar angle:" <<endl;
    Printpoints (points);
    Pstack.push_back (Points[0]);
    Pstack.push_back (Points[1]);
    int Stack_top=pstack.size ()-1;
        for (int i=2;i<points.size (); i++) {stack_top=pstack.size ()-1;
        Point Pt2=pstack[stack_top];
        Point Pt1=pstack[stack_top-1];
        Point Ptcur=points[i];
        int xm= (pt2.x-pt1.x) * (PTCUR.Y-PT2.Y)-(ptcur.x-pt2.x) * (PT2.Y-PT1.Y);
            while (xm<0) {pstack.pop_back ();
            Stack_top=pstack.size ()-1;
            Pt2=pstack[stack_top];
            PT1=PSTACK[STACK_TOP-1];
            
        xm= (pt2.x-pt1.x) * (PTCUR.Y-PT2.Y)-(ptcur.x-pt2.x) * (PT2.Y-PT1.Y); } PSTAck.push_back (Points[i]);
    cout<< "Print convex package point set:" <<endl;
Printpoints (Pstack);
    int Dist (Point p1,point p2) {return (p2.x-p1.x) * (p2.x-p1.x) + (P2.Y-P1.Y) * (P2.Y-P1.Y);} Maxls Rotatingcalipers () {
    int q=1;
    int maxlength=0;
    Maxls MLS; for (int i=0;i<pstack.size () -1;i++) {while (Xmult pstack[i+1],pstack[(q+1)%pstack.size ()],pstack[i]) >xmult (
        Pstack[i+1],pstack[q],pstack[i])) {q= (q+1)%pstack.size ();
        int d1=dist (pstack[i],pstack[q]);
        int d2=dist (pstack[i+1],pstack[q]);
            if (D1&GT;D2) {maxlength=d1;
    
        Mls=maxls (PSTACK[I],PSTACK[Q],D1);
            }else{maxlength=d2;
        Mls=maxls (PSTACK[I+1],PSTACK[Q],D2);
} return to MLS;
    int main () {cout<< "Please enter the number of nodes:" <<endl;
    int n;
    cin>>n;
    cout<< "Please enter" <<n<< "node:" <<endl;
    int p0i=0;
    int t=0;
while (n--) {int x;        int y;
        cin>>x>>y; if (y<p0.y| | (y==p0.y&&x<p0.x))
            {p0.x=x;
            P0.y=y;
                
        p0i=t;
        } t++;
    Points.push_back (Point (X,y));
    Point Tmp=points[0];
    Points[0]=points[p0i];
    
    points[p0i]=tmp;
    Convexhull ();
    Maxls maxl= rotatingcalipers ();
    cout<< "Two point maximum distance:" <<maxL.dist<<endl;
    cout<< "two points respectively are:" << "<" <<maxL.p1.x<< "," <<maxL.p1.y<< "";
    cout<< "<" <<maxL.p2.x<< "<<maxL.p2.y<<" > "<<endl;
    System ("pause");
return 0; }

Four, input:

9

3 1
4 3
5 2
3 5
6 5
8 4
5 7
2 6
1 4

Five, output:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.