POJ 1269 Intersecting Lines 判断直线相交

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值