[计算几何]圆与三角形是否相交

https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1298
把三角形的每条边单独判断,先判断两个点是否都在里面,是否一个点在里面一个点在外面,直接return。
然后判断点到直线的距离是否小于等于r,是的话用余弦定理判断和圆是否有交点,原理画图就能明白,如果没有交点的话在圆外的两个角必定有一个是钝角,用余弦定理判断是否小于0即可。
顺便一提,如果不需要用到double类型就都用long long类型,提高精度,点积叉积两点间距离的平方都是整数。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
struct vec{
    ll x, y;
}c;
ll r;
ll dot(const vec &a, const vec &b){ return a.x * b.x + a.y * b.y; }
ll cross(const vec &a, const vec &b){ return a.x * b.y - a.y * b.x; }
vec sub(const vec &a, const vec &b){ return vec{b.x - a.x, b.y - a.y}; }
ll dist2(const vec &a, const vec &b){ return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y); }
bool judge(const vec &a, const vec &b)
{
    if (dist2(c, a) < r * r && dist2(c, b) < r * r) return 0;
    if (dist2(c, a) <= r * r && dist2(c, b) >= r * r || dist2(c, b) <= r * r && dist2(c, a) >= r * r) return 1;
    if (cross(sub(a, c), sub(a, b)) * cross(sub(a, c), sub(a, b)) > r * r * dist2(a, b)) return 0;
    if (dist2(a, b) + dist2(b, c) < dist2(a, c) || dist2(b, a) + dist2(a, c) < dist2(b, c)) return 0;
    else return 1;
}
inline ll in()
{
    ll res=0,p=1;
    char c=getchar();
    while(c<'0'||c>'9') {if(c=='-') p=-1; c=getchar();}
    while(c>='0'&&c<='9') res=res*10+c-48,c=getchar();
    return p*res;
}
int main()
{
    int t;
    t = in();
    vec p1, p2, p3;
    while (t--){
        c.x = in(), c.y = in(), r = in();
        p1.x = in(), p1.y = in(), p2.x = in(), p2.y = in(), p3.x = in(), p3.y = in();
        if (judge(p1, p2) || judge(p2, p3) || judge(p3, p1)) puts("Yes");
        else puts("No");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值