hdoj 1086 You can Solve a Geometry Problem too 【计算几何】

题意:就是判断各线段之间有没有交点。

判断两线段相交,要运用到叉积。两个线段相交肯定相互跨越,假设一个条线段(p1p2),另一条是(q1q2),那么p1p2肯定在q1q2线段的两侧,那么运用叉积如果p1p2跨越q1q2的话(q1p1)x(q2p2)《= 0.同样也要验证 q1q2是不是也跨越p1p2,注意:p1p2跨越q1q2,不代两个线段相交,可能是p1p2跨越直线q1q2,所以说还是要再次判断q1q2是不是跨越p1p2

还有另外一种比较容易理解的解法:

就是如果两个线段相交,那么两线段两端端点的差即(p1-q1)与(p2-q2)的乘积肯定是小于等于0的(代码略)要参考地址请戳http://blog.csdn.net/yu_ch_sh/article/details/38711239

代码1:

#include <stdio.h>
#include <math.h>
struct node{
	double x, y;
}s[150], e[150];
double chaji(node a, node b, node c)
{
	return (a.x - c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
}
int main()
{
	int n;
	while(scanf("%d", &n), n){
		int i, j;
		for(i = 0; i < n; i ++){
			scanf("%lf%lf%lf%lf", &s[i].x, &s[i].y, &e[i].x, &e[i].y);
		}
		int ans = 0;
		for(i = 0; i < n-1; i ++){
			for(j = i+1; j < n; j ++){
				double temp1 = chaji(s[i], e[i], s[j]);
				double temp2 = chaji(s[i], e[i], e[j]);
				double temp3 = chaji(s[j], e[j], s[i]);
				double temp4 = chaji(s[j], e[j], e[i]); 
				if(temp1*temp2 <= 0&&temp3*temp4 <= 0) ++ans;
			}
		}
		printf("%d\n", ans);
	}
}

题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1086

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值