poj 1410 joj 1131 实心矩形 与 线段相交

本题为英文题……

陷阱1:The terms top left and bottom right do not imply any ordering of coordinates.

陷阱2: The rectangle consists of four straight linesand the area in between

然后,其他正常判断相交即可



陷阱翻译:

1.给出的top left 和 right bottom 不一定按顺序给出……即这两个点4个坐标是乱序的

2.矩形内部所有的点都算矩形的


第一个陷阱………………当时没发现,加了个对角线相交判断误打误撞对了= =……

#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <cstring>
//浮点几何函数库
#include <math.h>
#define eps 1e-8
#define zero(x) (((x)>0?(x):-(x))<eps)
struct point{double x,y;};
struct line{point a,b;};
using namespace std;
//计算cross product (P1-P0)x(P2-P0)
double xmult(point p1,point p2,point p0){
	return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
double xmult(double x1,double y1,double x2,double y2,double x0,double y0){
	return (x1-x0)*(y2-y0)-(x2-x0)*(y1-y0);
}

//计算dot product (P1-P0).(P2-P0)
double dmult(point p1,point p2,point p0){
	return (p1.x-p0.x)*(p2.x-p0.x)+(p1.y-p0.y)*(p2.y-p0.y);
}
double dmult(double x1,double y1,double x2,double y2,double x0,double y0){
	return (x1-x0)*(x2-x0)+(y1-y0)*(y2-y0);
}

//两点距离
double distance(point p1,point p2){
	return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
double distance(double x1,double y1,double x2,double y2){
	return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}

//判三点共线
int dots_inline(point p1,point p2,point p3){
	return zero(xmult(p1,p2,p3));
}
int dots_inline(double x1,double y1,double x2,double y2,double x3,double y3){
	return zero(xmult(x1,y1,x2,y2,x3,y3));
}

//判点是否在线段上,包括端点
int dot_online_in(point p,line l){
	return zero(xmult(p,l.a,l.b))&&(l.a.x-p.x)*(l.b.x-p.x)<eps&&(l.a.y-p.y)*(l.b.y-p.y)<eps;
}
int dot_online_in(point p,point l1,point l2){
	return zero(xmult(p,l1,l2))&&(l1.x-p.x)*(l2.x-p.x)<eps&&(l1.y-p.y)*(l2.y-p.y)<eps;
}
int dot_online_in(double x,double y,double x1,double y1,double x2,double y2){
	return zero(xmult(x,y,x1,y1,x2,y2))&&(x1-x)*(x2-x)<eps&&(y1-y)*(y2-y)<eps;
}
//判两点在线段同侧,点在线段上返回0
int same_side(point p1,point p2,line l){
	return xmult(l.a,p1,l.b)*xmult(l.a,p2,l.b)>eps;
}
int same_side(point p1,point p2,point l1,point l2){
	return xmult(l1,p1,l2)*xmult(l1,p2,l2)>eps;
}
//判点是否在线段上,不包括端点
int dot_online_ex(point p,line l){
	return dot_online_in(p,l)&&(!zero(p.x-l.a.x)||!zero(p.y-l.a.y))&&(!zero(p.x-l.b.x)||!zero(p.y-l.b.y));
}
int dot_online_ex(point p,point l1,point l2){
	return dot_online_in(p,l1,l2)&&(!zero(p.x-l1.x)||!zero(p.y-l1.y))&&(!zero(p.x-l2.x)||!zero(p.y-l2.y));
}
int dot_online_ex(double x,double y,double x1,double y1,double x2,double y2){
	return dot_online_in(x,y,x1,y1,x2,y2)&&(!zero(x-x1)||!zero(y-y1))&&(!zero(x-x2)||!zero(y-y2));
}
int intersect_in(line u,line v){
	if (!dots_inline(u.a,u.b,v.a)||!dots_inline(u.a,u.b,v.b))
		return !same_side(u.a,u.b,v)&&!same_side(v.a,v.b,u);
	return dot_online_in(u.a,v)||dot_online_in(u.b,v)||dot_online_in(v.a,u)||dot_online_in(v.b,u);
}
int intersect_in(point u1,point u2,point v1,point v2){
	if (!dots_inline(u1,u2,v1)||!dots_inline(u1,u2,v2))
		return !same_side(u1,u2,v1,v2)&&!same_side(v1,v2,u1,u2);
	return dot_online_in(u1,v1,v2)||dot_online_in(u2,v1,v2)||dot_online_in(v1,u1,u2)||dot_online_in(v2,u1,u2);
}

line line,Up,Down,Right,Left,tem1,tem2;;
point Lt,Ld,Rt,Rd;
bool Judge1()
{
    tem1.a = Lt;
    tem1.b = Rd;
    tem2.a = Ld;
    tem2.b = Rt;
    if(intersect_in(line , Up)||
       intersect_in(line , Down)||
       intersect_in(line , Left)||
       intersect_in(line , Right))
//       intersect_in(line , tem1)||
//       intersect_in(line , tem2))
    return true;
    else
    return false;
}
//bool Judge2()
//{
//    if(ponls(line.a,Up) || ponls(line.a,Down) || ponls(line.a,Left) || ponls(line.a,Right)) return true;
//    else if (ponls(line.b,Up) || ponls(line.b,Down) || ponls(line.b,Left) || ponls(line.b,Right)) return true;
//    else return false;
//}
bool Judge3()
{
    if((line.a.x >= Left.a.x && line.a.x <= Right.a.x &&
       line.a.y >= Left.a.y && line.a.y <= Right.b.y) ||
       (line.b.x >= Left.a.x && line.b.x <= Right.a.x &&
       line.b.y >= Left.a.y && line.b.y <= Right.b.y ))
        return true;
    else
        return false;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        double a[8];
        for(int i=0; i<8; i++)
            scanf("%lf",&a[i]);
        if(a[4] > a[6])
            swap(a[4],a[6]);
        if(a[5] < a[7])
            swap(a[5],a[7]);
        line.a.x = a[0];
        line.a.y = a[1];
        line.b.x = a[2];
        line.b.y = a[3];
        Lt.x = a[4];
        Lt.y = a[5];
        Ld.x = a[4];
        Ld.y = a[7];
        Rt.x = a[6];
        Rt.y = a[5];
        Rd.x = a[6];
        Rd.y = a[7];
        Up.a = Lt;
        Up.b = Rt;
        Left.a = Ld;
        Left.b = Lt;
        Down.a = Ld;
        Down.b = Rd;
        Right.a = Rd;
        Right.b = Rt;

        if(Judge1() || Judge3())
            printf("T\n");
        else
            printf("F\n");
    }
    return 0;
}


以前的代码……(貌似更简洁=..=)

#include<stdio.h>
#include<stdlib.h>
#include <math.h>
#include <iostream>
#define eps 1e-8
#define zero(x) (((x)>0?(x):-(x))<eps)
using namespace std;
struct point
{
    double x,y;
};

struct line
{
    point a,b;
};

double xmult(point p1,point p2,point p0)
{
    return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}

int dot_online_in(point p,point l1,point l2)
{
    return zero(xmult(p,l1,l2))&&(l1.x-p.x)*(l2.x-p.x)<eps&&(l1.y-p.y)*(l2.y-p.y)<eps;
}

int dots_inline(point p1,point p2,point p3)
{
    return zero(xmult(p1,p2,p3));
}

int same_side(point p1,point p2,point l1,point l2)
{
    return xmult(l1,p1,l2)*xmult(l1,p2,l2)>eps;
}
//�ж��Ƿ��ཻ�����Ƿ���0��
int intersect_in(point u1,point u2,point v1,point v2)
{
    if (!dots_inline(u1,u2,v1)||!dots_inline(u1,u2,v2))
    {
        return !same_side(u1,u2,v1,v2)&&!same_side(v1,v2,u1,u2);
    }
    return dot_online_in(u1,v1,v2)||dot_online_in(u2,v1,v2)||dot_online_in(v1,u1,u2)||dot_online_in(v2,u1,u2);
}


int main()
{
//    freopen("1806.txt", "r", stdin);
    point target1, target2;
    point Lu, Ld, Ru, Rd;
    int T;
    cin>>T;
    while(T--)
    {
        cin>>target1.x>>target1.y>>target2.x>>target2.y>>Lu.x>>Lu.y>>Rd.x>>Rd.y;
        Ld.x = Lu.x;Ld.y = Rd.y;
        Ru.x = Rd.x; Ru.y = Lu.y;
        if(intersect_in(target1,target2,Lu,Ld)||intersect_in(target1, target2,Lu,Rd)||intersect_in(target1, target2,Lu,Ru)||intersect_in(target1, target2,Ld, Rd)||intersect_in(target1, target2,Ld, Ru)||intersect_in(target1, target2,Ru, Rd))
            cout<<"T"<<endl;
        else if((target1.x>=Lu.x&&target1.x<=Rd.x&&target1.y<=Lu.y&&target1.y>=Rd.y)||(target2.x>=Lu.x&&target2.x<=Rd.x&&target2.y<=Lu.y&&target2.y>=Rd.y))
            cout<<"T"<<endl;
        else
            cout<<"F"<<endl;
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值