题意:判断是否线段有和给定的矩形右交点
解法:几何模板题
坑点:
不一定就是左上点和右下点
线段如果在矩形内也算是相交
- /*************************************************************************
- > File Name: poj1410.cpp
- > Author: cy
- > Mail: 1002@qq.com
- > Created Time: 14/11/10 19:38:24
- ************************************************************************/
- #include<iostream>
- #include<cstring>
- #include <algorithm>
- #include<cstdlib>
- #include<vector>
- #include<cmath>
- #include<stdlib.h>
- #include<iomanip>
- #include<list>
- #include<deque>
- #include<map>
- #include <stdio.h>
- #include <queue>
- const int maxn=1000+5;
- #define inf 0x3f3f3f3f
- #define INF 0x3FFFFFFFFFFFFFFFLL
- #define rep(i,n) for(i=0;i<n;i++)
- #define reP(i,n) for(i=1;i<=n;i++)
- #define ull unsigned long long
- #define ll long long
- #define cle(a) memset(a,0,sizeof(a))
- using namespace std;
- double eps= 0.00000001;
- struct point //定义点
- {
- double x;
- double y;
- };
- point operator -(point a,point b) //向量减法
- {
- point v;
- v.x=a.x-b.x;
- v.y=a.y-b.y;
- return v;
- }
- bool zero(double x)
- {
- return fabs(x)<eps?true:false;
- }
- double fork_mul(point a,point b) //向量叉积
- {
- return (a.x*b.y-a.y*b.x);
- }
- double min(double x,double y)
- {
- return x<y?x:y;
- }
- double max(double x,double y)
- {
- return x>y?x:y;
- }
- int intersect_sign(point a,point b,point c,point d,point &result) //两线段位置关系的标志
- {
- double cba=fork_mul(b-c,a-c);
- double dba=fork_mul(b-d,a-d),v12=fork_mul(b-a,d-c);
- if(zero(v12)==true) //两线段方向相同
- {
- if(zero(cba)==true&&zero(dba)==true) //两线段共线
- {
- if(min(a.x,b.x)<max(c.x,d.x)&&min(c.x,d.x)<max(a.x,b.x)&&min(a.y,b.y)<max(c.y,d.y)&&min(c.y,d.y)<max(a.y,b.y))
- return -3; //重叠共线
- if(min(a.x,b.x)>max(c.x,d.x)||min(c.x,d.x)>max(a.x,b.x)||min(a.y,b.y)>max(c.y,d.y)||min(c.y,d.y)>max(a.y,b.y))
- return -2; //分离共线
- return -1; //首尾相接
- }
- else
- return 0; //两线段平行
- }
- else //两线段方向不同,它们各自所在的直线相交
- {
- double bdc=fork_mul(d-b,c-b),adc=fork_mul(d-a,c-a);
- result.x=(dba*c.x-cba*d.x)/(dba-cba); //求两线段所在直线的交点
- result.y=(dba*c.y-cba*d.y)/(dba-cba);
- if(dba*cba<=0&&bdc*adc<=0)
- return 1; //线段相交
- else
- return 2; //线段不相交
- }
- }
- bool getbool(point a,point b,point c)//判断是否在矩形内
- {
- int minx=min(b.x,c.x);
- int maxx=max(b.x,c.x);
- int miny=min(b.y,c.y);
- int maxy=max(b.y,c.y);
- if(a.x>=minx&&a.x<=maxx&&a.y>=miny&&a.y<=maxy)
- {
- return true;
- }
- return false;
- }
- int main()
- {
- #ifndef ONLINE_JUDGE
- freopen("in.txt","r",stdin);
- //freopen("out.txt","w",stdout);
- #endif
- int n;
- cin>>n;
- while(n--)
- {
- point be,en;//线段的起点和终点
- scanf("%lf%lf%lf%lf",&be.x,&be.y,&en.x,&en.y);
- point upleft,downright,upright,downleft;
- scanf("%lf%lf%lf%lf",&upleft.x,&upleft.y,&downright.x,&downright.y);
- if(getbool(be,upleft,downright)||getbool(en,upleft,downright))
- {
- cout<<"T"<<endl;
- continue;
- }
- downleft.x=downright.x;downleft.y=upleft.y;
- upright.x=upleft.x;upright.y=downright.y;
- point temp;
- if(intersect_sign(be,en,upleft,upright,temp)%2==1||intersect_sign(be,en,upleft,downleft,temp)%2==1||intersect_sign(be,en,downleft,downright,temp)%2==1||intersect_sign(be,en,upright,downright,temp)%2==1)
- {
- cout<<"T"<<endl;
- }
- else{
- cout<<"F"<<endl;
- }
- }
- return 0;
- }