- #include<cstdio>
- #include<cstring>
- #include<algorithm>
- #include<cmath>
- using namespace std;
- const int INF = 0x3f3f3f3f;
- const int maxn = 100;
- const double eps = 1e-6;
- struct point
- {
- double x,y;
- point(double _x=0,double _y=0)
- {
- x = _x; y = _y;
- }
- };
- int sgn(double x)
- {
- if(fabs(x)<eps) return 0;
- if(x<0) return -1;
- else return 1;
- }
- // 向量 - 向量
- point operator -(const point &a, const point &b) {return point(a.x-b.x, a.y-b.y);}
- // 向量 + 向量
- point operator +(const point &a, const point &b) {return point(a.x+b.x, a.y+b.y);}
- // 向量 * 向量
- point operator *(const double &a, const point &b) {return point(a*b.x, a*b.y);}
- // 向量 * 常数 Dot
- double operator *(const point &a, const point &b) {return a.x*b.x+a.y*b.y;}
- // 向量 X 向量
- double operator ^(const point &a, const point &b) {return a.x*b.y-a.y*b.x;}
- // 向量 == 向量
- bool operator == (const point &a,const point &b) {return sgn(a.x-b.x)==0&&sgn(a.y-b.y)==0;}
- double Length(point a) {return sqrt(a*a);}
- //AB 夹角
- double Angle(point a,point b) {return acos( a*b/Length(a)/Length(b) );}
- //AB,AC 组成的平行四边形面积
- double Area(point a,point b,point c) {return (b-a)^(c-a);}
- //A 逆时针旋转弧度 rad 后的向量
- point Rotate(point a,double rad) {return point(a.x*cos(rad)-a.y*sin(rad),a.x*sin(rad)+a.y*cos(rad));}
- // 计算 A 的单位法线, 保证 A 不为 0 向量
- point Normal(point a)
- {
- double L = Length(a);
- return point(-a.y/L,a.x/L);
- }
- struct line
- {
- point s,e;
- line(){}
- line(point _s,point _e)
- {
- s = _s; e = _e;
- }
- };
- // 线段是否相交, 端点算相交
- bool inter(line l1,line l2)
- {
- return
- min(l1.s.x,l1.e.x)<=max(l2.s.x,l2.e.x) &&
- min(l2.s.x,l2.e.x)<=max(l1.s.x,l1.e.x) &&
- min(l1.s.y,l1.e.y)<=max(l2.s.y,l2.e.y) &&
- min(l2.s.y,l2.e.y)<=max(l1.s.y,l1.e.y) &&
- sgn((l2.s-l1.e)^(l1.s-l1.e)) * sgn((l2.e-l1.e)^(l1.s-l1.e))<=0 &&
- sgn((l1.s-l2.e)^(l2.s-l2.e)) * sgn((l1.e-l2.e)^(l2.s-l2.e))<=0;
- }
- // 线段是否相交, 端点不算相交
- bool inter2(line l1,line l2)
- {
- point a1=l1.s,a2=l1.e;
- point b1=l2.s,b2=l2.e;
- double c1 = (a2-a1)^(b1-a1), c2 = (a2-a1)^(b2-a1);
- double c3 = (b2-b1)^(a1-b1), c4 = (b2-b1)^(a2-b1);
- return sgn(c1)*sgn(c2)<0 && sgn(c3)*sgn(c4)<0;
- }
- // 求 p 边形的面积, res[0~n-1] 顺序存顶点, n 为顶点数, res[n]=res[0]
- double area()
- {
- double ret = 0;
- for(int i=0;i<n;i++)
- {
- int sgn = dcmp(cross(res[i],res[i+1]));
- ret += sgn*calc(res[i],res[i+1]);
- }
- return fabs(ret);
- }
- point p[maxn];
- line Line[maxn];
- int main()
- {
- double a,b,c,d;
- while(scanf("%lf %lf %lf %lf",&a,&b,&c,&d)!=EOF)
- {
- Line[1] = line(point(a,b),point(c,d));
- scanf("%lf %lf %lf %lf",&a,&b,&c,&d);
- Line[2] = line(point(a,b),point(c,d));
- if(inter(Line[1],Line[2]))
- {
- printf("Yes-inter1\n");
- }
- else
- {
- printf("No-inter1\n");
- }
- if(inter2(Line[1],Line[2]))
- {
- printf("Yes-inter2\n");
- }
- else
- {
- printf("No-inter2\n");
- }
- }
- }
来源: http://www.bubuko.com/infodetail-2701953.html