我老人家要开始玩几何了!
。这个题有点自闭。
就是问是否存在一条直线经过所有了n条线段,(有交点).
我老人家愚昧不可救药,想了想决定先求出来 这两条直线的交点,然后看是否在线段上。但是一直写不对。。。
看了看题解发现可以直接用叉积,显然如果没有交点,那么线段在直线的一边,所以叉积就是正的,否则小于等于0.
1 #include <iostream> 2 #include <cmath> 3 #include <cstdio> 4 typedef double db; 5 const db eps=1e-8; 6 const db pi = acos(-1); 7 int sign(db k){ 8 if (k>eps) return 1; else if (k<-eps) return -1; return 0; 9 } 10 int cmp(db k1,db k2){return sign(k1-k2);} 11 struct point{ 12 db x,y; 13 db abs(){return sqrt(x*x+y*y);} 14 db dis(point k1){return ((*this)-k1).abs();} 15 point operator - (const point &k1) const{return (point){x-k1.x,y-k1.y};} 16 point operator *(db k1)const {return (point){x*k1,y*k1};} 17 point operator + (const point &k1) const{return (point){k1.x+x,k1.y+y};} 18 point operator / (db k1) const{return (point){x/k1,y/k1};} 19 int operator == (const point &k1) const{return cmp(x,k1.x)==0&&cmp(y,k1.y)==0;} 20 }; 21 db cross(point k1,point k2){return k1.x*k2.y-k1.y*k2.x;} 22 db dot(point k1,point k2){return k1.x*k2.x+k1.y*k2.y;} 23 struct Line{ 24 point p[2]; 25 }; 26 int inmid(db k1,db k2,db k3){return sign(k1-k3)*sign(k2-k3)<=0;} 27 int inmid(point k1,point k2,point k3){//k3在[k1,k2] 28 return inmid(k1.x,k2.x,k3.x)&&inmid(k1.y,k2.y,k3.y); 29 } 30 point getLL (point k1,point k2,point k3,point k4){//两直线交点 31 db w1=cross(k1-k3,k4-k3),w2=cross(k4-k3,k2-k3); 32 return (k1*w2+k2*w1)/(w1+w2); 33 } 34 bool onS(point k1,point k2,point q){//q在[k1,k2] 35 return inmid(k1,k2,q)&&sign(cross(k1-q,k2-k1))==0; 36 } 37 int t,n; 38 Line l[12306]; 39 bool slove(point s,point t){ 40 if(sign(s.dis(t)==0))return false; 41 for(int i=1;i<=n;i++){ 42 if(sign(cross(s-l[i].p[0],t-l[i].p[0])*sign(cross(s-l[i].p[1],t-l[i].p[1])))>0) 43 return false; 44 } 45 return true; 46 } 47 int main(){ 48 scanf("%d",&t); 49 while (t--){ 50 scanf("%d",&n); 51 for(int i=1;i<=n;i++){ 52 scanf("%lf%lf%lf%lf",&l[i].p[0].x,&l[i].p[0].y,&l[i].p[1].x,&l[i].p[1].y); 53 } 54 bool f=0; 55 for(int i=1;i<=n;i++){ 56 for(int j=1;j<=n;j++){ 57 if(slove(l[i].p[0],l[j].p[0])){ 58 f=1;break; 59 } 60 else if(slove(l[i].p[0],l[j].p[1])){ 61 f=1;break; 62 } 63 else if(slove(l[i].p[1],l[j].p[0])){ 64 f=1;break; 65 } 66 else if(slove(l[i].p[1],l[j].p[1])){ 67 f=1;break; } 68 }if(f)break; 69 } 70 if(!f) 71 printf("No!\n"); 72 else 73 printf("Yes!\n"); 74 } 75 } 76 /** 77 1 78 3 79 0.0 0.0 0.0 1.0 80 0.0 2.0 0.0 3.0 81 1.0 1.0 2.0 1.0 82 */
原文:https://www.cnblogs.com/MXang/p/10422451.html