POJ 1648 Countryside Highway G++ 计算几何 判断线段是否相交 背

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
//英语      看博友分析    抄博友程序       计算几何      判断线段是否相交       背
struct point{
	double x;//int wa
	double y;
};
double cj(point a,point b,point c)
{
	return (a.x-b.x)*(c.y-b.y)-(a.y-b.y)*(c.x-b.x);
}
bool cross(point s1,point e1,point s2,point e2)
{
	if(max(s1.x,e1.x)>=min(s2.x,e2.x)&&
	   max(s2.x,e2.x)>=min(s1.x,e1.x)&&
	   max(s1.y,e1.y)>=min(s2.y,e2.y)&&
	   max(s2.y,e2.y)>=min(s1.y,e1.y)&&
	   cj(s2,e1,s1)*cj(e1,e2,s1)>=0&&//抄博友程序    背    ac
	   cj(s1,e2,s2)*cj(e2,e1,s2)>=0)
	   //cj(s2,s1,e1)*cj(e2,e1,s1)>=0&&//与博友程序一致   ac 
	   //cj(s1,s2,e2)*cj(e1,e2,s2)>=0)
	{
		  	return true;
	}else
	{
		return false;//不加百练wa 
	}
}
int main()
{
	double n,w,e;
	while(cin>>n>>w>>e)
	{
		point st, ed, p[4];
		st.x=0,st.y=w,ed.x=n*100,ed.y=e;//英语
		int js=0;
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<n;j++)
			{
				p[0].x=i*100,p[0].y=j*100;
				p[1].x=(i+1)*100,p[1].y=j*100;
				p[2].x=i*100,p[2].y=(j+1)*100;
				p[3].x=(i+1)*100,p[3].y=(j+1)*100;
				if(cross(st,ed,p[0],p[1])||cross(st,ed,p[0],p[2])
				||cross(st,ed,p[1],p[3])||cross(st,ed,p[2],p[3]))
				{
					js++;
				}
			}
		}
		cout<<js<<endl; 	
	} 
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值