[poj1410]Intersection
题目大意:求线段与实心矩形是否相交。
解题关键:转化为线段与线段相交的判断。
#include<cstdio> #include<cstring> #include<algorithm> #include<cstdlib> #include<cmath> #include<iostream> #define eps 1e-8 using namespace std; typedef long long ll; struct Point{double x,y;Point(){}Point(double _x,double _y){x=_x;y=_y;}Point operator-(const Point &b)const{return Point(x - b.x,y - b.y);}double operator^(const Point &b)const{return x*b.y-y*b.x;}double operator*(const Point &b)const{return x*b.x+y*b.y;} }; struct Line{Point s,e;Line(){}Line(Point _s,Point _e){s=_s;e=_e;} }A[35]; int sgn(double x){if(fabs(x)<eps)return 0;else if(x<0) return -1;else return 1; } //判断线段相交,模板 bool inter(Line l1,Line l2){return max(l1.s.x,l1.e.x)>=min(l2.s.x,l2.e.x)&&max(l2.s.x,l2.e.x)>=min(l1.s.x,l1.e.x)&&max(l1.s.y,l1.e.y)>=min(l2.s.y,l2.e.y)&&max(l2.s.y,l2.e.y)>=min(l1.s.y,l1.e.y)&&sgn((l2.s-l1.s)^(l1.e-l1.s))*sgn((l2.e-l1.s)^(l1.e-l1.s))<=0&&sgn((l1.s-l2.s)^(l2.e-l2.s))*sgn((l1.e-l2.s)^(l2.e-l2.s))<=0; } int main(){ int t,i; double xleft,ytop,xright,ybottom; double x1,y1,x2,y2; scanf("%d",&t); while(t--){ scanf("%lf%lf%lf%lf",&A[0].s.x,&A[0].s.y,&A[0].e.x,&A[0].e.y);//线段 scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2); xleft=min(x1,x2);xright=max(x1,x2); ybottom=min(y1,y2);ytop=max(y1,y2); A[1].s.x=xleft;A[1].s.y=ybottom;A[1].e.x=xleft;A[1].e.y=ytop; A[2].s.x=xleft;A[2].s.y=ytop;A[2].e.x=xright;A[2].e.y=ytop; A[3].s.x=xright;A[3].s.y=ytop;A[3].e.x=xright;A[3].e.y=ybottom; A[4].s.x=xright;A[4].s.y=ybottom;A[4].e.x=xleft;A[4].e.y=ybottom;//矩形的四条线段 for(i=1;i<=4;++i) if(inter(A[0],A[i]))break;bool flag=false;//矩形是实心的。 if(A[0].s.x<=xright&&A[0].s.x>=xleft&&A[0].s.y>=ybottom&&A[0].s.y<=ytop)flag=true; if(A[0].e.x<=xright&&A[0].e.x>=xleft&&A[0].e.y>=ybottom&&A[0].e.y<=ytop)flag=true; if(i>4&&flag==0) printf("F\n");else printf("T\n"); } return 0; }
转载于:https://www.cnblogs.com/elpsycongroo/p/8729954.html
[poj1410]Intersection相关推荐
- CF 1093 E. Intersection of Permutations
E. Intersection of Permutations 链接 题意: 给定两个序列,询问第一个排列的[l1,r1]和第二个排列[l2,r2]中有多少个共同的数,支持在第二个排列中交换两个数. ...
- 【leetcode75】Intersection of Two Arrays(数组的交集)
题目描述: 给定两个数组求他们的公共部分,输出形式是数组,相同的元素只是输出一次 例如: nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2]. 原文描述: ...
- 一个Apache CollectionUtils.intersection 方法的简单问题
2019独角兽企业重金招聘Python工程师标准>>> 今天在使用CollectionUtils.intersection() 的时候,发现个问题,明明两个集合中有几个完全相同的类 ...
- Python计算两个numpy数组的交集(Intersection)实战:两个输入数组的交集并排序、获取交集元素及其索引、如果输入数组不是一维的,它们将被展平(flatten),然后计算交集
Python计算两个numpy数组的交集(Intersection)实战:两个输入数组的交集并排序.获取交集元素及其索引.如果输入数组不是一维的,它们将被展平(flatten),然后计算交集 目录
- python 集合set 的三大方法intersection union difference来处理文氏图
TODO - 练习:A或B,但不能同时包含 编写一个函数,将两个集合(set_a和set_b)作为输入,并返回一个新的集合,其中包含set_a或set_b中的元素,但不包含两者兼有的元素. 在上面的文 ...
- Leetcode: Intersection of Two Arrays
Given two arrays, write a function to compute their intersection.Example: Given nums1 = [1, 2, 2, 1] ...
- Intersection of Two Linked Lists——经典问题
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- [Swift]LeetCode160. 相交链表 | Intersection of Two Linked Lists
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...
- Intersection of Two Linked Lists 解答
Question Write a program to find the node at which the intersection of two singly linked lists begin ...
最新文章
- mysql索引为啥要选择B+树 (上)
- MongoDB实现ID自增长
- Spring Boot与日志 ——日志框架、日志配置||SLF4j使用||SpringBoot日志关系||切换日志框架
- 好看的论文千篇一律,有趣的Github项目万里挑一!
- 无线模块的电源设计指南
- Oracle Hint
- ASP.NET MVC学习
- 查看本机IP地址、测试本机与其他设备是否连通、查本机IP归属地
- Win10电脑版微信来消息提醒工具
- HTML学生个人网站作业设计:电影网站设计——橙色国外电影(13页) HTML+CSS+JavaScript 简单DIV布局个人介绍网页模板代码 DW学生个人网站制作成品下载
- 计算机操作系统—信号
- OLTP与OLAP简介
- 【论文解读】Cross-domain Correspondence Learning for Exemplar-based Image Translation
- Windows操作系统+朝鲜红星+国产麒麟+红旗+渗透专用系统+Oracle专用+技术专栏【资源大合集】 | 寻找C站宝藏
- 打开计算机 访问不到d盘,电脑d盘打不开怎么办,教你电脑d盘打不开怎么办
- iOS中使用基于RSA使用公钥加密和公钥解密
- 网络视频会议开发之如何进行噪音消除
- Java读取Excel中的图片,并保存
- Bit 与 Byte 认识
- 公文格式排版中的字体要求
热门文章
- 电脑黑屏的原因有哪些
- 有人说如果车速每小时超过120公里,爆胎时无法挽回是真的吗?
- 为什么越有钱的人负债越多?
- 如何将手机轻松投屏电脑?
- 32位和64位操作系统,二者有何区别?是否64位更快?
- [leetcode] Longest Common Prefix 字符窜最长公共前缀判断
- sql azure 语法_什么是Azure SQL Cosmos DB?
- sql limit 子句_SQL Join子句介绍和概述
- sql server 分区_使用分区归档SQL Server数据
- 如何在Power BI Desktop报表中使用Web数据源