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多边形相交)
题意:给你一些多边形的点,判断每个多边形和那些多边形相交,编号按照字典序输出 思路:枚举每个多边形的每条边看是否相交,这里的相交是包括端点的,关键是给你正方形不相邻两个点求另外两个点怎么求,长方形给你 ...
随机推荐
- webstorm比dreamweaver强大的地方 转
比dw强大的地方有: . 对js的开发有长足的支持,那些自动提示,代码主题,调试之类的我就不说了,主要是对流行技术的支持,比如Node.js,less,sass,jq,ext,prototype等框架 ...
- .net网站报错:对象的当前状态使该操作无效
微软在2011年12月29号发布的2011年最后一个更新让哥哥为程序出现的异常头痛了一天. 这个异常在页面数据量小的时候并不会触发,只在页面数据量大的情况下才会出现,开始解决起来让人无从下手,最后才发 ...
- seo优化 标点符号
一.顿号“.” 顿号是一个只有在中文中使用的标点符号,这在英文中没有.毕竟该不该在标题中使用顿号呢,建议大家仍是不要使用,或者说在标题中就不要泛起中文的符号最好.不外,顿号可以使用在Descripti ...
- Html的第一次小结
一 Html的文档结构 (1) <html> 标记html文件的头标记,没有什么实质性的作用,但是却是必不可少的 (2) <head> 放置html文件信息.如css的一些 ...
- java模式:深入单例模式
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://devbean.blog.51cto.com/448512/203501 在GoF ...
- Cormen — The Best Friend Of a Man
Cormen — The Best Friend Of a Man time limit per test 1 second memory limit per test 256 megabytes i ...
- js框架——angular.js(5)
1. 3种过滤方式 <html> <head> <meta charset='utf-8'> <script src="js/angular.js& ...
- HDU 5768 Lucky7 (容斥原理 + 中国剩余定理 + 状态压缩 + 带膜乘法)
题意:……应该不用我说了,看起来就很容斥原理,很中国剩余定理…… 方法:因为题目中的n最大是15,使用状态压缩可以将所有的组合都举出来,然后再拆开成数组,进行中国剩余定理的运算,中国剩余定理能够求出同 ...
- 转:jmeter实践
本文主要介绍性能测试中的常用工具jmeter的使用方式,以方便开发人员在自测过程中就能自己动手对系统进行自动压测和模拟用户操作访问请求.最后还用linux下的压测工具ab做了简单对比. 1. ...
- AndroidGradle --多渠道打包配置(转发)
需求 国内Android app发布一般会有多个渠道,为了跟踪发展情况,通常会为每一个渠道定制一个特别的apk. 一般友盟之类第三方统计的渠道ID定义如下,以wandoujia为例 <meta- ...