coment oj計算幾何

typedef double db;
const db EPS = 1e-9;
inline int sign(db a){ return a < -EPS ? -1 : a > EPS;}
inline int cmp(db a,db b){return sign(a-b); }
struct P
{
    db x,y;
    P(){}
    P(db _x,db _y):x(_x),y(_y){}
    P operator+(P p) { return {x + p.x,y + p.y}; }
    P operator-(P p) { return {x - p.x,y - p.y}; }
    P operator*(db d) { return {x * d,y * d}; }
    P operator/(db d) { return {x / d,y / d}; }
    
    bool operator<(P p)const//先看第一維再看第二維
    {
        int c = cmp(x,p.x);
        if(c)return c == -1;
        return cmp(y,p.y) == -1;
    }
    bool operator==(P o)const
    {
        return cmp(x,o.x) == 0 && cmp(y,o.y) == 0;
    }
};
db dot(P p) { return x*p.x + y*p.y; }//點積
db det(P p) { return x*p.y - y*p.x; }//叉積 p*q*sin  p*q>0 q在p逆時針 p*q==0 共線 p*q<0 q在p順時針,可以用(1,0),(0,1)來看

db distTo(P p){return (*this-p).abs();}
db alpha() {return atan2(y, x);}
void read() { cin>>x>>y; }
void write() { cout<<"("<<x<<","<<y<<")"<<endl;}
db abs2() { return x*x+y*y; }
db abs() { return sqrt(abs2()); }
P rot90() { return P(-y,x); }
P unit() { return *this/abs(); }
int quad() { return sign(y) == 1 || (sign(y) == 0 && sign(x) >= 0); }

極角排序 atan2

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章