POJ 1269 Intersecting Lines(简单计算几何 · 直线与直线的位置关系)

博客介绍了POJ 1269编程题的解决方案,重点在于计算几何中判断两条直线是否相交的算法分析,并提供了相关代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、传送门

http://poj.org/problem?id=1269

二、算法分析说明

在这里插入图片描述在这里插入图片描述

三、代码

#include<cstdio>
#include<cmath>
#pragma warning(disable:4996)
template<class _Ty> struct point { _Ty x, y; };
unsigned n; point<double> a, b, c, d; double s1, s2, k;
template<class _Ty> inline _Ty cross_product(const _Ty& x1, const _Ty& y1, const _Ty& x2, const _Ty& y2) {
	return x1 * y2 - x2 * y1;
}
template<class _Ty> inline _Ty cross_product(const point<_Ty>& a, const point<_Ty>& b, const point<_Ty>& c, const point<_Ty>& d) {//cross product ab×cd
	return (b.x - a.x) * (d.y - c.y) - (d.x - c.x) * (b.y - a.y);
}
int main() {
	scanf("%u", &n); ++n; puts("INTERSECTING LINES OUTPUT");
	while (--n) {
		scanf("%lf%lf%lf%lf%lf%lf%lf%lf", &a.x, &a.y, &b.x, &b.y, &c.x, &c.y, &d.x, &d.y);
		if (cross_product(a, b, c, d) == 0) {
			if (cross_product(a, b, a, c) == 0) { puts("LINE"); continue; }
			puts("NONE"); continue;
		}
		s1 = cross_product(a, c, d, c); s2 = cross_product(d, c, b, c); k = s1 / (s1 + s2);
		printf("POINT %.2lf %.2lf\n", a.x + k * (b.x - a.x), a.y + k * (b.y - a.y));
	}
	puts("END OF OUTPUT");
	return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值