poj 1410 Intersection 线段相交
题目链接
题意
判断线段和矩形是否有交点(矩形的范围是四条边及内部)。
思路
判断线段和矩形的四条边有无交点 && 线段是否在矩形内。
注意第二个条件。
Code
#include <cstdio>
#include <cmath>
#include <iostream>
#include <cstring>
#define inf 0x3f3f3f3f
#define eps 1e-6
#define maxn 110
using namespace std;
typedef long long LL;
struct POINT {
double x;
double y;
POINT(double a=0, double b=0) { x=a; y=b;} //constructor
};
struct LINESEG {
POINT s;
POINT e;
LINESEG(POINT a, POINT b) { s=a; e=b;}
LINESEG() { }
}seg[maxn];
double multiply(POINT sp,POINT ep,POINT op) {
return((sp.x-op.x)*(ep.y-op.y)-(ep.x-op.x)*(sp.y-op.y));
}
bool intersect(LINESEG u,LINESEG v) {
return( (max(u.s.x,u.e.x)>=min(v.s.x,v.e.x))&& //排斥实验
(max(v.s.x,v.e.x)>=min(u.s.x,u.e.x))&&
(max(u.s.y,u.e.y)>=min(v.s.y,v.e.y))&&
(max(v.s.y,v.e.y)>=min(u.s.y,u.e.y))&&
(multiply(v.s,u.e,u.s)*multiply(u.e,v.e,u.s)>=0)&& //跨立实验
(multiply(u.s,v.e,v.s)*multiply(v.e,u.e,v.s)>=0));
}
bool inside(LINESEG l, int x1, int y1, int x2, int y2) {
return l.s.x >= x1 && l.s.x <= x2 && l.s.y <= y1 && l.s.y >= y2;
return l.e.x >= x1 && l.e.x <= x2 && l.e.y <= y1 && l.e.y >= y2;
}
void work() {
int x1,y1,x2,y2,x3,y3,x4,y4;
scanf("%d%d%d%d%d%d%d%d", &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4);
if (x3 > x4) swap(x3, x4); if (y3 < y4) swap(y3, y4);
LINESEG l(POINT(x1, y1), POINT(x2, y2));
if (intersect(l, LINESEG(POINT(x3, y3), POINT(x3, y4))) || intersect(l, LINESEG(POINT(x3, y3), POINT(x4, y3)))
|| intersect(l, LINESEG(POINT(x4, y3), POINT(x4, y4))) || intersect(l, LINESEG(POINT(x3, y4), POINT(x4, y4)))
|| inside(l, x3, y3, x4, y4)) {
putchar('T');
}
else putchar('F');
printf("\n");
}
int main() {
int n;
scanf("%d", &n);
while (n--) work();
return 0;
}
poj 1410 Intersection 线段相交的更多相关文章
- POJ 1410 Intersection --几何,线段相交
题意: 给一条线段,和一个矩形,问线段是否与矩形相交或在矩形内. 解法: 判断是否在矩形内,如果不在,判断与四条边是否相交即可.这题让我发现自己的线段相交函数有错误的地方,原来我写的线段相交函数就是单 ...
- 简单几何(线段相交) POJ 1410 Intersection
题目传送门 题意:一个矩形和一条线段,问是否有相交 分析:考虑各种情况.坑点:给出的矩形的两个端点是无序的,还有线段完全在矩形内也算相交 /****************************** ...
- [POJ 1410] Intersection(线段与矩形交)
题目链接:http://poj.org/problem?id=1410 Intersection Time Limit: 1000MS Memory Limit: 10000K Total Sub ...
- POJ 1410 Intersection(判断线段交和点在矩形内)
Intersection Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9996 Accepted: 2632 Desc ...
- 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 1066(枚举+线段相交)
Treasure Hunt Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6328 Accepted: 2627 Des ...
- POJ 1410 Intersection(线段相交&&推断点在矩形内&&坑爹)
Intersection 大意:给你一条线段,给你一个矩形,问是否相交. 相交:线段全然在矩形内部算相交:线段与矩形随意一条边不规范相交算相交. 思路:知道详细的相交规则之后题事实上是不难的,可是还有 ...
- POJ 1410 Intersection (线段和矩形相交)
题目: Description You are to write a program that has to decide whether a given line segment intersect ...
随机推荐
- c++ 拷贝资源方法
#include "stdio.h" #include "stdlib.h" #include <sys/types.h> #include < ...
- (转发)IOS高级开发~Runtime(二)
一些公用类: @interface ClassCustomClass :NSObject{ NSString *varTest1; NSString *varTest2; NSString *varT ...
- 51nod——2489 小b和灯泡(打表/平方数)
这题打表去找因子的个数然后判奇偶也行.预处理O(n) 扫一遍判断O(n). ; i * i <= n; i++){ for(int j = i; i * j <= n; j++){ div ...
- luogu4608 [FJOI2016]所有公共子序列问题
题目描述: luogu loj 题解: 序列自动机(?)+高精+普及dp. 这个是猫老师的序列自动机(字符串从1开始): ]) { memset(t[n],-,sizeof(t[n])); ;i> ...
- 【概率dp 高斯消元】bzoj3270: 博物馆
一类成环概率dp的操作模式 Description 有一天Petya和他的朋友Vasya在进行他们众多旅行中的一次旅行,他们决定去参观一座城堡博物馆.这座博物馆有着特别的样式.它包含由m条走廊连接的n ...
- 【NOIP2017提高A组冲刺11.8】好文章
#include<algorithm> #include<iostream> #include<cstring> #include<cstdio> us ...
- 【Python学习之七】递归——汉诺塔问题的算法理解
汉诺塔问题 汉诺塔的移动可以用递归函数非常简单地实现.请编写move(n, a, b, c)函数,它接收参数n,表示3个柱子A.B.C中第1个柱子A的盘子数量,然后打印出把所有盘子从A借助B移动到C的 ...
- GTX 750等低配显卡如何玩转Deepfakes?
这里说的Deepfakes软件还是DeepFaceLab,人工智能换脸,是使用深度学习方法来实现的.而深度学习程序对电脑配置要求是非常高的,尤其是跑模型这个环节.很多低配电脑,根本就跑步起来.比如像G ...
- shutil,zipfile,tarfile模块
一,shutil模块 1.shutil.chown() shutil.chown('test.txt',user='mysql',group='mysql') #改变文件的属主和属组. 2.shuti ...
- drf 认证功能
drf(django rest-framework)认证组件 复习 HyperlinkedIdentityField ```python 功能:快速生成连接 1. publish = seriali ...