poj1410——Intersection
程序员文章站
2022-04-02 16:56:54
...
大致题意:就是判断矩形有没有和线段相交的水题,等等,为什么别人说还要判断是不是在矩形内???但是我没判断啊???我怎么就过了????可能是数据水吧,其实判断线段在不在矩形内也很简单,大不了就多一点点代码量判断一下。算了,不添了,就当是生活给的一点小乐趣吧。
最后,代码:
#include<iostream>
using namespace std;
struct line
{
double x1,y1,x2,y2;
};
double multiple_cross(double x1,double y1,double x2,double y2)
{
return x1*y2-x2*y1;
}
bool check(line a,line b)
{
if(!(min(a.x1,a.x2)<=max(b.x1,b.x2)&&min(a.y1,a.y2)<=max(b.y1,b.y2)&&min(b.x1,b.x2)<=max(a.x1,a.x2)&&min(b.y1,b.y2)<=max(a.y1,a.y2)))
return false;
double u,v,w,z;
u=multiple_cross(a.x1-b.x1,a.y1-b.y1,b.x2-b.x1,b.y2-b.y1);
v=multiple_cross(a.x2-b.x1,a.y2-b.y1,b.x2-b.x1,b.y2-b.y1);
w=multiple_cross(b.x1-a.x1,b.y1-a.y1,a.x2-a.x1,a.y2-a.y1);
z=multiple_cross(b.x2-a.x1,b.y2-a.y1,a.x2-a.x1,a.y2-a.y1);
if(u*v<=0&&w*z<=0)
return true;
return false;
}
int main()
{
int n;
cin>>n;
while(n--)
{
line a,b,c;
cin>>a.x1>>a.y1>>a.x2>>a.y2;
cin>>b.x1>>b.y1>>b.x2>>b.y2;
bool flag=false;
c.x1=b.x1;
c.y1=b.y1;
c.x2=b.x1;
c.y2=b.y2;
if(check(a,c))
flag=true;
c.x2=b.x2;
c.y2=b.y1;
if(check(a,c))
flag=true;
c.x1=b.x2;
c.y1=b.y1;
if(check(a,c))
flag=true;
c.x1=b.x1;
c.y1=b.y2;
if(check(a,b))
flag=true;
if(flag)
cout<<"T"<<endl;
else
cout<<"F"<<endl;
}
return 0;
}
推荐阅读
-
[LC] 349. Intersection of Two Arrays
-
349. Intersection of Two Arrays
-
LeetCode刷题笔记(Intersection of Two Arrays II)
-
LeetCode 350. Intersection of Two Arrays II
-
leetcode 349. Intersection of Two Arrays(C语言)10
-
LeetCode 350. Intersection of Two Arrays II
-
Binary Search:349. Intersection of Two Arrays
-
Java中的Union Types和Intersection Types
-
HDUOJ 6786 Intersection
-
区域的集合运算---complement,difference,intersection,union1,union2