POJ3304 思维、判断线段和直线相交

题目

Segments

给出n个线段,问是否存在一条直线,使得n个线段在这条直线上的投影会交于一点。

解题思路

假设交点为x,过x作这条直线的垂线l,那么必然l经过所有线段。所以问题转化为,是否存在一条直线和n条线段相交。

代码

#include <iostream>
#include <vector>
using namespace std;

const int N = 1e5 + 5;

typedef struct Point
{
    double x, y;
    Point operator-(const Point temp)
    {
        Point T;
        T.x = x - temp.x;
        T.y = y - temp.y;
        return T;
    }
    bool operator==(const Point temp)
    {
        return x == temp.x && y == temp.y;
    }
} Vector;

double cross(Vector A, Vector B)
{
    return A.x * B.y - B.x * A.y;
}

const double eps = 0;

int sign(double x)
{
    if (x < eps)
        return -1;
    else if (x > eps)
        return 1;
    return 0;
}
Point Start[N], End[N];
bool f(Point a1, Point a2, Point b1, Point b2)
{
    double c1 = cross(a2 - a1, b1 - a1), c2 = cross(a2 - a1, b2 - a1);
    return sign(c1) * sign(c2) <= 0;
}

int n;
bool check(Point A, Point B)
{
    if (A == B)
        return 0;
    for (int i = 1; i <= n; i++)
        if (!f(A, B, Start[i], End[i]))
            return 0;
    return 1;
}
void solve()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        scanf("%lf %lf", &Start[i].x, &Start[i].y);
        scanf("%lf %lf", &End[i].x, &End[i].y);
    }
    int flag = 0;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            if (check(Start[i], Start[j]))
                flag = 1;
            if (check(Start[i], End[j]))
                flag = 1;
            if (check(End[i], End[j]))
                flag = 1;
            if (check(End[i], Start[j]))
                flag = 1;
        }
    }
    puts(flag ? "Yes!" : "No!");
}

int main()
{
    int t;
    cin >> t;
    while (t--)
        solve();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hesorchen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值