标签:
向司老大学习开始学点计算几何..>_<
1.直线和线段交点(poj 3304)
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <cmath> 5 #include <algorithm> 6 using namespace std; 7 8 const double eps = 1e-8; 9 const int maxn = 1000+5; 10 int n; 11 12 int dcmp(double x){ 13 if(fabs(x) < eps) return 0; 14 if(x < 0) return -1; 15 return 1; 16 } 17 18 struct Point{ 19 double x,y; 20 Point(){} 21 Point(double _x,double _y){ 22 x = _x;y = _y; 23 } 24 Point operator - (const Point &b) const{ 25 return Point(x-b.x,y-b.y); 26 } 27 Point operator + (const Point &b) const{ 28 return Point(x+b.x,y+b.y); 29 } 30 double operator ^ (const Point &b) const{//叉积 31 return x*b.y - y*b.x; 32 } 33 double operator * (const Point &b) const{//点积 34 return x*b.x + y*b.y; 35 } 36 }; 37 38 struct Line{ 39 Point s,e; 40 Line(){} 41 Line(Point _s,Point _e){ 42 s = _s;e = _e; 43 } 44 }; 45 46 double xmult(Point p0,Point p1,Point p2){ 47 return (p1-p0) ^ (p2-p0); 48 } 49 50 bool Seg_inter_line(Line l1,Line l2){ 51 return dcmp(xmult(l2.s,l1.s,l1.e)) * dcmp(xmult(l2.e,l1.s,l1.e)) <= 0; 52 } 53 54 double dis(Point a,Point b){ 55 return sqrt( (b-a) * (b-a) ); 56 } 57 58 Line line[maxn]; 59 60 bool check(Line l1){ 61 if(dcmp(dis(l1.s,l1.e)) == 0) return false; 62 for(int i = 0;i < n;i++) { 63 if(Seg_inter_line(l1,line[i]) == false) return false; 64 } 65 return true; 66 } 67 68 void solve(){ 69 for(int i = 0;i < n;i++){ 70 for(int j = 0;j < n;j++){ 71 Point a = line[i].s; 72 Point b = line[i].e; 73 Point c = line[j].s; 74 Point d = line[j].e; 75 if(check(Line(a,c)) || check(Line(a,d)) || check(Line(b,c)) || check(Line (b,d))){ 76 puts("Yes!"); 77 return; 78 } 79 } 80 } 81 puts("No!"); 82 } 83 84 int main(){ 85 int T; 86 scanf("%d",&T); 87 while(T--){ 88 scanf("%d",&n); 89 double x1,x2,y1,y2; 90 for(int i = 0;i < n;i++){ 91 scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2); 92 line[i] = Line(Point(x1,y1),Point(x2,y2)); 93 } 94 solve(); 95 } 96 return 0; 97 }
标签:
原文地址:http://www.cnblogs.com/wuyuewoniu/p/5788980.html