TZOJ 2560 Geometric Shapes(判断多边形是否相交)
描述
While creating a customer logo, ACM uses graphical utilities to draw a picture that can later be cut into special fluorescent materials. To ensure proper processing, the shapes in the picture cannot intersect. However, some logos contain such intersecting shapes. It is necessary to detect them and decide how to change the picture.
Given a set of geometric shapes, you are to determine all of their intersections. Only outlines are considered, if a shape is completely inside another one, it is not counted as an intersection.

输入
Input contains several pictures. Each picture describes at most 26 shapes, each specified on a separate line. The line begins with an uppercase letter that uniquely identifies the shape inside the corresponding picture. Then there is a kind of the shape and two or more points, everything separated by at least one space. Possible shape kinds are:
• square: Followed by two distinct points giving the opposite corners of the square.
•
rectangle: Three points are given, there will always be a right angle
between the lines connecting the first point with the second and the
second with the third.
• line: Specifies a line segment, two distinct end points are given.
• triangle: Three points are given, they are guaranteed not to be co-linear.
•
polygon: Followed by an integer number N (3 ≤ N ≤ 20) and N points
specifying vertices of the polygon in either clockwise or anti-clockwise
order. The polygon will never intersect itself and its sides will have
non-zero length.
All points are always given as two integer coordinates X and Y
separated with a comma and enclosed in parentheses. You may assume that
|X|, |Y | ≤ 10000.
The picture description is terminated by a line containing a single
dash (“-”). After the last picture, there is a line with one dot (“.”).
输出
For
each picture, output one line for each of the shapes, sorted
alphabetically by its identifier (X). The line must be one of the
following:
• “X has no intersections”, if X does not intersect with any other shapes.
• “X intersects with A”, if X intersects with exactly 1 other shape.
• “X intersects with A and B”, if X intersects with exactly 2 other shapes.
• “X intersects with A, B, . . ., and Z”, if X intersects with more than 2 other shapes.
Please note that there is an additional comma for more than two
intersections. A, B, etc. are all intersecting shapes, sorted
alphabetically.
Print one empty line after each picture, including the last one.
样例输入
A square (1,2) (3,2)
F line (1,3) (4,4)
W triangle (3,5) (5,5) (4,3)
X triangle (7,2) (7,4) (5,3)
S polygon 6 (9,3) (10,3) (10,4) (8,4) (8,1) (10,2)
B rectangle (3,3) (7,5) (8,3)
-
B square (1,1) (2,2)
A square (3,3) (4,4)
-
.
样例输出
A has no intersections
B intersects with S, W, and X
F intersects with W
S intersects with B
W intersects with B and F
X intersects with B
A has no intersections
B has no intersections
题意
给你多边形,如果在内部则视为不相交,判断哪些是相交的
题解
把多边形按边存,如果两个多边形相交,那么一定存在两条边相交
判断两条边相交,先用俩矩形快速排斥,再用跨立实验,如果ab和cd相交,那么cd的两端一定在向量ab的两侧,可以通过abc和abd叉积相乘<0判断是否相交
然后就是存多边形,这里正方形和矩形另外的点得通过向量计算一下
PS:码农题,读输入,输出都恶心,题目不算太难
代码
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;
struct point
{
double x,y;
point(double x=,double y=):x(x),y(y){}
};
bool judge(point a,point b,point c,point d)
{
if(!(min(a.x,b.x)<=max(c.x,d.x)&&min(c.y,d.y)<=max(a.y,b.y)&&min(c.x,d.x)<=max(a.x,b.x)&&min(a.y,b.y)<=max(c.y,d.y)))
return false;
double u,v,w,z;
u=(c.x-a.x)*(b.y-a.y)-(b.x-a.x)*(c.y-a.y);
v=(d.x-a.x)*(b.y-a.y)-(b.x-a.x)*(d.y-a.y);
w=(a.x-c.x)*(d.y-c.y)-(d.x-c.x)*(a.y-c.y);
z=(b.x-c.x)*(d.y-c.y)-(d.x-c.x)*(b.y-c.y);
return (u*v<=0.00000001&&w*z<=0.00000001);
}
vector<point>G[]; int main()
{
//freopen("A.txt","w",stdout);
int m;
double a,b;
char op[],shape[];
while(scanf("%s",op)!=EOF,op[]!='.')
{
for(int i=;i<;i++)G[i].clear();
while(op[]!='-')
{
int cnt=op[]-'A';
scanf("%s",shape);
if(shape[]=='s')///正方形
{
for(int i=;i<=;i++)
{
scanf(" (%lf,%lf)",&a,&b);
G[cnt].push_back(point(a,b));
}
double A=G[cnt][].x,B=G[cnt][].y,C=G[cnt][].x,D=G[cnt][].y;
G[cnt].push_back(point((A*1.0+B+C-D)/2.0,(-A*1.0+B+C+D)/2.0));
G[cnt].push_back(point((A*1.0-B+C+D)/2.0,(A*1.0+B-C+D)/2.0));
swap(G[cnt][],G[cnt][]);
G[cnt].push_back(G[cnt][]);
}
if(shape[]=='r')///矩形
{
for(int i=;i<=;i++)
{
scanf(" (%lf,%lf)",&a,&b);
G[cnt].push_back(point(a,b));
}
G[cnt].push_back(point(G[cnt][].x*1.0+G[cnt][].x-G[cnt][].x,G[cnt][].y*1.0+G[cnt][].y-G[cnt][].y));
G[cnt].push_back(G[cnt][]);
}
if(shape[]=='l')///线
{
for(int i=;i<=;i++)
{
scanf(" (%lf,%lf)",&a,&b);
G[cnt].push_back(point(a,b));
}
}
if(shape[]=='t')///三角形
{
for(int i=;i<=;i++)
{
scanf(" (%lf,%lf)",&a,&b);
G[cnt].push_back(point(a,b));
}
G[cnt].push_back(G[cnt][]);
}
if(shape[]=='p')///多边形
{
scanf("%d",&m);
for(int i=;i<=m;i++)
{
scanf(" (%lf,%lf)",&a,&b);
G[cnt].push_back(point(a,b));
}
G[cnt].push_back(G[cnt][]);
}
scanf("%s",op);
}
for(int i=;i<;i++)
{
int flag=;
set<int>SET;
if((int)G[i].size()==)continue;
for(int j=;j<(int)G[i].size()-;j++)
{
for(int k=;k<;k++)
{
if((int)G[k].size()==||i==k)continue;
for(int l=;l<(int)G[k].size()-;l++)
{
if(judge(G[i][j],G[i][j+],G[k][l],G[k][l+]))
{
flag=;
SET.insert(k);
break;
}
}
}
}
if(flag==)
{
vector<int>VEC(SET.begin(),SET.end());
int len=(int)VEC.size();
printf("%c intersects with",i+'A');
if(len==)
{printf(" %c and %c\n",VEC[]+'A',VEC[]+'A');continue;}
for(int l=;l<len-;l++)
printf(" %c,",VEC[l]+'A');
if(len>)
printf(" and %c",VEC[len-]+'A');
else
printf(" %c",VEC[len-]+'A');
printf("\n");
}
else
printf("%c has no intersections\n",i+'A');
}
printf("\n");
}
return ;
}
TZOJ 2560 Geometric Shapes(判断多边形是否相交)的更多相关文章
- POJ 3449 Geometric Shapes 判断多边形相交
题意不难理解,给出多个多边形,输出多边形间的相交情况(嵌套不算相交),思路也很容易想到.枚举每一个图形再枚举每一条边 恶心在输入输出,不过还好有sscanf(),不懂可以查看cplusplus网站 根 ...
- POJ 3449 Geometric Shapes(判断几个不同图形的相交,线段相交判断)
Geometric Shapes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 1243 Accepted: 524 D ...
- Geometric Shapes (poj3449多边形相交)
题意:给你一些多边形的点,判断每个多边形和那些多边形相交,编号按照字典序输出 思路:枚举每个多边形的每条边看是否相交,这里的相交是包括端点的,关键是给你正方形不相邻两个点求另外两个点怎么求,长方形给你 ...
- POJ 3449 Geometric Shapes --计算几何,线段相交
题意: 给一些多边形或线段,输出与每一个多边形或线段的有哪一些多边形或线段. 解法: 想法不难,直接暴力将所有的图形处理成线段,然后暴力枚举,相交就加入其vector就行了.主要是代码有点麻烦,一步一 ...
- poj3449 Geometric Shapes【计算几何】
含[判断线段相交].[判断两点在线段两侧].[判断三点共线].[判断点在线段上]模板 Geometric Shapes Time Limit: 2000MS Memory Limit: 655 ...
- POJ 3449 Geometric Shapes (求正方形的另外两点)
Geometric Shapes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 1470 Accepted: 622 D ...
- POJ 3449 Geometric Shapes
判断两个多边形是否相交,只需判断边是否有相交. 编码量有点大,不过思路挺简单的. #include<cstdio> #include<cstring> #include< ...
- POJ1584 判断多边形是否为凸多边形,并判断点到直线的距离
求点到直线的距离: double dis(point p1,point p2){ if(fabs(p1.x-p2.x)<exp)//相等的 { return fabs(p2.x-pe ...
- 计算几何--判断两条线段相交--poj 2653
Pick-up sticks Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 8862 Accepted: 3262 De ...
随机推荐
- 自动配置IE代理脚本
http://www.cnblogs.com/ttyp/archive/2005/11/18/279124.html
- dshow采集过程
捕捉静态图片常用的filter是Sample Graber filter,它的用法参考手册.然后将捕捉filter的静态PIN连接到Sample Grabber,再将Sample Grabber连接到 ...
- Django--views(视图层)
路径匹配后-----传给视图函数 一.视图函数 视图层,熟练掌握两个对象即可:请求对象(request)和响应对象(HttpResponse) 一个视图函数,简称视图,是一个简单的Python 函数, ...
- [原]vue - webapp 返回无效 解决方案
- javascript:FileReader对象(读取文件)
FileReader对象 1.检测浏览器对FileReader的支持 1 if(window.FileReader) { 2 var fr = new FileReader(); 3 // add y ...
- 多线程中的join总结笔记
join方法的原理 就是调用相应线程的wait方法进行等待操作的,假如线程1中调用了线程2的join方法,则相当于在线程1中调用了线程2的wait方法,当线程2执行完(或者到达等待时间),线程2会自动 ...
- creator.d.ts 的错误
//export class PhysicsCollider{ export class PhysicsCollider extends Collider{ ==================检查代 ...
- 18.3 redis 的安装
因为之前我们server不存东西 我们 发现 后打开的网页 是接手不到之前的变化,不能更新到最新的变化的. 我们需要做到server给client发最新的代码已达到同步 我们有三种做法同步到最新的代码 ...
- 在前台页面写java代码,导入java的包
- js 遍历行和列
]; //遍历列 ; i < table.rows[].cells.length; i++) { console.log(table.rows[].cells[i].innerText); ]. ...