08 计算几何

点/向量

 

 1 const double eps=1e-8;
 2 struct Point{
 3     double x,y;
 4     Point(){};
 5     Point(double x,double y): x(x),y(y){};
 6     Point operator+(Point m){//向量加法
 7         Point t(x+m.x,y+m.y);
 8         return t;
 9     }
10     Point operator-(Point m){//向量减法
11         Point t(x-m.x,y-m.y);
12         return t;
13     }
14     Point operator*(double m){//数乘
15         Point t(x*m,y*m);
16         return t;
17     }
18     bool operator==(Point m){//判断相等
19         if(fabs(x-m.x)+fabs(y-m.y)<=eps)
20             return 1;
21         else return 0;
22     }
23     double len(){//向量的模
24         return sqrt(x*x+y*y);
25     }
26     double len2(){//模平方
27         return len()*len();
28     }
29     double ang(){//极角,范围(-pi,pi]
30         return atan2(y,x);
31     }
32     void one(double l=1){//归一化,默认为1
33         x=l*x/len(),y=l*y/len();
34     }
35     double dis(Point m){//两点之间的距离
36         return sqrt((x-m.x)*(x-m.x)+(y-m.y)*(y-m.y));
37     }
38     double operator*(Point m){//向量点乘
39         return x*m.x+y*m.y;
40     }
41     double operator^(Point m){//向量叉乘,注意^的优先度,记得带上括号
42         return x*m.y-y*m.x;
43     }
44     Point rotate(double ang){//顺时针旋转
45         Point t(x*cos(ang)-y*sin(ang),x*sin(ang)+y*cos(ang));
46         return t;
47     }
48 };
49 double sin(Point v1,Point v2){//向量夹角的正弦
50     return (v1^v2)/(v1.len()*v2.len());
51 }
52 double cos(Point v1,Point v2){//向量夹角的余弦
53     return v1*v2/(v1.len()*v2.len());
54 }
55 double ang(Point v1,Point v2){//两向量的夹角
56     return atan2(v1^v2,v1*v2);
57 }

 

直线

 

 1 typedef Point Vec;
 2 struct Line{//点向式直线
 3     Vec dir;//方向向量中x为正值
 4     Point pnt;//所经过点
 5     Line(){}
 6     Line(Vec _dir,Point _pnt):dir(_dir),pnt(_pnt){}
 7     int relation(Point p){//判断某点与直线的位置关系,-1为点在左侧,1为点在右侧,0为过该直线
 8         Vec v=dir,u=p-dir;
 9         if((v^u)<eps){
10             return -1;
11         }else if((v^u)>eps){
12             return 1;
13         }else return 0;
14     }
15     double pointdis(Point p){//点到直线的距离
16         return fabs(p^dir)/dir.len();
17     }
18     Point touying(Point p){//点到该直线的投影所在点
19         Point t,u=p-dir,v=dir;
20         t=dir*(fabs(v^u)/v.len2());
21         return t=t+pnt;
22     }
23     bool operator||(Line s){//平行且不相等
24         if(dir.x*s.dir.y==dir.y*s.dir.x){
25             Vec t=s.pnt-pnt;
26             if(dir.x*t.y!=dir.y*t.x)
27                 return 1;
28         }
29         return 0;
30     }
31     bool operator|(Line s){//只判断平行与否
32         if(dir.x*s.dir.y==dir.y*s.dir.x){
33             return 1;
34         }
35         return 0;
36     }
37 };

 

 先就写这两个吧,感觉太多的暂时用不到。等整理板子的时候再补充吧。

 

posted @ 2020-08-08 21:20  Zabreture  阅读(79)  评论(0编辑  收藏  举报