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 ...
随机推荐
- 解析swf文件头,获取flash的原始尺寸
要想解析swf文件头,首先要弄清楚的当然是swf文件格式规范.规范中对swf文件格式作了详细的说明.关于swf文件头,它是由以下几个部分组成:+-------+---+--------+------- ...
- Haskell语言学习笔记(86)字符串格式化与插值
String 的格式化 Text.Printf 这个模块用来处理字符串格式化. printf :: PrintfType r => String -> r printf 用于格式化字符串, ...
- Linux Install redis
1.将下载好的压缩包放到/usr/local目录下# tar xzf redis-3.0.2.tar.gz # cd redis-3.0.2 # make//--------------------- ...
- MVC之Model元数据
Contronoller激活之后,ASP.NET MVC会根据当前请求上下文得到目标Action的名称,然后解析出对应的方法并执行之. 在整个Action方法的执行过程中,Model元数据的解析是一个 ...
- sqlalchemy 学习--单表操作
以下所有代码片段都使用了统一的引用,该引用如下: from sqlalchemy import create_engine, ForeignKey from sqlalchemy.ext.declar ...
- 一个漂亮的 PlaceHolder
预览: 不知道为什么下面这个窗口中的 JavaScript 代码没有运行-_-||,想看实际效果就把下面的代码保存下来打开看吧. 代码: <!DOCTYPE HTML> <html ...
- TensorFlow saved_model 模块
最近在学tensorflow serving 模块,一直对接口不了解,后面看到这个文章就豁然开朗了, 主要的困难在于 tf.saved_model.builder.SavedModelBuilde ...
- appium+python自动化测试
appium+python自动化测试 标签(空格分隔): appium 获取APP的包名 1.aapt即Android Asset Packaging Tool,在SDK的build-tools目录下 ...
- Mysql 5.7 忘记root密码或重置密码的详细方法
在Centos中安装完MySQL数据库以后,不知道密码,这可怎么办,下面给大家说一下怎么重置密码 在Centos中安装完MySQL数据库以后,不知道密码,这可怎么办,下面给大家说一下怎么重置密码 1. ...
- cdh 安装系列1-- manager 6.01 安装
一.如果是远程centos安装,请参考我得博客,通过xmanager 链接centos桌面 二.Non-production Installation Ideal for trying Clouder ...