zoukankan      html  css  js  c++  java
  • POJ 1410 (线段是否与多边形相交 + 点是否在多边形内)

    题目:传送门

    题意:有 n 个测试样例,每个样例,输入四个点,前两个点代表一条线段,后两个点代表正方形的两个对角端点。

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <queue>
    #include <map>
    #include <vector>
    #include <set>
    #include <string>
    #include <math.h>
    #define LL long long
    #define mem(i, j) memset(i, j, sizeof(i))
    #define rep(i, j, k) for(int i = j; i <= k; i++)
    #define dep(i, j, k) for(int i = k; i >= j; i--)
    #define pb push_back
    #define make make_pair
    #define INF 1e20
    #define inf LLONG_MAX
    #define PI acos(-1)
    using namespace std;
    
    const int N = 1e2 + 5;
    const double eps = 1e-10;
    
    struct Point {
        double x, y;
        Point(double x = 0, double y = 0) : x(x), y(y) { } /// 构造函数
    };
    
    /// 向量加减乘除
    inline Point operator + (const Point& A, const Point& B) { return Point(A.x + B.x, A.y + B.y); }
    inline Point operator - (const Point& A, const Point& B) { return Point(A.x - B.x, A.y - B.y); }
    inline Point operator * (const Point& A, const double& p) { return Point(A.x * p, A.y * p); }
    inline Point operator / (const Point& A, const double& p) { return Point(A.x / p, A.y / p); }
    
    inline int dcmp(const double& x) { if(fabs(x) < eps) return 0; else return x < 0 ? -1 : 1; }
    
    inline double Cross(const Point& A, const Point& B) { return A.x * B.y - A.y * B.x; } /// 叉积
    inline double Dot(const Point& A, const Point& B) { return A.x * B.x + A.y * B.y; } /// 点积
    inline double Length(const Point& A) { return sqrt(Dot(A, A)); } /// 向量长度
    inline double Angle(const Point& A, const Point& B) { return acos(Dot(A, B) / Length(A) / Length(B)); } /// 向量A,B夹角
    
    inline Point GetLineIntersection(const Point P, const Point v, const Point Q, const Point w) {///求直线p + v*t 和 Q + w*t 的交点,需确保有交点,v和w是方向向量
        Point u = P - Q;
        double t = Cross(w, u) / Cross(v, w);
        return P + v * t;
    }
    
    inline bool Onsegment(Point p, Point a1, Point a2) { /// 判断点p是否在线段p1p2上
        return dcmp(Cross(a1 - p, a2 - p)) == 0 && dcmp(Dot(a1 - p, a2 - p)) <= 0;
    }
    
    inline bool SegmentProperInsection(Point a1, Point a2, Point b1, Point b2) { /// 判断线段是否相交
        if(dcmp(Cross(a1 - a2, b1 - b2)) == 0) /// 两线段平行
            return Onsegment(b1, a1, a2) || Onsegment(b2, a1, a2) || Onsegment(a1, b1, b2) || Onsegment(a2, b1, b2);
        Point tmp = GetLineIntersection(a1, a2 - a1, b1, b2 - b1);
        return Onsegment(tmp, a1, a2) && Onsegment(tmp, b1, b2);
    }
    
    inline int isPointInpolygon(Point tmp, Point P[], int n) { /// 判断点是否在多边形里
        int wn = 0;
        rep(i, 0, n - 1) {
            if(Onsegment(tmp, P[i], P[(i + 1) % n])) return -1; /// 边界
            int k = dcmp(Cross(P[(i + 1) % n] - P[i], tmp - P[i]));
            int d1 = dcmp(P[i].y - tmp.y);
            int d2 = dcmp(P[(i +1) % n].y - tmp.y);
            if(k > 0 && d1 <= 0 && d2 > 0) wn++;
            if(k < 0 && d2 <= 0 && d1 > 0) wn--;
        }
        if(wn != 0) return 1; /// 内部
        return 0; /// 外部
    }
    
    Point P[N];
    
    void solve() {
        Point st, ed;
        double x1, x2, y1, y2;
        scanf("%lf %lf %lf %lf", &st.x, &st.y, &ed.x, &ed.y);
        scanf("%lf %lf %lf %lf", &x1, &y1, &x2, &y2);
        if(x1 > x2) swap(x1, x2);
        if(y1 > y2) swap(y1, y2);
        P[0] = Point(x1, y1);
        P[1] = Point(x1, y2);
        P[2] = Point(x2, y2);
        P[3] = Point(x2, y1);
        rep(i, 0, 2) { /// 判断线段是否和多边形的边相交
            if(SegmentProperInsection(st, ed, P[i], P[i + 1]) == 1) {
                puts("T"); return ;
            }
        }
    
        if(isPointInpolygon(st, P, 4) || isPointInpolygon(ed, P, 4)) { /// 判断线段是否有一个端点在多边形里或者边界上
            puts("T"); return ;
        }
        puts("F");
    }
    
    int main() {
        int _; scanf("%d", &_);
    
        while(_--) solve();
    
        return 0;
    }
    一步一步,永不停息
  • 相关阅读:
    asp.net core过滤器记录响应对象
    ef core实现无感知软删除
    Egret资源跨域问题
    ASP.Net Core中使用jquery-ajax-unobtrusive替换Ajax.BeginForm
    把.Net开发环境迁移到Linux上去
    Mysql8.0升级后,Navicat连接报错caching_sha2_password 问题
    改MySQL的编码方式,解决jdbc MySQL中文乱码问题
    怡红公子专属网址导航
    html以前没有学到的标签
    有哪些质量上乘的程序员必关注的网站或论坛
  • 原文地址:https://www.cnblogs.com/Willems/p/12381126.html
Copyright © 2011-2022 走看看