51 nod 1264 线段相交

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
const int P = 1e-8;
using namespace std;


int main(){
int T, x[5], y[5], i, j;
double k[4], b[4];
double x1, y1;
scanf("%d", &T);
while(T--){
for(i = 1; i <= 4; ++i)
   scanf("%d%d", &x[i], &y[i]);
for(i = 1, j = 1; i <= 4; i = i + 2, ++j){
k[j] = (y[i + 1] - y[i]) * 1.0 / (x[i + 1] - x[i]); 
}
for(i = 1; i <= 2 ; ++i)
   b[i] = y[i + 1] - k[i] * x[i + 1];
if((int)b[1] != 0 && (int)b[2] != 0 && b[2] * k[1] == k[2] * b[1] && (int)k[1] * 100000 != (int)k[2] * 100000){
printf("No\n");
printf("%d %d", (int)k[1], (int)k[2]);
continue;
}
else if((int)b[1] != 0 && (int)b[2] != 0 && b[2] * k[1] == k[2] * b[1] && (int)k[1] * 100000 == (int)k[2] * 100000){
if(x[2] < x[1])
   swap(x[1], x[2]);
if((x[3] >= x[1] && x[3] <= x[2]) || (x[4] >= x[1] && x[4] <= x[2]))
printf("Yes\n");
else 
   printf("No\n");
}
else{
x1 = (b[2] - b[1]) * 1.0 / (k[1] - k[2]);
y1 = k[1] * x1 + b[1];
if(x1 <= min(max(x[1], x[2]), max(x[3], x[4])) && x1 >= max(min(x[1], x[2]), min(x[3], x[4])) && y1 <= min(max(y[1], y[2]), max(y[3], y[4])) && y1 >= max(min(y[1], y[2]), min(y[3], y[4])))
printf("Yes\n");
else 
   printf("No\n");
}

}
return 0;
}


15ms     1876KB


我是先把四个点求出直线,判是否相交,相交求出交点,判断是否在两线段上。。。有点麻烦,也许还有更加简洁的方法。以后会更得,大笑大笑


你可能感兴趣的:(51 nod 1264 线段相交)