题目链接:点击打开链接
题目意思很简单,就是问两个线段是否可以相交。
思路很是简单,两个线段互相跨立就可以相交了。所谓跨立就是,一条线段的两个点在另一条线段的两侧。
这个题目的数据有点水,没有考虑到点在线段上也是可以的。
code:(已经修改到最可能看懂的了)
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
struct Point{
double x, y;
};
struct Line{
Point a, b;
};
struct Vector{
double x, y;
};
double operator * (Vector a, Vector b){
return a.x * b.y - a.y * b.x;
}
Vector operator - (Point a, Point b){
Vector tmp;
tmp.x = a.x - b.x;
tmp.y = a.y - b.y;
return tmp;
}
bool Judge(Line L1, Line L2){
Vector A, B, C, A1, B1, C1;
A = L1.a - L1.b; B = L2.b - L1.b; C = L2.a - L1.b;
A1 = L2.a - L2.b; B1 = L1.b - L2.b; C1 = L1.a - L2.b;
double dir1 = A * B, dir2 = A * C, dir3 = A1 * B1, dir4 = A1 * C1;
if(dir1 * dir2 < 0 && dir3 * dir4 < 0) return true;
return false;
}
int main(){
int T;
scanf("%d", &T);
while(T --){
Line A, B;
scanf("%lf %lf %lf %lf %lf %lf %lf %lf", &A.a.x, &A.a.y, &A.b.x, &A.b.y, &B.a.x, &B.a.y, &B.b.x, &B.b.y);
if(Judge(A, B)) puts("Interseetion");
else puts("Not Interseetion");
}
return 0;
}
还是要慢慢的学习计算几何。