poj 1410 Intersection

本文介绍了一个算法问题,即如何判断给定的线段是否与矩形相交。通过具体的示例和C++代码实现,展示了如何确定线段与矩形是否有至少一个公共点。

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

problem:

Intersection

Time Limit:1000MS

Memory Limit:10000KB

64bit IO Format:%I64d & %I64u

Submit

Status

Description

You are to write a program that has to decide whether a given line segment intersects a given rectangle.

An example:

line: start point: (4,9)

end point: (11,2)

rectangle: left-top: (1,5)

right-bottom: (7,1)

Figure 1: Line segment does not intersect rectangle

The

line is said to intersect the rectangle if the line and the rectangle

have at least one point in common. The rectangle consists of four

straight lines and the area in between. Although all input values are

integer numbers, valid intersection points do not have to lay on the

integer grid.

Input

The input consists of n test cases. The first line of the input file

contains the number n. Each following line contains one test case of the

format:

xstart ystart xend yend xleft ytop xright ybottom

where

(xstart, ystart) is the start and (xend, yend) the end point of the

line and (xleft, ytop) the top left and (xright, ybottom) the bottom

right corner of the rectangle. The eight numbers are separated by a

blank. The terms top left and bottom right do not imply any ordering of

coordinates.

Output

For each test case in the input file, the output file should contain a

line consisting either of the letter "T" if the line segment intersects

the rectangle or the letter "F" if the line segment does not intersect

the rectangle.

Sample Input

1

4 9 11 2 1 5 7 1

Sample Output

F

实在是太坑。。

判断线段和矩形的四条边相交是不够的。。

因为矩形是实心的,要判断线段是否在矩形内部。。。

注意审题。。

我的代码实在太sb。。

code:

#include <iostream>

#include <stdio.h>

#include <math.h>

using namespace std;

#define eps 1e-6

#define N 6

struct line

{

double x1,x2,y1,y2;

}a,b;

inline int dcmp(double x)

{

if(fabs(x)<eps)

return 0;

return x>0?1:-1;

}

double x,y;

void cal()

{

double a1=a.y1-a.y2,b1=a.x2-a.x1,c1=a.x1*a.y2-a.x2*a.y1;

double a2=b.y1-b.y2,b2=b.x2-b.x1,c2=b.x1*b.y2-b.x2*b.y1;

x=(c1*b2-c2*b1)/(a2*b1-a1*b2);

y=(a2*c1-a1*c2)/(a1*b2-a2*b1);

}

bool judgep(line c,line d)

{

if((c.x1-c.x2)*(d.y1-d.y2)==(c.y1-c.y2)*(d.x1-d.x2)) return true;

else return false;

}

bool judget(line c,line d)

{

if((c.x2-c.x1)*(d.y1-c.y1)==(c.y2-c.y1)*(d.x1-c.x1)) return true;

else return false;

}

int ans;

int main()

{

int t;

cin>>t;

while(t--)

{

ans=0;

cin>>a.x1>>a.y1>>a.x2>>a.y2>>b.x1>>b.y1>>b.x2>>b.y2;

//cout<<b.x1<<" "<<b.y1<<" "<<b.x2<<" "<<b.y2<<endl;

if((a.x1-b.x1)*(a.x1-b.x2)<=0&&(a.y1-b.y1)*(a.y1-b.y2)<=0) ans=1;

if((a.x2-b.x1)*(a.x2-b.x2)<=0&&(a.y2-b.y1)*(a.y2-b.y2)<=0) ans=1;

if(judgep(a,b));

else

{

cal();

if((x-a.x1)*(x-a.x2)<=0&&(x-b.x1)*(x-b.x2)<=0&&(y-a.y1)*(y-a.y2)<=0&&(y-b.y1)*(y-b.y2)<=0) ans=1;

}

double t;

t=b.x2;

b.x2=b.x1;

b.x1=t;

//cout<<b.x1<<" "<<b.y1<<" "<<b.x2<<" "<<b.y2<<endl;

if(judgep(a,b));

else

{

cal();

if((x-a.x1)*(x-a.x2)<=0&&(x-b.x1)*(x-b.x2)<=0&&(y-a.y1)*(y-a.y2)<=0&&(y-b.y1)*(y-b.y2)<=0) ans=1;

}

double w1=b.x1,w2=b.x2,w3=b.y1,w4=b.y2;

b.x2=b.x1;

//cout<<b.x1<<" "<<b.y1<<" "<<b.x2<<" "<<b.y2<<endl;

if(judgep(a,b));

else

{

cal();

if((x-a.x1)*(x-a.x2)<=0&&(x-b.x1)*(x-b.x2)<=0&&(y-a.y1)*(y-a.y2)<=0&&(y-b.y1)*(y-b.y2)<=0) ans=1;

}

b.x2=w2;

b.y1=b.y2;

//cout<<b.x1<<" "<<b.y1<<" "<<b.x2<<" "<<b.y2<<endl;

if(judgep(a,b));

else

{

cal();

if((x-a.x1)*(x-a.x2)<=0&&(x-b.x1)*(x-b.x2)<=0&&(y-a.y1)*(y-a.y2)<=0&&(y-b.y1)*(y-b.y2)<=0) ans=1;

}

b.y1=w3;

b.x1=b.x2;

//cout<<b.x1<<" "<<b.y1<<" "<<b.x2<<" "<<b.y2<<endl;

if(judgep(a,b));

else

{

cal();

if((x-a.x1)*(x-a.x2)<=0&&(x-b.x1)*(x-b.x2)<=0&&(y-a.y1)*(y-a.y2)<=0&&(y-b.y1)*(y-b.y2)<=0) ans=1;

}

b.x1=w1;

b.y2=b.y1;

//cout<<b.x1<<" "<<b.y1<<" "<<b.x2<<" "<<b.y2<<endl;

if(judgep(a,b));

else

{

cal();

if((x-a.x1)*(x-a.x2)<=0&&(x-b.x1)*(x-b.x2)<=0&&(y-a.y1)*(y-a.y2)<=0&&(y-b.y1)*(y-b.y2)<=0) ans=1;

}

if(ans==0) cout<<"F"<<endl;

else cout<<"T"<<endl;

}

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值