POJ 3449 Geometric Shapes
判断两个多边形是否相交,只需判断边是否有相交。
编码量有点大,不过思路挺简单的。
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<string>
#include<queue>
#include<list>
#include<algorithm>
#include<iostream>
using namespace std; string s;
struct point
{
double x;
double y;
point (double a,double b){x=a;y=b;}
}; struct Sharp
{
string name;
vector<point>v;
vector<string> ans;
}sharp[];
int tot; bool cmp(const Sharp&a,const Sharp&b)
{
return a.name<b.name;
} void work()
{
int a,b,c,d,e,f;
if(s=="square")
{
scanf(" (%d,%d)",&a,&b);
scanf(" (%d,%d)",&c,&d);
point p1(1.0*a,1.0*b);
point p2(1.0*(a+b+c-d)/2.0,1.0*(-a+b+c+d)/2.0);
point p3(1.0*c,1.0*d);
point p4(1.0*(a-b+c+d)/2.0,1.0*(a+b-c+d)/2.0);
sharp[tot].v.push_back(p1);
sharp[tot].v.push_back(p2);
sharp[tot].v.push_back(p3);
sharp[tot].v.push_back(p4);
}
if(s=="rectangle")
{
scanf(" (%d,%d)",&a,&b);
scanf(" (%d,%d)",&c,&d);
scanf(" (%d,%d)",&e,&f);
point p1(1.0*a,1.0*b);
point p2(1.0*c,1.0*d);
point p3(1.0*e,1.0*f);
point p4(p1.x+p3.x-p2.x,p1.y+p3.y-p2.y);
sharp[tot].v.push_back(p1);
sharp[tot].v.push_back(p2);
sharp[tot].v.push_back(p3);
sharp[tot].v.push_back(p4);
}
if(s=="line")
{
for(int i=;i<=;i++)
{
scanf(" (%d,%d)",&a,&b);
point p(1.0*a,1.0*b);
sharp[tot].v.push_back(p);
}
}
if(s=="triangle")
{
for(int i=;i<=;i++)
{
scanf(" (%d,%d)",&a,&b);
point p(1.0*a,1.0*b);
sharp[tot].v.push_back(p);
}
}
if(s=="polygon")
{
int x;
scanf("%d",&x);
for(int i=;i<=x;i++)
{
scanf(" (%d,%d)",&a,&b);
point p(1.0*a,1.0*b);
sharp[tot].v.push_back(p);
}
}
tot++;
} const double eps=1e-;
#define zero(x)(((x)>0?(x):(-x))<eps) 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 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;
} 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 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);
} bool judge(int a,int b)
{
for(int i=;i<sharp[a].v.size();i++)
{
for(int j=;j<sharp[b].v.size();j++)
{
point p1=sharp[a].v[i];
point p2=sharp[a].v[(i+)%sharp[a].v.size()];
point p3=sharp[b].v[j];
point p4=sharp[b].v[(j+)%sharp[b].v.size()]; if(intersect_in(p1,p2,p3,p4)) return ;
}
}
return ;
} void init()
{
tot=;
for(int i=;i<;i++)
{
sharp[i].ans.clear();
sharp[i].v.clear();
}
}
int main()
{
while()
{
cin>>s;
if(s==".") break; init(); sharp[tot].name=s;
cin>>s; work(); while()
{
cin>>s;
if(s=="-") break; sharp[tot].name=s;
cin>>s; work();
} sort(sharp,sharp+tot,cmp); for(int i=;i<tot;i++)
{
for(int j=i+;j<tot;j++)
{
if(judge(i,j))
{
sharp[i].ans.push_back(sharp[j].name);
sharp[j].ans.push_back(sharp[i].name);
}
}
} for(int i=;i<tot;i++)
{
cout<<sharp[i].name<<" ";
if(sharp[i].ans.size()==) printf("has no intersections\n");
else if(sharp[i].ans.size()==) cout<<"intersects with "<<sharp[i].ans[]<<endl;
else if(sharp[i].ans.size()==) cout<<"intersects with "<<sharp[i].ans[]<<" and "<<sharp[i].ans[]<<endl;
else
{
printf("intersects with");
for(int k=;k<sharp[i].ans.size();k++)
{
if(k<sharp[i].ans.size()-) cout<<" "<<sharp[i].ans[k]<<",";
else cout<<" and "<<sharp[i].ans[k]<<endl;
}
}
}
printf("\n");
}
return ;
}
POJ 3449 Geometric Shapes的更多相关文章
- POJ 3449 Geometric Shapes(判断几个不同图形的相交,线段相交判断)
Geometric Shapes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 1243 Accepted: 524 D ...
- POJ 3449 Geometric Shapes (求正方形的另外两点)
Geometric Shapes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 1470 Accepted: 622 D ...
- POJ 3449 Geometric Shapes --计算几何,线段相交
题意: 给一些多边形或线段,输出与每一个多边形或线段的有哪一些多边形或线段. 解法: 想法不难,直接暴力将所有的图形处理成线段,然后暴力枚举,相交就加入其vector就行了.主要是代码有点麻烦,一步一 ...
- 简单几何(线段相交)+模拟 POJ 3449 Geometric Shapes
题目传送门 题意:给了若干个图形,问每个图形与哪些图形相交 分析:题目说白了就是处理出每个图形的线段,然后判断是否相交.但是读入输出巨恶心,就是个模拟题加上线段相交的判断,我第一次WA不知道输出要按字 ...
- POJ 3449 Geometric Shapes 判断多边形相交
题意不难理解,给出多个多边形,输出多边形间的相交情况(嵌套不算相交),思路也很容易想到.枚举每一个图形再枚举每一条边 恶心在输入输出,不过还好有sscanf(),不懂可以查看cplusplus网站 根 ...
- TZOJ 2560 Geometric Shapes(判断多边形是否相交)
描述 While creating a customer logo, ACM uses graphical utilities to draw a picture that can later be ...
- poj3449 Geometric Shapes【计算几何】
含[判断线段相交].[判断两点在线段两侧].[判断三点共线].[判断点在线段上]模板 Geometric Shapes Time Limit: 2000MS Memory Limit: 655 ...
- Geometric Shapes - POJ 3449(多边形相交)
题目大意:给一些几何图形的编号,求出来这些图形都和那些相交. 分析:输入的正方形对角线上的两个点,所以需要求出来另外两个点,公式是: x2:=(x1+x3+y3-y1)/2; y2:=(y1+y3 ...
- Geometric Shapes (poj3449多边形相交)
题意:给你一些多边形的点,判断每个多边形和那些多边形相交,编号按照字典序输出 思路:枚举每个多边形的每条边看是否相交,这里的相交是包括端点的,关键是给你正方形不相邻两个点求另外两个点怎么求,长方形给你 ...
随机推荐
- shell查看进程
用shell脚本监控进程是否存在 不存在则启动的实例,先上代码干货: #!/bin/shps -fe|grep processString |grep -v grepif [ $? -ne 0 ]th ...
- Lua基础(一)
1.Lua中有8个基本类型分别为:nil.boolean.number.string.userdata.function.thread和table 2.Lua中的表达式包括数字常量.字符串常量.变量. ...
- CSS单行、多行文本溢出显示省略号(……)
这个问题经常遇到 1.单行文本溢出显示省略号(…) text-overflow:ellipsis-----部分浏览器还需要加宽度width属性 overflow:hidden;text-overflo ...
- struts2中的值栈对象ValueStack
ValueStack, 即值栈对象. 值栈对象: 是整个struts数据存储的核心,或者叫中转站. 用户每次访问struts的action,都会创建一个Action对象.值栈对象.ActionCont ...
- web工程中地址的写法
硬盘地址用\ 如:c:\\ 虚拟地址用 / /* 只要是写地址,建议大家最好以"/"开头,/到底代表什么呢? 1.看这个地址是给服务器用的,还是给浏览器用的. 2.如果给浏览器 ...
- PHP上传图片三个步骤详细分析
学习PHP时,你可能会遇到PHP上传图片问题,这里将介绍PHP上传图片问题的解决方法,在这里拿出来和大家分享一下.今天我们就开始一起学习PHP上传图片.上传图片原理:首先判断文件类型是否为图片格式,若 ...
- cfdiv2/c/找规律
题目连接 £:若n<4,NO: £:若n==4,特判,n==5,特判. £:若n>=6,用2-4组成24,1和5和6组成零,即可. #include <set> #includ ...
- android 学习之RecyclerView
RecyclerView:ListView的升级版,它提供了更好的性能而且更容易使用.该控件是一个可以装载大量的视图集合,并且可以非常效率的进行回收和滚动.当你list中的元素经常动态改变时可以使用R ...
- hdu_5723_Abandoned country(最小生成树)
题目链接:hdu_5723_Abandoned country 题意: 让你求最小生成树的花费,然后求任给两点的期望路程 题解: 最小生成树大家都会求,Kruskal这里要改改,因为后面要求任意两点的 ...
- Counting Islands II
Counting Islands II 描述 Country H is going to carry out a huge artificial islands project. The projec ...