POJ 3304 Segments

幾何 直線與線段相交

若是每條線段的投影在直線上有重合的點,那麼咱們經過這一點作一條直線一定會通過全部的線段!! 那麼咱們考慮把這條直線隨意移動到與其中一條線段的某個端點重合,此時直線仍是過了全部線段,咱們再以該點爲中心順時針或逆時針旋轉直線,讓這條直線剛好通過另外一個線段的某個端點,此時直線必定仍是通過全部線段,且通過了其中某兩個線段的兩個端點。 根據這個思路咱們能夠枚舉全部端點,用叉積判斷直線與線段是否相交,複雜度O(n^3)ios

#include <iostream>
#include <cstdio>
#include <cmath>
#define INF 0x3f3f3f3f
#define full(a, b) memset(a, b, sizeof a)
using namespace std;
typedef long long ll;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
    int X = 0, w = 0; char ch = 0;
    while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
    while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
    return w ? -X : X;
}
inline int gcd(int a, int b){ return a % b ? gcd(b, a % b) : b; }
inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
template<typename T>
inline T max(T x, T y, T z){ return max(max(x, y), z); }
template<typename T>
inline T min(T x, T y, T z){ return min(min(x, y), z); }
template<typename A, typename B, typename C>
inline A fpow(A x, B p, C lyd){
    A ans = 1;
    for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
    return ans;
}

const int N = 105;
const double eps = 1e-8;
struct Point { double x,  y;} s[N], e[N];
int n, _;

double mul(const Point &a, const Point &b, const Point &c){
    return (a.x - c.x) * (b.y - c.y) - (a.y - c.y) * (b.x - c.x);
}

bool check(const Point &a, const Point &b){
    if(fabs(a.x - b.x) < eps && fabs(a.y - b.y) < eps) return false;
    for(int i = 0; i < n; i ++){
        if(mul(a, b, s[i]) * mul(a, b, e[i]) > eps)
            return false;
    }
    return true;
}

int main(){

    while(scanf("%d", &_) != EOF){
        for(; _; _ --){
            scanf("%d", &n);
            for(int i = 0; i < n; i ++){
                scanf("%lf%lf%lf%lf", &s[i].x, &s[i].y, &e[i].x, &e[i].y);
            }
            if(n == 1){
                printf("Yes!\n");
                continue;
            }
            bool flag = false;
            for(int i = 0; i < n; i ++){
                for(int j = i + 1; j < n; j ++){
                    if(check(s[i], s[j]) || check(s[i], e[j]) || check(e[i], e[j]) || check(e[i], s[j])){
                        flag = true;
                        break;
                    }
                }
                if(flag) break;
            }
            if(flag) printf("Yes!\n");
            else printf("No!\n");
        }
    }
    return 0;
}
相關文章
相關標籤/搜索