POJ 3449 Geometric Shapes(判断几个不同图形的相交,线段相交判断)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 1243 | Accepted: 524 |
Description
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
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 (“.”).
Output
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.
Sample Input
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)
-
.
Sample Output
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
Source
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <math.h> using namespace std;
const double eps = 1e-;
int sgn(double x)
{
if(fabs(x) < eps)return ;
if(x < )return -;
else return ;
}
struct Point
{
double x,y;
Point(){}
Point(double _x,double _y)
{
x = _x;y = _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;
}
//点积
double operator *(const Point &b)const
{
return x*b.x + y*b.y;
}
};
struct Line
{
Point s,e;
Line(){}
Line(Point _s,Point _e)
{
s = _s;e = _e;
}
};
//*判断线段相交
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 n;//点数
Point p[];
}node[];
bool cmp(Node a,Node b)
{
return a.id < b.id;
}
char str[];
bool check(Node a,Node b)
{
for(int i = ;i < a.n;i++)
for(int j = ;j < b.n;j++)
if(inter(Line(a.p[i],a.p[(i+)%a.n]),Line(b.p[j],b.p[(j+)%b.n])))
return true;
return false;
}
bool ff[];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n;
while(scanf("%s",str) == )
{
if(str[] == '.')break;
node[].id = str[];
scanf("%s",str);
if(strcmp(str,"square")==)
{
node[].n = ;
scanf(" (%lf,%lf)",&node[].p[].x,&node[].p[].y);
//cout<<node[0].p[0].x<<" "<<node[0].p[0].y<<endl;
scanf(" (%lf,%lf)",&node[].p[].x,&node[].p[].y);
// cout<<node[0].p[2].x<<" "<<node[0].p[2].y<<endl;
node[].p[].x = ((node[].p[].x+node[].p[].x)+(node[].p[].y-node[].p[].y))/;
node[].p[].y = ((node[].p[].y+node[].p[].y)+(node[].p[].x-node[].p[].x))/;
node[].p[].x = ((node[].p[].x+node[].p[].x)-(node[].p[].y-node[].p[].y))/;
node[].p[].y = ((node[].p[].y+node[].p[].y)-(node[].p[].x-node[].p[].x))/;
}
else if(strcmp(str,"line")==)
{
node[].n = ;
scanf(" (%lf,%lf)",&node[].p[].x,&node[].p[].y);
scanf(" (%lf,%lf)",&node[].p[].x,&node[].p[].y);
}
else if(strcmp(str,"triangle")==)
{
node[].n = ;
scanf(" (%lf,%lf)",&node[].p[].x,&node[].p[].y);
scanf(" (%lf,%lf)",&node[].p[].x,&node[].p[].y);
scanf(" (%lf,%lf)",&node[].p[].x,&node[].p[].y);
}
else if(strcmp(str,"rectangle")==)
{
node[].n = ;
scanf(" (%lf,%lf)",&node[].p[].x,&node[].p[].y);
scanf(" (%lf,%lf)",&node[].p[].x,&node[].p[].y);
scanf(" (%lf,%lf)",&node[].p[].x,&node[].p[].y);
node[].p[].x = node[].p[].x + (node[].p[].x - node[].p[].x);
node[].p[].y = node[].p[].y + (node[].p[].y - node[].p[].y);
}
else if(strcmp(str,"polygon")==)
{
scanf("%d",&node[].n);
for(int i = ;i < node[].n;i++)
{
scanf(" (%lf,%lf)",&node[].p[i].x,&node[].p[i].y);
}
}
n = ;
while(scanf("%s",str)==)
{ //cout<<str<<endl;
if(str[] == '-')break;
node[n].id = str[];
scanf("%s",str);
if(strcmp(str,"square")==)
{
node[n].n = ;
scanf(" (%lf,%lf)",&node[n].p[].x,&node[n].p[].y);
scanf(" (%lf,%lf)",&node[n].p[].x,&node[n].p[].y);
node[n].p[].x = ((node[n].p[].x+node[n].p[].x)+(node[n].p[].y-node[n].p[].y))/;
node[n].p[].y = ((node[n].p[].y+node[n].p[].y)+(node[n].p[].x-node[n].p[].x))/;
node[n].p[].x = ((node[n].p[].x+node[n].p[].x)-(node[n].p[].y-node[n].p[].y))/;
node[n].p[].y = ((node[n].p[].y+node[n].p[].y)-(node[n].p[].x-node[n].p[].x))/;
}
else if(strcmp(str,"line")==)
{
node[n].n = ;
scanf(" (%lf,%lf)",&node[n].p[].x,&node[n].p[].y);
scanf(" (%lf,%lf)",&node[n].p[].x,&node[n].p[].y);
}
else if(strcmp(str,"triangle")==)
{
node[n].n = ;
scanf(" (%lf,%lf)",&node[n].p[].x,&node[n].p[].y);
scanf(" (%lf,%lf)",&node[n].p[].x,&node[n].p[].y);
scanf(" (%lf,%lf)",&node[n].p[].x,&node[n].p[].y);
}
else if(strcmp(str,"rectangle")==)
{
node[n].n = ;
scanf(" (%lf,%lf)",&node[n].p[].x,&node[n].p[].y);
scanf(" (%lf,%lf)",&node[n].p[].x,&node[n].p[].y);
scanf(" (%lf,%lf)",&node[n].p[].x,&node[n].p[].y);
node[n].p[].x = node[n].p[].x + (node[n].p[].x - node[n].p[].x);
node[n].p[].y = node[n].p[].y + (node[n].p[].y - node[n].p[].y);
}
else if(strcmp(str,"polygon")==)
{
scanf("%d",&node[n].n);
for(int i = ;i < node[n].n;i++)
{
scanf(" (%lf,%lf)",&node[n].p[i].x,&node[n].p[i].y);
}
}
n++;
}
sort(node,node+n,cmp);
for(int i = ;i < n;i++)
{
printf("%c ",node[i].id);
memset(ff,false,sizeof(ff));
int cnt = ;
for(int j = ;j < n;j++)
if(i != j)
if(check(node[i],node[j]))
{
cnt++;
ff[j] = true;
}
if(cnt == )printf("has no intersections\n");
else if(cnt == )
{
printf("intersects with ");
for(int j = ; j < n;j++)
if(ff[j])
{
printf("%c\n",node[j].id);
break;
}
}
else if(cnt == )
{
printf("intersects with ");
for(int j = ; j < n;j++)
if(ff[j])
{
if(cnt==)printf("%c ",node[j].id);
if(cnt==)printf("and %c\n",node[j].id);
cnt--;
}
}
else
{
printf("intersects with ");
for(int j = ; j < n;j++)
if(ff[j])
{
if(cnt > )printf("%c, ",node[j].id);
if(cnt==)printf("and %c\n",node[j].id);
cnt--;
}
}
} printf("\n");
}
}
POJ 3449 Geometric Shapes(判断几个不同图形的相交,线段相交判断)的更多相关文章
- POJ 3449 Geometric Shapes 判断多边形相交
题意不难理解,给出多个多边形,输出多边形间的相交情况(嵌套不算相交),思路也很容易想到.枚举每一个图形再枚举每一条边 恶心在输入输出,不过还好有sscanf(),不懂可以查看cplusplus网站 根 ...
- POJ 3449 Geometric Shapes (求正方形的另外两点)
Geometric Shapes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 1470 Accepted: 622 D ...
- 简单几何(线段相交)+模拟 POJ 3449 Geometric Shapes
题目传送门 题意:给了若干个图形,问每个图形与哪些图形相交 分析:题目说白了就是处理出每个图形的线段,然后判断是否相交.但是读入输出巨恶心,就是个模拟题加上线段相交的判断,我第一次WA不知道输出要按字 ...
- POJ 3449 Geometric Shapes --计算几何,线段相交
题意: 给一些多边形或线段,输出与每一个多边形或线段的有哪一些多边形或线段. 解法: 想法不难,直接暴力将所有的图形处理成线段,然后暴力枚举,相交就加入其vector就行了.主要是代码有点麻烦,一步一 ...
- POJ 3449 Geometric Shapes
判断两个多边形是否相交,只需判断边是否有相交. 编码量有点大,不过思路挺简单的. #include<cstdio> #include<cstring> #include< ...
- 线段相交 poj 1066
// 线段相交 poj 1066 // 思路:直接枚举每个端点和终点连成线段,判断和剩下的线段相交个数 // #include <bits/stdc++.h> #include <i ...
- Geometric Shapes - POJ 3449(多边形相交)
题目大意:给一些几何图形的编号,求出来这些图形都和那些相交. 分析:输入的正方形对角线上的两个点,所以需要求出来另外两个点,公式是: x2:=(x1+x3+y3-y1)/2; y2:=(y1+y3 ...
- 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 ...
随机推荐
- LA 4794 Sharing Chocolate
大白书中的题感觉一般都比较难,能理解书上代码就已经很不错了 按照经验,一般数据较小的题目,都有可能是用状态压缩来解决的 题意:问一个面积为x×y的巧克力,能否切若干刀,将其切成n块面积为A1,A2,, ...
- LeetCode Count Primes 求素数个数(埃拉托色尼筛选法)
题意:给一个数n,返回小于n的素数个数. 思路:设数字 k =from 2 to sqrt(n),那么对于每个k,从k2开始,在[2,n)范围内只要是k的倍数的都删掉(也就是说[k,k2)是不用理的, ...
- aspose.Cells 导出Excel
aspose aspse.Cells可以操作Excel,且不依赖于系统环境. 使用模板,通过绑定输出数据源 这种适合于对格式没有特别要求的,直接绑定数据源即可.和数据绑定控件差不多. Workbook ...
- Thrift——初学
是什么? Thrift是一个跨语言的服务部署框架最初由Facebook于2007年开发,2008年进入Apache开源项目.Thrift通过一个中间语言(IDL, 接口定义语言)来定义RPC的接口和数 ...
- [Android] 关于系统工具栏和全屏沉浸模式
随着应用程序的一些深入设计,大家总想要更好的界面和体验,所以有些东西并不能只是知道方法就结束了,是得要去深入研究研究的.通过这个过程我觉得,从应用层面来讲,想实现一个功能很简单,但若想实现的好,就要去 ...
- 基于CentOS与VmwareStation10搭建Oracle11G RAC 64集群环境:2.搭建环境-2.6. 安装Oracle所依赖的必要包
2.6. 安装Oracle所依赖的必要包 2.6.1. 检查Oracle所依赖的必要rpm包 [root@localhost /]#rpm -q binutils compat-libstdc elf ...
- 编译及load mydqli.so文件
(1)cd /usr/local/php-5.2.17/ext/mysqli(2)输入/usr/local/php/bin/phpize 回车(3)./configure --prefix=/usr/ ...
- PV是什么意思
现在国内的站点都号称自己能统计pv,不过听我讲完pv到底是什么,估计所有的站长都要笑了. pv的意思,就是一个访问者在24小时(0点到24点)内到底看了你网站几个页面. 不过在国内,由 ...
- php.curl详解
目前为目最全的CURL中文说明了,学PHP的要好好掌握.有很多的参数.大部份都很有用.真正掌握了它和正则,一定就是个采集高手了. PHP中的CURL函数库(Client URL Library Fun ...
- A woman without arms
任吉美出生在中国烟台海阳一个极为普通的渔民家里.她先天残疾,没有胳膊和手. 小吉美注定要比别人生活得更艰难.她不能自己穿衣,不能自己端碗吃饭,也不能像兄弟姐妹们一样帮助妈妈干家务活,她觉得自己成了家里 ...