计算几何模板(点+线段)1.0
Posted wwkkk
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了计算几何模板(点+线段)1.0相关的知识,希望对你有一定的参考价值。
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<cmath> 5 using namespace std; 6 const int INF = 0x3f3f3f3f; 7 const int maxn = 100; 8 const double eps = 1e-6; 9 10 struct point 11 { 12 double x,y; 13 point(double _x=0,double _y=0) 14 { 15 x = _x; y = _y; 16 } 17 }; 18 19 int sgn(double x) 20 { 21 if(fabs(x)<eps) return 0; 22 if(x<0) return -1; 23 else return 1; 24 } 25 26 //向量-向量 27 point operator -(const point &a, const point &b) {return point(a.x-b.x, a.y-b.y);} 28 //向量+向量 29 point operator +(const point &a, const point &b) {return point(a.x+b.x, a.y+b.y);} 30 //向量*向量 31 point operator *(const double &a, const point &b) {return point(a*b.x, a*b.y);} 32 //向量*常数 Dot 33 double operator *(const point &a, const point &b) {return a.x*b.x+a.y*b.y;} 34 //向量X向量 35 double operator ^(const point &a, const point &b) {return a.x*b.y-a.y*b.x;} 36 //向量==向量 37 bool operator == (const point &a,const point &b) {return sgn(a.x-b.x)==0&&sgn(a.y-b.y)==0;} 38 39 double Length(point a) {return sqrt(a*a);} 40 //AB夹角 41 double Angle(point a,point b) {return acos( a*b/Length(a)/Length(b) );} 42 //AB、AC组成的平行四边形面积 43 double Area(point a,point b,point c) {return (b-a)^(c-a);} 44 //A逆时针旋转弧度rad后的向量 45 point Rotate(point a,double rad) {return point(a.x*cos(rad)-a.y*sin(rad),a.x*sin(rad)+a.y*cos(rad));} 46 //计算A的单位法线,保证A不为0向量 47 point Normal(point a) 48 { 49 double L = Length(a); 50 return point(-a.y/L,a.x/L); 51 } 52 53 struct line 54 { 55 point s,e; 56 line(){} 57 line(point _s,point _e) 58 { 59 s = _s; e = _e; 60 } 61 }; 62 63 //线段是否相交、端点算相交 64 bool inter(line l1,line l2) 65 { 66 return 67 min(l1.s.x,l1.e.x)<=max(l2.s.x,l2.e.x) && 68 min(l2.s.x,l2.e.x)<=max(l1.s.x,l1.e.x) && 69 min(l1.s.y,l1.e.y)<=max(l2.s.y,l2.e.y) && 70 min(l2.s.y,l2.e.y)<=max(l1.s.y,l1.e.y) && 71 sgn((l2.s-l1.e)^(l1.s-l1.e)) * sgn((l2.e-l1.e)^(l1.s-l1.e))<=0 && 72 sgn((l1.s-l2.e)^(l2.s-l2.e)) * sgn((l1.e-l2.e)^(l2.s-l2.e))<=0; 73 } 74 75 //线段是否相交、端点不算相交 76 bool inter2(line l1,line l2) 77 { 78 point a1=l1.s,a2=l1.e; 79 point b1=l2.s,b2=l2.e; 80 double c1 = (a2-a1)^(b1-a1), c2 = (a2-a1)^(b2-a1); 81 double c3 = (b2-b1)^(a1-b1), c4 = (b2-b1)^(a2-b1); 82 return sgn(c1)*sgn(c2)<0 && sgn(c3)*sgn(c4)<0; 83 } 84 //求p边形的面积、res[0~n-1]顺序存顶点、n为顶点数,res[n]=res[0] 85 double area() 86 { 87 double ret = 0; 88 for(int i=0;i<n;i++) 89 { 90 int sgn = dcmp(cross(res[i],res[i+1])); 91 ret += sgn*calc(res[i],res[i+1]); 92 } 93 return fabs(ret); 94 } 95 96 point p[maxn]; 97 line Line[maxn]; 98 99 100 int main() 101 { 102 double a,b,c,d; 103 while(scanf("%lf %lf %lf %lf",&a,&b,&c,&d)!=EOF) 104 { 105 Line[1] = line(point(a,b),point(c,d)); 106 scanf("%lf %lf %lf %lf",&a,&b,&c,&d); 107 Line[2] = line(point(a,b),point(c,d)); 108 if(inter(Line[1],Line[2])) 109 { 110 printf("Yes-inter1 "); 111 } 112 else 113 { 114 printf("No-inter1 "); 115 } 116 if(inter2(Line[1],Line[2])) 117 { 118 printf("Yes-inter2 "); 119 } 120 else 121 { 122 printf("No-inter2 "); 123 } 124 } 125 }
以上是关于计算几何模板(点+线段)1.0的主要内容,如果未能解决你的问题,请参考以下文章