Code:Computational Geometry

· · 个人记录

一个封装好的计算几何板子。

#include <algorithm>
#include <iostream>
#include <vector>
#include <array>
#include <cmath>

using namespace std; 

#ifndef Computational_Geometry
    #define Computational_Geometry

namespace Geometry
{
    typedef double flttype;
    typedef const flttype& crflttype;

    const flttype eps=1e-9,pi=acos((flttype)-1.);

    struct point;
    struct line;
    struct vectorl;

    struct point
    {
        flttype x,y;
        point()=default;
        point(crflttype xpos,crflttype ypos):x(xpos),y(ypos)
        {
        }
        point(const point& that)=default;
        point(point&& that)=default;
        point& operator=(const point& that)=default;
        point& operator=(point&& that)=default;
        ~point(void)=default;
        point operator+(const vectorl& that)const;
        point& operator+=(const vectorl& that);
        friend istream& operator>>(istream& inputer,point& obj)
        {
            return inputer>>obj.x>>obj.y;
        }
    };
    struct line
    {
        point a,b;
        line()=default;
        line(crflttype ax,crflttype ay,crflttype bx,crflttype by):a(ax,ay),b(bx,by)
        {
        }
        line(const point& s,const point& t):a(s),b(t)
        {
        }
        line(const line& that)=default;
        line(line&& that)=default;
        line& operator=(const line& that)=default;
        line& operator=(line&& that)=default;
        ~line(void)=default;
        friend istream& operator>>(istream& inputer,line& obj)
        {
            return inputer>>obj.a>>obj.b;
        }
    };
    struct vectorl:public point
    {
        vectorl()=default;
        vectorl(crflttype xpos,crflttype ypos):point(xpos,ypos)
        {
        }
        vectorl(const line& s):vectorl(s.b.x-s.a.x,s.b.y-s.a.y)
        {
        }
        vectorl(const point& a2,const point& b2):vectorl(line(a2,b2))
        {
        }
        vectorl(const vectorl& that)=default;
        vectorl(vectorl&& that)=default;
        vectorl& operator=(const vectorl& that)=default;
        vectorl& operator=(vectorl&& that)=default;
        ~vectorl(void)=default;
        inline flttype length(void)const
        {
            return sqrt(x*x+y*y);
        }
        inline flttype size(void)const
        {
            return sqrt(x*x+y*y);
        }
        inline flttype angle(void)const
        {
            return atan2(y,x);
        }
        vectorl operator+(const vectorl& that)const
        {
            return vectorl(x+that.x,y+that.y);
        }
        vectorl operator-(void)const
        {
            return vectorl(-x,-y);
        }
        vectorl operator-(const vectorl& that)const
        {
            return *this+(-that);
        }
        vectorl operator*(crflttype that)const
        {
            return vectorl(x*that,y*that);
        }
        friend vectorl operator*(crflttype a,const vectorl& vec)
        {
            return vectorl(vec.x*a,vec.y*a);
        }
        vectorl operator/(crflttype that)const
        {
            return vectorl(x/that,y/that);
        }
        vectorl operator+=(const vectorl& that)
        {
            return *this=*this+that;
        }
        vectorl operator-=(const vectorl& that)
        {
            return *this=*this-that;
        }
        vectorl operator*=(crflttype that)
        {
            return *this=*this*that;
        }
        vectorl operator/=(crflttype that)
        {
            return *this=*this/that;
        }
        friend istream& operator>>(istream& inputer,vectorl& obj)
        {
            return inputer>>obj.x>>obj.y;
        }
    };

    point point::operator+(const vectorl& that)const
    {
        return point(x+that.x,y+that.y);
    }
    point& point::operator+=(const vectorl& that)
    {
        return *this=*this+that;
    }

