您的位置:首页 > 其它

POJ 1269 Intersecting Lines 判断直线相交

2014-11-11 11:01 429 查看
#include "stdio.h"
#include "math.h"
#define EPS 1e-8

struct point {
double x, y;
};
struct line {
struct point a, b;
};

double xProduct(struct point a, struct point b) {
return a.x*b.y - a.y*b.x;
}

void judge(struct line l1, struct line l2) {
struct point v1, v2;
v1.x = l1.b.x - l1.a.x;
v1.y = l1.b.y - l1.a.y;
v2.x = l2.b.x - l2.a.x;
v2.y = l2.b.y - l2.a.y;
double prod = xProduct(v1, v2);
if (fabs(prod) < EPS) {
struct point v3;
v3.x = l2.a.x - l1.a.x;
v3.y = l2.a.y - l1.a.y;
if (fabs(xProduct(v1, v3)) < EPS) {
printf("LINE\n");
} else {
printf("NONE\n");
}
} else {
double a = -v1.y;
double b = v1.x;
double c = l1.a.y*l1.b.x - l1.a.x*l1.b.y;
double d = -v2.y;
double e = v2.x;
double f = l2.a.y*l2.b.x - l2.a.x*l2.b.y;
double y = (c*d - f*a) / (b*d - a*e);
double x = (c*e - f*b) / (a*e - b*d);
printf("POINT %.2lf %.2lf\n", x, y);
}
}

int main() {
printf("INTERSECTING LINES OUTPUT\n");
int t;
struct line l1, l2;
scanf("%d", &t);
while (t--) {
scanf("%lf %lf %lf %lf %lf %lf %lf %lf",
&l1.a.x, &l1.a.y, &l1.b.x, &l1.b.y,
&l2.a.x, &l2.a.y, &l2.b.x, &l2.b.y);
judge(l1, l2);
}
printf("END OF OUTPUT\n");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  poj 计算几何