poj3449(判断直线相交)
题目链接:https://vjudge.net/problem/POJ-3449
题意:给出若干几何体,判断每个几何体与其它几何体的相交情况,并依次输出。
思路:
首先要知道的是根据正方形对角线的两个点怎么求其它两个点,比如已知(x0,y0),(x2,y2),那么:
x1+x3=x0+x2,
x1-x3=y2-y0,
y1+y3=y0+y2,
y1-y3=x0-x2
之后就暴力枚举了,枚举所有几何体的所有边,用线段相交判断几何体相交。这题的输入输出很恶心。
AC代码:
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<cstring>
using namespace std; const double eps=1e-;
int sgn(double x){
if(abs(x)<eps) return ;
else if(x<) return -;
else return ;
} struct Point{
double x,y;
Point(){}
Point(double xx,double yy){
x=xx,y=yy;
}
Point operator + (const Point& b)const{
return Point(x+b.x,y+b.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;
}
}; struct Line{
Point s,e;
Line(){}
Line(Point ss,Point ee){
s=ss,e=ee;
}
}; 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.e)^(l1.s-l1.e))*sgn((l2.e-l1.e)^(l1.s-l1.e))<=&&
sgn((l1.s-l2.e)^(l2.s-l2.e))*sgn((l1.e-l2.e)^(l2.s-l2.e))<=;
} struct node{
char id;
int num;
Point pt[];
}sp[]; bool cmp(node a,node b){
return a.id<b.id;
} bool check(node a,node b){
for(int i=;i<a.num;++i)
for(int j=;j<b.num;++j)
if(inter(Line(a.pt[i],a.pt[(i+)%a.num]),Line(b.pt[j],b.pt[(j+)%b.num])))
return true;
return false;
} vector<char> vc;
int n;
char str[]; int main(){
while(scanf("%s",str),str[]!='.'){
sp[].id=str[];
scanf("%s",str);
if(strcmp(str,"square")==){
sp[].num=;
scanf(" (%lf,%lf)",&sp[].pt[].x,&sp[].pt[].y);
scanf(" (%lf,%lf)",&sp[].pt[].x,&sp[].pt[].y);
sp[].pt[].x=(sp[].pt[].x+sp[].pt[].x+(sp[].pt[].y-sp[].pt[].y))/;
sp[].pt[].x=(sp[].pt[].x+sp[].pt[].x-(sp[].pt[].y-sp[].pt[].y))/;
sp[].pt[].y=(sp[].pt[].y+sp[].pt[].y+(sp[].pt[].x-sp[].pt[].x))/;
sp[].pt[].y=(sp[].pt[].y+sp[].pt[].y-(sp[].pt[].x-sp[].pt[].x))/;
}
else if(strcmp(str,"rectangle")==){
sp[].num=;
scanf(" (%lf,%lf)",&sp[].pt[].x,&sp[].pt[].y);
scanf(" (%lf,%lf)",&sp[].pt[].x,&sp[].pt[].y);
scanf(" (%lf,%lf)",&sp[].pt[].x,&sp[].pt[].y);
sp[].pt[].x=sp[].pt[].x+(sp[].pt[].x-sp[].pt[].x);
sp[].pt[].y=sp[].pt[].y+(sp[].pt[].y-sp[].pt[].y);
}
else if(strcmp(str,"line")==){
sp[].num=;
scanf(" (%lf,%lf)",&sp[].pt[].x,&sp[].pt[].y);
scanf(" (%lf,%lf)",&sp[].pt[].x,&sp[].pt[].y);
}
else if(strcmp(str,"triangle")==){
sp[].num=;
scanf(" (%lf,%lf)",&sp[].pt[].x,&sp[].pt[].y);
scanf(" (%lf,%lf)",&sp[].pt[].x,&sp[].pt[].y);
scanf(" (%lf,%lf)",&sp[].pt[].x,&sp[].pt[].y);
}
else{
scanf("%d",&sp[].num);
for(int i=;i<sp[].num;++i)
scanf(" (%lf,%lf)",&sp[].pt[i].x,&sp[].pt[i].y);
}
n=;
while(scanf("%s",str),str[]!='-'){
sp[n].id=str[];
scanf("%s",str);
if(strcmp(str,"square")==){
sp[n].num=;
scanf(" (%lf,%lf)",&sp[n].pt[].x,&sp[n].pt[].y);
scanf(" (%lf,%lf)",&sp[n].pt[].x,&sp[n].pt[].y);
sp[n].pt[].x=(sp[n].pt[].x+sp[n].pt[].x+(sp[n].pt[].y-sp[n].pt[].y))/;
sp[n].pt[].x=(sp[n].pt[].x+sp[n].pt[].x-(sp[n].pt[].y-sp[n].pt[].y))/;
sp[n].pt[].y=(sp[n].pt[].y+sp[n].pt[].y+(sp[n].pt[].x-sp[n].pt[].x))/;
sp[n].pt[].y=(sp[n].pt[].y+sp[n].pt[].y-(sp[n].pt[].x-sp[n].pt[].x))/;
}
else if(strcmp(str,"rectangle")==){
sp[n].num=;
scanf(" (%lf,%lf)",&sp[n].pt[].x,&sp[n].pt[].y);
scanf(" (%lf,%lf)",&sp[n].pt[].x,&sp[n].pt[].y);
scanf(" (%lf,%lf)",&sp[n].pt[].x,&sp[n].pt[].y);
sp[n].pt[].x=sp[n].pt[].x+(sp[n].pt[].x-sp[n].pt[].x);
sp[n].pt[].y=sp[n].pt[].y+(sp[n].pt[].y-sp[n].pt[].y);
}
else if(strcmp(str,"line")==){
sp[n].num=;
scanf(" (%lf,%lf)",&sp[n].pt[].x,&sp[n].pt[].y);
scanf(" (%lf,%lf)",&sp[n].pt[].x,&sp[n].pt[].y);
}
else if(strcmp(str,"triangle")==){
sp[n].num=;
scanf(" (%lf,%lf)",&sp[n].pt[].x,&sp[n].pt[].y);
scanf(" (%lf,%lf)",&sp[n].pt[].x,&sp[n].pt[].y);
scanf(" (%lf,%lf)",&sp[n].pt[].x,&sp[n].pt[].y);
}
else{
scanf("%d",&sp[n].num);
for(int i=;i<sp[n].num;++i)
scanf(" (%lf,%lf)",&sp[n].pt[i].x,&sp[n].pt[i].y);
}
++n;
}
sort(sp,sp+n,cmp);
for(int i=;i<n;++i){
printf("%c ",sp[i].id);
vc.clear();
for(int j=;j<n;++j)
if(j!=i)
if(check(sp[i],sp[j]))
vc.push_back(sp[j].id);
if(vc.size()==){
printf("has no intersections\n");
}
else if(vc.size()==){
printf("intersects with %c\n",vc[]);
}
else if(vc.size()==){
printf("intersects with %c and %c\n",vc[],vc[]);
}
else{
printf("intersects with ");
for(int j=;j<vc.size()-;++j)
printf("%c, ",vc[j]);
printf("and %c\n",vc[vc.size()-]);
}
}
printf("\n");
}
return ;
}
poj3449(判断直线相交)的更多相关文章
- Revit API判断直线相交关系移动风管
start ) ); )) )) ); XYZ xyz12 = lCurve1.Curve.get_EndPoint(); XY ...
- [poj] 1066 Treasure Hunt || 判断直线相交
原题 在金字塔内有一个宝藏p(x,y),现在要取出这个宝藏. 在金字塔内有许多墙,为了进入宝藏所在的房间必须把墙炸开,但是炸墙只能炸每个房间墙的中点. 求将宝藏运出城堡所需要的最小炸墙数. 判断点和直 ...
- POJ 1269 Intersecting Lines【判断直线相交】
题意:给两条直线,判断相交,重合或者平行 思路:判断重合可以用叉积,平行用斜率,其他情况即为相交. 求交点: 这里也用到叉积的原理.假设交点为p0(x0,y0).则有: (p1-p0)X(p2-p0) ...
- CC34:判断直线相交
题目 解法 水题,判断斜率.判断截距,ok..... class CrossLine { public: bool checkCrossLine(double s1, double s2, doubl ...
- poj 1127(直线相交+并查集)
Jack Straws Description In the game of Jack Straws, a number of plastic or wooden "straws" ...
- poj 1269 Intersecting Lines(直线相交)
Intersecting Lines Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8637 Accepted: 391 ...
- POJ 1269 Intersecting Lines(直线相交判断,求交点)
Intersecting Lines Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8342 Accepted: 378 ...
- 判断线段和直线相交 POJ 3304
// 判断线段和直线相交 POJ 3304 // 思路: // 如果存在一条直线和所有线段相交,那么平移该直线一定可以经过线段上任意两个点,并且和所有线段相交. #include <cstdio ...
- 判断直线与线段相交 POJ 3304 Segments
题意:在二维平面中,给定一些线段,然后判断在某直线上的投影是否有公共点. 转化,既然是投影,那么就是求是否存在一条直线L和所有的线段都相交. 证明: 下面给出具体的分析:先考虑一个特殊的情况,即n=1 ...
随机推荐
- 001_Python安装
1. 判断电脑是否已经安装python,电脑->运行->输入:python 2. 如果没有安装,去官网下载:https://www.python.org/downloads/ 3. 安装过 ...
- 独立看门狗 IWDG
一,独立看门狗 二,独立看门狗的时钟源 独立看门狗拥有自己的时钟源,不依赖PLL时钟输出的分频信号,能够独立运行,这样子的好处就是PLL假如受到干扰, 导致运行异常,独立的看门狗还能正常地进行工作,如 ...
- 牛客小白月赛11 Rinne Loves Xor
题目链接:https://ac.nowcoder.com/acm/contest/370/I code: #include<bits/stdc++.h> using namespace s ...
- MySQL数据分析-(2)数据库的底层逻辑
(一) 数据库存在的逻辑 1.案例开篇-大部分公司对于数据和数字的管理都是低效率的 我们要学习数据库,就必须要搞清楚数据库是在什么样的情景下发明并流行的?学习新知识就要搞清楚每个知识点的来龙去脉,这样 ...
- Spring Boot教程(七)通过springboot 去创建和提交一个表单
创建工程 涉及了 web,加上spring-boot-starter-web和spring-boot-starter-thymeleaf的起步依赖. <dependencies> < ...
- 微信小程序之简单记账本开发记录(三)
昨天已经编辑了主界面,在wxml文件中设置好跳转链接之后,就可以进行下一步的开发了 在pages中建立一个新的页面文件夹作为之后的支出页面 编辑后台,今天先搭建大致界面
- 一个 TCP 连接可以发多少个 HTTP 请求
第一个问题 第二个问题 第三个问题 第四个问题 第五个问题 曾经有这么一道面试题:从 URL 在浏览器被被输入到页面展现的过程中发生了什么? 相信大多数准备过的同学都能回答出来,但是如果继续问:收到的 ...
- MyBatis错误:The server time zone value '?泄???????' is unrecognized or represents more t
原文地址:http://blog.csdn.net/oppo5630/article/details/52162783 解决java.sql.SQLException: The server time ...
- shell 變數
echo $? 上个命令的退出状态,或函数的返回值. ref: http://c.biancheng.net/cpp/view/2739.html
- About Xi’an
Introduction Ancient Capital It is the birthplace of the Chinese Nation, is one of the four ancient ...