POJ3608 (rotating jam-finding the closest point-to-distance of two convex packets)

Source: Internet
Author: User

Analysis: The following is from: http://blog.csdn.net/acmaker/article/details/3178696

Considering the following algorithm, the input of the algorithm is two convex polygons p and Q, respectively with M and n clockwise given vertices.

1. Calculate the vertex with the lowest Y coordinate value on p (called YMINP) and the highest Y coordinate value on Q (called YMAXQ).

2. Construct two tangent LP and LQ for polygons at YMINP and YMAXQ so that their corresponding polygons are on their right.

At this time the LP and LQ have different orientations, and YMINP and Ymaxq become a pair of heel points between the polygons.

3. Calculate the distance (YMINP,YMAXQ) and maintain it as the current minimum value.

4. Rotate parallel lines clockwise until one is coincident with the edge of the polygon on which it is located.

5. If only one line is coincident with the edge, then only the vertex-edge pair heel point pair and vertex-vertex pair heel point pairs are calculated. are going to have them with the current minimum value

The comparison, if less than the current minimum value, replaces the update. If the two tangents are coincident with the edges, then the situation becomes more complicated. If the side "overlaps", that is,

You can construct a crossover that intersects two edges (but not at the vertices), then the edge-edge distance is computed. Otherwise, calculate three new vertex-top

Point "to heel point to distance. All of these distances are compared to the current minimum value, and the replacement is updated if it is less than the current minimum value.

6. Repeat step 4 and step 5 until the new point pair is (YMINP,YMAXQ).

7. Output the minimum distance.

#include <iostream>
#include <string.h>
#include <algorithm>
#include <stdio.h>
#include <math.h>

using namespace Std;

const int n=50000;
const double eps=1e-9;
const double Inf=1e99;

struct POINT
{
Double x, y;
};

Point P[n],q[n];

Double Cross (Point A,point b,point C)
{
Return (b.x-a.x) * (C.Y-A.Y)-(B.Y-A.Y) * (c.x-a.x);
}

Double Dist (Point A,point B)
{
return sqrt ((a.x-b.x) * (a.x-b.x) + (A.Y-B.Y) * (A.Y-B.Y));
}

Double multi (point a,point b,point C)
{
Return (b.x-a.x) * (c.x-a.x) + (B.Y-A.Y) * (C.Y-A.Y);
}

Sort Clockwise
void anticlockwise (Point p[],int N)
{
for (int i=0;i<n-2;i++)
{
Double Tmp=cross (p[i],p[i+1],p[i+2]);
if (tmp>eps) return;
else if (tmp<-eps)
{
Reverse (p,p+n);
Return
}
}
}

Calculates the shortest distance from point C to line AB
Double Getdist (Point a,point b,point C)
{
if (Dist (A, b) <eps) return dist (B,C);
if (multi (a,b,c) <-eps) return dist (A,C);
if (multi (b,a,c) <-eps) return dist (B,C);
Return Fabs (Cross (a,b,c)/dist (A, b));
}

Find the distance between the two ends of a line to another line, in turn, a total of 4 cases
Double Mindist (Point a,point b,point c,point D)
{
return min (min (getdist (a,b,c), Getdist (a,b,d)), Min (Getdist (c,d,a), Getdist (c,d,b)));
}

Double Solve (Point p[],point q[],int n,int m)
{
int yminp=0,ymaxq=0;
for (int i=0;i<n;i++)
if (P[I].Y&LT;P[YMINP].Y)
Yminp=i;
for (int i=0;i<m;i++)
if (Q[I].Y&GT;Q[YMAXQ].Y)
Ymaxq=i;
P[N]=P[0];
Q[M]=Q[0];
Double Tmp,ans=inf;
for (int i=0;i<n;i++)
{
while (Tmp=cross (P[YMINP+1],Q[YMAXQ+1],P[YMINP])-cross (P[YMINP+1],Q[YMAXQ],P[YMINP]) >eps)
Ymaxq= (ymaxq+1)%m;
if (tmp<-eps) ans=min (Ans,getdist (P[YMINP],P[YMINP+1],Q[YMAXQ]));
Else Ans=min (Ans,mindist (p[yminp],p[yminp+1],q[ymaxq],q[ymaxq+1]));
yminp= (yminp+1)%n;
}
return ans;
}

int main ()
{
int n,m;
while (CIN&GT;&GT;N&GT;&GT;M)
{
if (n==0&&m==0) break;
for (int i=0;i<n;i++)
cin>>p[i].x>>p[i].y;
for (int i=0;i<m;i++)
cin>>q[i].x>>q[i].y;
Anticlockwise (P,n);
Anticlockwise (Q,M);
printf ("%.5lf\n", Min (Solve (p,q,n,m), Solve (Q,p,m,n)));
}
return 0;
}

POJ3608 (rotating jam-finding the closest point-to-distance of two convex packets)

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.