UVa 10256 The Great Divide,判斷兩個凸包是否相離,10256凸包

來源:互聯網
上載者:User

UVa 10256 The Great Divide,判斷兩個凸包是否相離,10256凸包


先從給出的兩個點集中分別計算出兩個凸包,

然後判斷兩個凸包是否相離。


#include<cstdio>#include<vector>#include<cmath>#include<algorithm>using namespace std;const double eps = 1e-10;double dcmp(double x) {    if(fabs(x) < eps) return 0;    else return x < 0 ? -1 : 1;}struct Point {    double x, y;    Point(double x=0, double y=0):x(x),y(y) {}};typedef Point Vector;Vector operator - (const Point& A, const Point& B) {    return Vector(A.x-B.x, A.y-B.y);}double Cross(const Vector& A, const Vector& B) {    return A.x*B.y - A.y*B.x;}double Dot(const Vector& A, const Vector& B) {    return A.x*B.x + A.y*B.y;}bool operator < (const Point& p1, const Point& p2) {    return p1.x < p2.x || (p1.x == p2.x && p1.y < p2.y);}bool operator == (const Point& p1, const Point& p2) {    return p1.x == p2.x && p1.y == p2.y;}bool SegmentProperIntersection(const Point& a1, const Point& a2, const Point& b1, const 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(const Point& p, const Point& a1, const Point& a2) {    return dcmp(Cross(a1-p, a2-p)) == 0 && dcmp(Dot(a1-p, a2-p)) < 0;}// 點集凸包// 如果不希望在凸包的邊上有輸入焦點,把兩個 <= 改成 <// 如果不介意點集被修改,可以改成傳遞引用vector<Point> ConvexHull(vector<Point> p) {    // 預先處理,重複資料刪除點    sort(p.begin(), p.end());    p.erase(unique(p.begin(), p.end()), p.end());    int n = p.size();    int m = 0;    vector<Point> ch(n+1);    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--;    ch.resize(m);    return ch;}int IsPointInPolygon(const Point& p, const vector<Point>& poly) {    int wn = 0;    int n = poly.size();    for(int i=0; i<n; ++i) {        const Point& p1 = poly[i];        const Point& p2 = poly[(i+1)%n];        if(p1 == p || p2 == p || OnSegment(p, p1, p2)) return -1;//在邊界上        int k = dcmp(Cross(p2-p1, p-p1));        int d1 = dcmp(p1.y - p.y);        int d2 = dcmp(p2.y - p.y);        if(k > 0 && d1 <= 0 && d2 > 0) wn++;        if(k < 0 && d2 <= 0 && d1 > 0) wn--;    }    if(wn != 0) return 1;    return 0;}bool ConvexPolygonDisjoint(const vector<Point> ch1, const vector<Point> ch2) {    int c1 = ch1.size();    int c2 = ch2.size();    for(int i=0; i<c1; ++i)        if(IsPointInPolygon(ch1[i], ch2) != 0) return false;    for(int i=0; i<c2; ++i)        if(IsPointInPolygon(ch2[i], ch1) != 0) return false;    for(int i=0; i<c1; ++i)        for(int j=0; j<c2; ++j)            if(SegmentProperIntersection(ch1[i], ch1[(i+1)%c1], ch2[j], ch2[(j+1)%c2])) return false;    return true;}int main() {    int n, m;    while(scanf("%d%d", &n, &m) == 2 && n > 0 && m > 0) {        vector<Point> P1, P2;        double x, y;        for(int i = 0; i < n; i++) {            scanf("%lf%lf", &x, &y);            P1.push_back(Point(x, y));        }        for(int i = 0; i < m; i++) {            scanf("%lf%lf", &x, &y);            P2.push_back(Point(x, y));        }        if(ConvexPolygonDisjoint(ConvexHull(P1), ConvexHull(P2)))            printf("Yes\n");        else            printf("No\n");    }    return 0;}





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.