POJ 1410 (if the line segment intersects the polygon point is within the polygon +)

Title: Portal

The meaning of problems: there are n test sample, each sample, enter the four points, the first two points represent a line, two points after two squares represent the diagonal endpoints.

 

#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));} /// vector length 
inline Double Angle ( const Point & A, const Point & B) { return ACOS (Dot (A, B) / the Length (A) / the Length (B));} /// vector A, B angle 

inline Point GetLineIntersection ( const Point P, const Point V, const Point Q, const Point W) { /// the Line and p + v * t Q + w * t of the intersection, the need to ensure that there is an intersection, v and w are direction vectors 
    Point U = P - Q;
     Double T = Cross (w, U) / Cross (V, w);
     return P + V * T;
}

inline BOOL Onsegment (Point p, Point A1, A2 Point) { /// determines whether a point p on the line segment P1P2 
    return dCMP (Cross (A1 - p, A2 - p)) == 0 && dCMP (Dot (A1 - p , A2 - P)) <= 0 ; 
} 

inline BOOL SegmentProperInsection (Point A1, A2 Point, Point B1, B2 Point) { /// determines whether or not the line segment intersects 
    iF (dCMP (Cross (A1 - A2, B1 - B2)) == 0 ) // two parallel line segments 
        return Onsegment (B1, A1, A2) || Onsegment (B2, A1, A2) || Onsegment (A1, B1, B2) || Onsegment (A2, B1, B2); 
    tmp Point = 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) the swap ( X1, X2);
     IF (Y1> Y2) the 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 ) { /// sides of the polygon is determined whether the line segment and intersecting 
        iF (SegmentProperInsection (ST, ED, P [I], P [I +. 1 ]) == . 1 ) { 
            the puts ( " T " ); return ; 
        } 
    } 

    IF (isPointInpolygon (ST, P, . 4 ) || isPointInpolygon (ED, P, . 4 )) { /// determines whether there is a line segment endpoint on the border or inside the polygon 
        the puts ( " T " ); return ; 
    } 
    the puts ( " F. " ); 
} 

int main () {
     int _; Scanf ( " % D " , & _); 

    the while (_-- ) Solve ( );

    return 0;
}

 

Guess you like

Origin www.cnblogs.com/Willems/p/12381126.html