计算几何ACM——一些代码整理

来源:互联网 发布:杰洛特的母亲 知乎 编辑:程序博客网 时间:2024/05/22 16:59

实数的gcd

double gcd(double x,double y){    while(fabs(x)>eps && fabs(y)>eps)//此处的eps根据需要取    {        if(x>y)            x-=floor(x/y)*y;        else            y-=floor(y/x)*x;    }    return x+y;}

求向量叉积

double cross(const Point& p1,const Point& p2,const Point& q1,const Point& q2){    return (p2.x-p1.x)*(q2.y-q1.y)-(p2.y-p1.y)*(q2.x-q1.x);}//vector cross product

极角排序的一种实现方式

bool cmp(const Point& a,const Point& b){    if(a.y==0 && b.y==0 && a.x*b.x<=0)        return a.x>b.x;    if(a.y==0 && a.x>=0 && b.y!=0)        return true;    if(b.y==0 && b.x>=0 && a.y!=0)        return false;    if(b.y*a.y<=0)        return a.y>b.y;    return cross(O,a,O,b)>0 || (cross(O,a,O,b)==0 && a.x<b.x);}

得到垂心

Point get_circumcenter(Point a,Point b,Point c){    double a1=b.x-a.x,b1=b.y-a.y,c1=(a1*a1+b1*b1)/2;    double a2=c.x-a.x,b2=c.y-a.y,c2=(a2*a2+b2*b2)/2;    double d=a1*b2-a2*b1;    return Point(a.x+(c1*b2-c2*b1)/d,a.y+(a1*c2-a2*c1)/d);}
原创粉丝点击