    inline flttype sqr(crflttype x)
    {
        return x*x;
    }
    inline bool valinrg(crflttype val,flttype rgl,flttype rgr)
    {
        if(rgl>rgr)
            swap(rgl,rgr);
        return rgl-eps<=val&&val<=rgr+eps;
    }
    inline flttype inprod(const vectorl& a,const vectorl& b)
    {
        return a.x*b.x+a.y*b.y;
    }
    inline flttype outprod(const vectorl& a,const vectorl& b)
    {
        return a.x*b.y-a.y*b.x;
    }
    inline flttype inprod(const point& a,const point& b,const point& c)
    {
        return inprod(vectorl(a,b),vectorl(a,c));
    }
    inline flttype outprod(const point& a,const point& b,const point& c)
    {
        return outprod(vectorl(a,b),vectorl(a,c));
    }
    inline flttype ptdist(const point& a,const point& b)
    {
        return vectorl(a,b).length();
    }
    inline point rotate(const point& a,double angle)
    {
        return point(a.x*cos(angle)-a.y*sin(angle),a.x*sin(angle)+a.y*cos(angle));
    }
    bool pt_on_ln(const line& s,const point& p)
    {
        return valinrg(p.x,s.a.x,s.b.x)&&valinrg(p.y,s.a.y,s.b.y)&&abs(outprod(vectorl(p,s.a),vectorl(p,s.b)))<=eps;
    }
    bool ln_itsct_ln(const line& a,const line& b)
    {
        if(outprod(vectorl(a),vectorl(a.a,b.a))*outprod(vectorl(a),vectorl(a.a,b.b))<-eps&&outprod(vectorl(b),vectorl(b.a,a.a))*outprod(vectorl(b),vectorl(b.a,a.b))<-eps)
            return true;
        if(pt_on_ln(b,a.a)||pt_on_ln(b,a.b)||pt_on_ln(a,b.a)||pt_on_ln(a,b.b))
            return true;
        return false;
    }
    bool pt_on_rect(const point& a,const point& b,const point& p)
    {
        return valinrg(p.x,a.x,b.x)&&valinrg(p.y,a.y,b.y);
    }
    template<typename type>
    vector<point> convex_hull(type points,int cnt=-1)
    {
        if(!~cnt)
            cnt=points.size();
        point& o=points[0];
        for(int i=1;i<cnt;i++)
            if(make_pair(o.x,o.y)<make_pair(points[i].x,points[i].y))
                swap(o,points[i]);
        sort(points.begin(),points.begin()+cnt,
                [o](const point& a,const point& b)
                {
                    flttype tmp=outprod(o,a,b);
                    if(abs(tmp)<eps)
                        return ptdist(o,a)<ptdist(o,b);
                    return tmp>0;
                }
            );
        vector<point> result;
        for(int i=0;i<cnt;i++)
        {
            while(result.size()>1&&outprod(result.back(),points[i],result[result.size()-2])<eps)
                result.pop_back();
            result.push_back(points[i]);
        }
        return result;
    }
    inline flttype vecangle(const vectorl& a,const vectorl& b)
    {
        flttype temp=inprod(a,b)/a.size()/b.size();
        if(abs(1-temp)<eps)
            temp=1;
        if(abs(-1-temp)<eps)
            temp=-1;
        flttype tmp=acos(temp);
        return outprod(a,b)>-eps?tmp:2*pi-tmp;
    }
    point perppt(const line& l,const point& p)
    {
        vectorl tmp=vectorl(l.a,l.b);
        return l.a+inprod(vectorl(l.a,p),tmp)/inprod(tmp,tmp)*tmp;
    }
    template<typename type>
    vector<point> convex_hfhl(type points,bool sideflag,int cnt=-1,bool sortflag=true)
    {
        if(!~cnt)
            cnt=points.size();
        if(sortflag)
            sort(points.begin(),points.begin()+cnt,[](const point& a,const point& b){return a.x<b.x;});
        vector<point> result;
        for(int i=0;i<cnt;i++)
        {
            while(!result.empty()&&result.back().x==points[i].x&&(sideflag?result.back().y>points[i].y:result.back().y<points[i].y))
                result.pop_back();
            if(!result.empty()&&result.back().x==points[i].x&&(sideflag?result.back().y<=points[i].y:result.back().y>=points[i].y))
                continue;
            while(result.size()>1&&(sideflag?outprod(result.back(),result[result.size()-2],points[i])>-eps:outprod(result.back(),result[result.size()-2],points[i])<eps))
                result.pop_back();
            result.push_back(points[i]);
        }
        return result;
    }
    flttype slope(const point& a,const point& b)
    {
        return (a.y-b.y)/(a.x-b.x);
    }
}

#endif