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) 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;
}

猜你喜欢

转载自www.cnblogs.com/Willems/p/12381126.html