【计算几何/外积运用】 HRBUST 1142 围困

围困

Time Limit: 1000 MS Memory Limit: 65536 K

Description

Leyni是一名猎人,一天他在草原中散步,遇到了三只老狼,三只老狼围成了一个三角形,如果Leyni在这个三角形中,那么他必死无疑,否则他还有一线生机。
现在已知三只老狼的坐标和Leyni的坐标,请问,Leyni还有活下去的机会吗?

Input

本题有多组测试数据,对于每组测试数据:
第一行,含有4对坐标,x1, y1, x2, y2, x3, y3, xLeyni, yLeyni。前3对坐标表示三只老狼的位置,最后一对表示Leyni的坐标。坐标的范围在[-10000, 10000],且都是整数。
注:输入数据保证Leyni不会在三角形的边上。

Output

对于每组测试数据:
第一行,如果Leyni还有一线生机,那么请输出Live,否则请输出Die

Sample Input

-1 -1 1 -1 0 1 0 0
-1 -1 1 -1 0 1 10 10

Sample Output

Die
Live

Author

齐达拉图

题意

判断一个点是否在三角形内部。

思路

叉积运用
我们可以让Leyni与三角形的三个顶点分别连线,那么形成了三条新的线段
若这三条线段的任意一条与三角形的边相交,那么说明Leyni在三角形外部
那么问题就变成了:两线段相交的问题
那么对于两线段相交,我们有定理:
若两线段相交,两个线段都符合另一条线段的两个端点分别属于当前线段的顺时针和逆时针方向。
一个线段的一个端点与另一个线段的两个端点之间的方向关系我们可以用下面的思路判定:
设这个点是A,另外两个端点是B和C,若向量AB与AC的叉乘为负数则C在AB向量的顺时针方向。反则为正就是逆时针方向。

那么我们只要分别判断新构成的三条线段是否与三角形相交,如果相交就输出Live,否则输出Die.

坑点

裸题,没有。

AC代码

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

///点和向量的定义(向量可以看成一个起点为0,0的向量,那么就可以当成点。
class Point{
public:
    double esp = 0.00001;
    double x,y;
    Point(int x = 0,int y = 0):x(x),y(y){}

    Point operator + (Point p) {return Point (x + p.x, y + p.y);}
    Point operator - (Point p) {return Point (x - p.x, y - p.y);}
    Point operator * (double n) {return Point (x*n,y*n);}
    Point operator / (double n) {return Point (x/n,y/n);}

    double Norm() {return x*x + y*y;}
    double Abs() {return sqrt(Norm());}

    bool operator < (const Point &a)
    {
        return x != a.x ? x < a.x : y < a.y;
    }

    bool operator == (const Point &a)
    {
        return fabs(x - a.x) < esp && fabs(y - a.y) < esp;
    }

};

typedef Point Vector;

double dot(Vector a,Vector b)
{
    //cout<<a.x<<' '<<a.y<<' '<<b.x<<' '<<b.y<<endl;
    return a.x*b.x + a.y*b.y;
}

double cross(Vector a,Vector b)
{
    return a.x*b.y - a.y*b.x;
}
///判断顺时针,外积为负数。(c在ba的顺时针)
bool isclock(Point a,Point b,Point c)
{
    if(cross(a-b,a-c)<0) return true;
    return false;
}
///判断线段相交:若两线段相交,则另一线段的两个端点分别位于当前线段的两段点的顺时针或者逆时针方向。
bool isinter(Point a,Point b,Point c,Point d)
{
    if(((isclock(a,c,d)==true&&isclock(b,c,d)==false)||(isclock(a,c,d)==false&&isclock(b,c,d)==true))&&(
        (isclock(a,c,d)==true&&isclock(b,c,d)==false)||(isclock(a,c,d)==false&&isclock(b,c,d)==true)))
        return true;
    return false;
}

int main(void)
{
    Point p0, p1,p2,p3;
    while(cin>>p0.x>>p0.y>>p1.x>>p1.y>>p2.x>>p2.y>>p3.x>>p3.y)
    {
        if(isinter(p3,p0,p1,p2)) cout<<"Live"<<endl;
        else if(isinter(p3,p2,p0,p1)) cout<<"Live"<<endl;
        else if(isinter(p3,p1,p0,p2)) cout<<"Live"<<endl;
        else cout<<"Die"<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

两米长弦

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

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

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

打赏作者

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

抵扣说明:

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

余额充值