1. 程式人生 > >計算幾何模板(點+線段)1.0

計算幾何模板(點+線段)1.0

計算幾何 int cst opera str point col cstring 弧度

  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\n"); 111 } 112 else 113 { 114 printf("No-inter1\n"); 115 } 116 if(inter2(Line[1],Line[2])) 117 { 118 printf("Yes-inter2\n"); 119 } 120 else 121 { 122 printf("No-inter2\n"); 123 } 124 } 125 }

計算幾何模板(點+線段)1.0