POJ 1410 Intersection 数据错误
题目要求判断一条线段和一个矩形是否相交,或者是否在矩形里面(题目好像没说?)
思路就是直接暴力判断和矩形四条边是否相交,和线段的坐标是否在矩形的坐标范围即可。
然后题目的数据,(xleft,ytop) 和 (xright,ybottom)不是按顺序给出的,需要自己判断下顺序。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = +;
struct coor
{
double x,y;
coor(){}
coor(double xx,double yy):x(xx),y(yy){}
double operator ^(coor rhs) const //计算叉积(向量积)
{
return x*rhs.y - y*rhs.x;
}
coor operator -(coor rhs) const //坐标相减,a-b得到向量ba
{
return coor(x-rhs.x,y-rhs.y);
}
double operator *(coor rhs) const //数量积
{
return x*rhs.x + y*rhs.y;
}
}a,b;
struct Line
{
coor point1,point2;
Line(){}
Line(coor xx,coor yy):point1(xx),point2(yy){}
}line[maxn],seg;
const double eps = 1e-;
bool same (double a,double b)
{
return fabs(a-b)<eps;
}
bool OnSegment (coor a,coor b,coor c) //判断点C是否在线段ab上
{
double min_x = min(a.x,b.x), min_y = min(a.y,b.y);
double max_x = max(a.x,b.x), max_y = max(a.y,b.y);
if (c.x>=min_x && c.x<=max_x && c.y>=min_y && c.y<=max_y) return true;
else return false;
}
bool SegmentIntersect (coor a,coor b,coor c,coor d)
{
double d1 = (b-a)^(d-a); //direction(a,b,d);以a为起点,计算ab和ab的叉积
double d2 = (b-a)^(c-a);
double d3 = (d-c)^(a-c);
double d4 = (d-c)^(b-c);
if (d1*d2< && d3*d4<) return true;
else if (same(d1,) && OnSegment(a,b,d)) return true;
else if (same(d2,) && OnSegment(a,b,c)) return true;
else if (same(d3,) && OnSegment(c,d,a)) return true;
else if (same(d4,) && OnSegment(c,d,b)) return true;
else return false;
}
void work ()
{
scanf("%lf%lf%lf%lf",&seg.point1.x,&seg.point1.y,&seg.point2.x,&seg.point2.y);
scanf("%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y);
if (a.x > b.x) swap(a.x,b.x);
if (a.y < b.y) swap(a.y,b.y);
line[] = Line(a,coor(b.x,a.y));
line[] = Line(coor(a.x,b.y),b);
line[] = Line(a,coor(a.x,b.y));
line[] = Line(coor(b.x,a.y),b);
for (int i=;i<=;++i)
{
if (SegmentIntersect(seg.point1,seg.point2,line[i].point1,line[i].point2))
{
printf ("T\n");
return ;
}
}
if (seg.point1.x>=a.x&&seg.point1.x<=b.x&&seg.point2.x>=a.x&&seg.point2.x<=b.x
&&seg.point1.y>=b.y&&seg.point1.y<=a.y&&seg.point2.y>=b.y&&seg.point2.y<=a.y)
{
printf ("T\n");
return ;
}
printf ("F\n");
return ;
} int main()
{
#ifdef local
freopen("data.txt","r",stdin);
#endif
int t;
cin>>t;
while(t--) work();
return ;
}
然而这题应该用判断点在多边形里比较好
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = +;
struct coor
{
double x,y;
coor(){}
coor(double xx,double yy):x(xx),y(yy){}
double operator ^(coor rhs) const //计算叉积(向量积)
{
return x*rhs.y - y*rhs.x;
}
coor operator -(coor rhs) const //坐标相减,a-b得到向量ba
{
return coor(x-rhs.x,y-rhs.y);
}
double operator *(coor rhs) const //数量积
{
return x*rhs.x + y*rhs.y;
}
}a,b,liu[maxn];
struct Line
{
coor point1,point2;
Line(){}
Line(coor xx,coor yy):point1(xx),point2(yy){}
}line[maxn],seg;
const double eps = 1e-;
bool same (double a,double b)
{
return fabs(a-b)<eps;
}
bool OnSegment (coor a,coor b,coor c) //判断点C是否在线段ab上
{
double min_x = min(a.x,b.x), min_y = min(a.y,b.y);
double max_x = max(a.x,b.x), max_y = max(a.y,b.y);
if (c.x>=min_x && c.x<=max_x && c.y>=min_y && c.y<=max_y) return true;
else return false;
}
bool SegmentIntersect (coor a,coor b,coor c,coor d)
{
double d1 = (b-a)^(d-a); //direction(a,b,d);以a为起点,计算ab和ab的叉积
double d2 = (b-a)^(c-a);
double d3 = (d-c)^(a-c);
double d4 = (d-c)^(b-c);
if (d1*d2< && d3*d4<) return true;
else if (same(d1,) && OnSegment(a,b,d)) return true;
else if (same(d2,) && OnSegment(a,b,c)) return true;
else if (same(d3,) && OnSegment(c,d,a)) return true;
else if (same(d4,) && OnSegment(c,d,b)) return true;
else return false;
}
bool PointInPolygon (coor p[],int n,coor cmp)
{
//思路:求解y=cmp.y与多边形一侧有多少个交点,奇数就在里面,偶数就在外面,cmp在边上是不行的
int cnt = ; //记录单侧有多少个交点,这里的p[],必须有顺序
for (int i=;i<=n;++i)
{
int t = (i+)>n ? :(i+); //下标为1要这样
coor p1=p[i],p2=p[t];
if (cmp.y >= max(p1.y,p2.y)) continue;//交点在延长线上和在凸顶点都不要
if (cmp.y < min(p1.y,p2.y)) continue;//交点在凹顶点上就要,这里没取等
if (same(p1.y,p2.y)) continue; //与cmp.y是平行的
double x = (cmp.y-p1.y)*(p1.x-p2.x)/(p1.y-p2.y) + p1.x; //求交点 p1.y != p2.y不会除0错误
if (x>cmp.x) cnt++;//只统计一侧的交点
}
return cnt&;
}
void work ()
{
scanf("%lf%lf%lf%lf",&seg.point1.x,&seg.point1.y,&seg.point2.x,&seg.point2.y);
scanf("%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y);
if (a.x > b.x) swap(a.x,b.x);
if (a.y < b.y) swap(a.y,b.y);
line[] = Line(a,coor(b.x,a.y));
line[] = Line(coor(a.x,b.y),b);
line[] = Line(a,coor(a.x,b.y));
line[] = Line(coor(b.x,a.y),b); liu[]=a;
liu[]=coor(b.x,a.y);
liu[]=b;
liu[]=coor(a.x,b.y); for (int i=;i<=;++i)
{
if (SegmentIntersect(seg.point1,seg.point2,line[i].point1,line[i].point2))
{
printf ("T\n");
return ;
}
}
if (PointInPolygon(liu,,seg.point1)&&PointInPolygon(liu,,seg.point2))
{
printf ("T\n");
return ;
}
printf ("F\n");
return ;
} int main()
{
#ifdef local
freopen("data.txt","r",stdin);
#endif
int t;
cin>>t;
while(t--) work();
return ;
}
POJ 1410 Intersection 数据错误的更多相关文章
- [POJ 1410] Intersection(线段与矩形交)
题目链接:http://poj.org/problem?id=1410 Intersection Time Limit: 1000MS Memory Limit: 10000K Total Sub ...
- POJ 1410 Intersection (计算几何)
题目链接:POJ 1410 Description You are to write a program that has to decide whether a given line segment ...
- POJ 1410 Intersection --几何,线段相交
题意: 给一条线段,和一个矩形,问线段是否与矩形相交或在矩形内. 解法: 判断是否在矩形内,如果不在,判断与四条边是否相交即可.这题让我发现自己的线段相交函数有错误的地方,原来我写的线段相交函数就是单 ...
- POJ 1410 Intersection(判断线段交和点在矩形内)
Intersection Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9996 Accepted: 2632 Desc ...
- POJ 1410 Intersection(线段相交&&推断点在矩形内&&坑爹)
Intersection 大意:给你一条线段,给你一个矩形,问是否相交. 相交:线段全然在矩形内部算相交:线段与矩形随意一条边不规范相交算相交. 思路:知道详细的相交规则之后题事实上是不难的,可是还有 ...
- POJ 1410 Intersection (线段和矩形相交)
题目: Description You are to write a program that has to decide whether a given line segment intersect ...
- poj 1410 Intersection (判断线段与矩形相交 判线段相交)
题目链接 Intersection Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12040 Accepted: 312 ...
- 简单几何(线段相交) POJ 1410 Intersection
题目传送门 题意:一个矩形和一条线段,问是否有相交 分析:考虑各种情况.坑点:给出的矩形的两个端点是无序的,还有线段完全在矩形内也算相交 /****************************** ...
- POJ 1410 Intersection(计算几何)
题目大意:题目意思很简单,就是说有一个矩阵是实心的,给出一条线段,问线段和矩阵是否相交解题思路:用到了线段与线段是否交叉,然后再判断线段是否在矩阵里面,这里要注意的是,他给出的矩阵的坐标明显不是左上和 ...
随机推荐
- Java的native关键字
一. 什么是Native Method 简单地讲,一个Native Method就是一个java调用非java代码的接口.一个Native Method是这样一个java的方法:该方法的实现由非j ...
- rsync 介绍和参数说明
Rsync 介绍: 我们经常需要在不同目录或者服务器之间做文件同步和更新,Linux提供了很多内置命令可以使用比如scp等等,但是今天我们介绍一个更加强大的工具rsync.rsync 命令是一个远程同 ...
- Python脚本开头两行:#!/usr/bin/python和# -*- coding: utf-8 -*-的作用
转于:https://www.crifan.com/python_head_meaning_for_usr_bin_python_coding_utf-8/ 出处:在路上 一.基本功能 1)#!/us ...
- windows7下安装gem包---bcrypt-ruby
在Gemfile中添加 gem 'bcrypt-ruby', '~> 3.0.0' 然后执行bundle install,rails服务启动没有问题,但是运行程序时页面报错如下: cannot ...
- Python模块-configparse模块
configparse模块用来解析配置文件 配置文件 [DEFAULT] port = 3306 socket = /tmp/mysql.sock [mysqldump] max_allowed_pa ...
- python的WeakKeyDictionary类和weakref模块的其他函数
python的WeakKeyDictionary类和weakref模块的其他函数 # -*- coding: utf-8 -*- # @Author : ydf # @Time : 2019/6/13 ...
- DevExpress 柱状图
通过构造函数,把值传递过来 public XtraInterpreterChartForm(object ds) { InitializeComponent(); datasource = ds; } ...
- .clearfix:after
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 关于startservice的几个启动返回值的意义
START_NOT_STICKY 如果服务进程在它启动后(从onStartCommand()返回后)被kill掉, 并且没有新启动的intent传给他, 那么将服务移出启动状态并且不重新生成, 直到再 ...
- #410div2C. Mike and gcd problem
C. Mike and gcd problem time limit per test 2 seconds memory limit per test 256 megabytes input stan ...