zoukankan      html  css  js  c++  java
  • 计算几何基础模板

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cstdlib>
    #include<cmath>
    #include<iostream>
    #define eps 1e-8
    using namespace std;
    typedef long long ll;
    int sgn(double x){
        if(fabs(x)<eps) return 0;
        else if(x<0) return -1;
        else return 1;
    }
    struct Point{
        double x,y;
        Point(){}
        Point(double _x,double _y){x=_x;y=_y;}
        Point operator-(const Point &b)const{return Point(x-b.x,y-b.y);}
        double operator^(const Point &b)const{return x*b.y-y*b.x;}
        double operator*(const Point &b)const{return x*b.x+y*b.y;}
    };
    struct Line{
        Point s,e;
        Line(){}
        Line(Point _s,Point _e){s=_s,e=_e;}
        pair<int,Point>operator&(const Line &b)const{
            Point res=s;
            if(sgn((s-e)^(b.s-b.e))==0){
                if(sgn((s-b.e)^(b.s-b.e))==0) return make_pair(0,res);
                else return make_pair(1,res);
            }
            double t=((s-b.s)^(b.s-b.e))/((s-e)^(b.s-b.e));
            res.x+=(e.x-s.x)*t;
            res.y+=(e.y-s.y)*t;
            return make_pair(2,res);
        }
    };
    bool inter(Line l1,Line l2){
        return 
        max(l1.s.x,l1.e.x)>=min(l2.s.x,l2.e.x)&&
        max(l2.s.x,l2.e.x)>=min(l1.s.x,l1.e.x)&&
        max(l1.s.y,l1.e.y)>=min(l2.s.y,l2.e.y)&&
        max(l2.s.y,l2.e.y)>=min(l1.s.y,l1.e.y)&&
        sgn((l2.s-l1.e)^(l1.s-l1.e))*sgn((l2.e-l1.e)^(l1.s-l1.e))<=0&&
        sgn((l1.s-l2.e)^(l2.s-l2.e))*sgn((l1.e-l2.e)^(l2.s-l2.e))<=0;
    }
    
    int main(){
        return 0;
    }
  • 相关阅读:
    ES6(二)解构赋值详解
    面试题
    说出x的结果,并解释为什么?
    23种设计模式
    自定义滚动条
    JavaScript之数据类型
    [[转]CSS浮动原理
    正选反选JS
    让2个并列的div根据内容自动保持同等高度js
    jquery鼠标滑过展示图片时显示详情
  • 原文地址:https://www.cnblogs.com/elpsycongroo/p/8884006.html
Copyright © 2011-2022 走看看