UVA 10256 - The Great Divide

来源:互联网 发布:局域网网络监控 编辑:程序博客网 时间:2024/06/01 12:37

这个题 就是把两个颜色的凸包全部求出来。 然后判断 凸包之间是否有重叠。


在判断凸包之间时候有重叠的时候 有两个方面。 1   判断  某一个点 是否在另一个凸包内部  2   判断 凸包的线段是否出现相交。



#include <cstdio>#include <algorithm>#include <iostream>#include <cstring>#include <cmath>#include <cstdlib>#include <string>#include <map>#include <vector>#include <set>#include <queue>#include <stack>#include <cctype>using namespace std;#define ll long longtypedef unsigned long long ull;#define maxn 10010#define INF 1<<30struct Point{    double x,y;    Point(double x = 0, double y = 0):x(x),y(y) {}};typedef Point Vector ;Vector operator + (Vector A, Vector B){ return Vector(A.x + B.x, A.y + B.y);}Vector operator - (Vector A, Point B){return Vector(A.x - B.x, A.y - B.y);}Vector operator * (Vector A, double p){return Vector(A.x * p, A.y * p);}Vector operator / (Vector A, double p){return Vector(A.x / p, A.y / p);}bool operator < (const Point & a, const Point & b){    return a.x < b.x || (a.x == b.x && a.y < b.y);}const double eps = 1e-10;int dcmp(double x){    if(fabs(x) < eps) return 0;    else return x < 0 ? -1 : 1;}bool operator == (const Point & a, const Point & b){    return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;}double Dot(Vector A, Vector B){ return A.x * B.x + A.y * B.y;} // 点积double Length(Vector A) { return sqrt(Dot(A, A));}             //向量长度double Angle(Vector A, Vector B){ return acos(Dot(A, B)/Length(A)/Length(B));}                                                                //夹角double Cross(Vector A, Vector B){return A.x*B.y - A.y * B.x;}   //叉积(面积两倍)double Area2(Point A, Point B, Point C){return Cross(B-A,C-A);} // 面积两倍Vector Rotate(Vector A, double rad){return Vector(A.x*cos(rad) - A.y*sin(rad), A.x*sin(rad)+A.y*cos(rad));}// 旋转后的直线</span>Vector Normal(Vector A){    double L = Length(A);    return Vector(-A.y/L, A.x/L);}Point GetLineIntersection(Point P, Point v, Point Q, Point w){  //求直线 pv 与qw的交点    Vector u = P-Q;    double t = Cross(w, u) / Cross(v, w);    return P+v*t;}double DistanceToline(Point P, Point A, Point B){                //点到直线的距离    Vector v1 = B - A,v2 = P - A;    return fabs(Cross(v1,v2))/Length(v1);}double DistanceTpsegment(Point P, Point A, Point B){             //点到线段的距离    if(A == B) return Length(P-A);    Vector v1 = B - A, v2 = P - A, v3 = P - B;    if(dcmp(Dot(v1, v2)) < 0) return Length(v2);    else if(dcmp(Dot(v1,v3)) > 0) return Length(v3);    else return fabs(Cross(v1, v2)) / Length(v1);}Point GetLinePrijection(Point P, Point A, Point B){             // 点在直线上的投影    Vector v = B-A;    return A+v*(Dot(v, P-A)/Dot(v,v));}bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2){ // 线段相交判定(不包括在端点处的情况)    double c1 = Cross(a2 - a1,b1-a1) ,c2 = Cross(a2 - a1,b2-a1),    c3 = Cross(b2 - b1, a1-b1), c4 = Cross(b2 - b1, a2 - b1);    return dcmp(c1)*dcmp(c2) < 0 && dcmp(c3)*dcmp(c4) < 0;}bool OnSegment(Point p, Point a1, Point a2){                              //线段在端点处是否可能相交    return dcmp(Cross(a1-p, a2-p)) == 0 && dcmp(Dot(a1-p, a2-p)) < 0;}double ConvexPolygonArea(Point * p,int n){                         // 多边形的有向面积    double area = 0;    for(int i = 1; i < n-1; i++)        area += Cross(p[i] - p[0],p[i+1] - p[0]);    return area/2;}int ConvexHull(Point *p, int n, Point * ch){                 // 求凸包。 点保存在 ch中    sort(p, p+n);    int m = 0;    for(int i = 0; i < n; i++){        while(m > 1 && Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2]) <= 0) m--;        ch[m++] = p[i];    }    int k = m;    for(int i = n-2; i >= 0; i--){        while(m > k && Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2]) <= 0) m--;        ch[m++] = p[i];    }    if(n > 1) m --;    return m;}int isPointInPolygon(Point p, Point q[], int n){ // 判断p 是否在q这个凸包的内部, n为 q的数量    int w = 0;    for(int i = 0; i < n; i++){        if(OnSegment(p, q[i], q[(i+1) % n]))            return -1;        int k = dcmp(Cross(q[(i+1)%n] - q[i], p-q[i]));        int d1 = dcmp(q[i].y - p.y);        int d2 = dcmp(q[(i+1)%n].y - p.y);        if(k > 0 && d1 <= 0 && d2 > 0)            w++;        if(k < 0 && d2 <= 0 && d1 > 0)            w--;    }    if(w != 0) return 1; // 在内部    return 0;    // 在外部}int pan(Point af1[], int p1n, Point af2[], int p2n){    for(int i = 0; i < p1n; i++)        if(isPointInPolygon(af1[i], af2, p2n))            return 0;    for(int i = 0; i < p2n; i++)        if(isPointInPolygon(af2[i], af1, p1n))            return 0;    for(int i = 0; i < p1n; i++){        for(int j = 0; j < p2n; j++){            if(SegmentProperIntersection(af1[i], af1[(i+1)%p1n], af2[i], af2[(j+1)%p2n]))                return 0;        }    }    return 1;}int main (){    int m,n;    while(scanf("%d%d",&m,&n)){        if(m == 0 && n == 0)            break;        Point p1[maxn];        Point p2[maxn];        Point af1[maxn];        Point af2[maxn];        for(int i = 0; i < m; i++){            double x, y;            scanf("%lf%lf",&x, &y);            p1[i] = Point(x,y);        }        for(int i = 0; i < n; i++){            double x, y;            scanf("%lf%lf",&x, &y);            p2[i] = Point(x,y);        }        int p1n = ConvexHull(p1, m, af1);        int p2n = ConvexHull(p2, n, af2);        if(pan(af1, p1n, af2, p2n)) printf("Yes\n");        else printf("No\n");    }    return 0;}


0 0
原创粉丝点击