计算几何
题意还是比较好理解的,就是给出四个点p1,p2,p3,p4. 表示两条直线,求直线重合,相交,平行。
对着书写的。确实很好些,poj浮点数一定要用c++,g++poj不靠谱。 计算几何的叉积判断很好用。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;
struct point {
double x,y;
};
struct lin {
double a,b,c;
};
int line(double x1,double y1,double x2,double y2) { // 向量叉乘判断平行 == 0
return x1*y2 - y1*x2;
}
lin LINE(double x1,double y1,double x2,double y2) { // 求解直线方程
lin a;
a.a = y2 - y1;
a.b = x1 - x2;
a.c = x2*y1 - x1*y2;
return a;
}
double x,y;
void linster(lin l1,lin l2) { // x,y表示直线的交点
double d = l1.a*l2.b - l2.a*l1.b;
x = (l2.c*l1.b - l1.c*l2.b)/d;
y = (l2.a*l1.c - l1.a*l2.c)/d;
}
int main() {
int t;
scanf("%d",&t);
printf("INTERSECTING LINES OUTPUT\n");
while(t--) {
point p1,p2,p3,p4;
scanf("%lf %lf %lf %lf",&p1.x,&p1.y,&p2.x,&p2.y);
scanf("%lf %lf %lf %lf",&p3.x,&p3.y,&p4.x,&p4.y);
lin l1,l2;
if(line(p1.x - p2.x,p1.y - p2.y,p3.x - p1.x, p3.y-p1.y) == 0 && line(p1.x-p2.x,p1.y-p2.y,p4.x-p1.x,p4.y-p1.y) == 0)
printf("LINE\n");
else if(line(p2.x - p1.x,p2.y-p1.y,p3.x-p4.x,p3.y-p4.y) == 0)
printf("NONE\n");
else {
l1 = LINE(p1.x,p1.y,p2.x,p2.y);
l2 = LINE(p3.x,p3.y,p4.x,p4.y);
linster(l1,l2);
printf("POINT %.2lf %.2lf\n",x,y);
}
}
printf("END OF OUTPUT\n");
return 0;
}