几何模板

```
const double eps = 1e-8;

int n;


struct Point {
    double x, y;
    Point() {}

    static double c_eps(double p) {
        if(fabs(p) < eps) return 0;
        return p;
    }

    void cin(){
        scanf("%lf%lf", &x, &y);
    }

    Point(double x, double y): x(x), y(y) {}

    Point operator + (const Point &p) const {
        return Point(x + p.x, y + p.y);
    }
    Point operator - (const Point &p) const {
        return Point(x - p.x, y - p.y);
    }

    Point operator * (double d) {
        return Point(x * d, y * d);
    }

    //用于求解点在向量的哪一侧
    double dot(Point p) {
        return x * p.x + y * p.y;
    }
    //a.det(b) == 0表示平行
    double det(Point p) {
        return x * p.y - y * p.x;
    }

    double dist(Point p) {
        return c_eps(sqrt((*this - p).dot(*this - p)));
    }

    void print() {
        printf("point : %.3f, %.3f\n", x, y);
    }
};

struct Line {
    Point s, e;
    Line() {}
    Line(Point s, Point e): s(s), e(e) {}
    void cin() {
        s.cin();
        e.cin();
    }
    //判断线段平行
    bool px(Line p){
        return Point::c_eps((e - s).det(p.e - p.s)) == 0;
    }
    //判断点在线段上
    bool on_seg(Point p) {
        return Point::c_eps((s - p).det(e - p)) == 0 && Point::c_eps((s - p).dot(e - p)) <= 0;
    }
    //计算出两直线的交点
    Point stra_stra_intersection(Line p) {
        return s + (e - s) * ((p.e - p.s).det(p.s - s) / (p.e - p.s).det(e - s));
    }
    //判断直线是否与线段相交
    bool is_stra_seg_intersection_1(Line p) {
        return (s - p.s).det(e - p.s) * (s - p.e).det(e - p.e) <= eps;
    }
//存在严重的精度问题,待测试中
    bool is_stra_seg_intersection_2(Line p) {
        if(px(p) && (Line(s, p.s).on_seg(e) || Line(s, p.e).on_seg(e) || Line(s, p.s).on_seg(p.e) || Line(s, p.e).on_seg(p.s) )) {
            return true;
        }
        if(px(p)) return false;
        Point r = stra_stra_intersection(p);
        //r.print();
        return p.on_seg(r);
    }

    //判断线段和线段是否相交
    bool is_seg_seg_intersection(Line p) {
        if(px(p)) {
            return on_seg(p.e) || on_seg(p.s) || p.on_seg(e) || p.on_seg(s);
        } else {
            Point r = stra_stra_intersection(p);
            return on_seg(r) && p.on_seg(r);
        }
    }

    void print(){
        printf("Line:\n");
        s.print();
        e.print();
    }
};
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值