题意不难理解,给出多个多边形,输出多边形间的相交情况(嵌套不算相交),思路也很容易想到。枚举每一个图形再枚举每一条边

恶心在输入输出,不过还好有sscanf(),不懂可以查看cplusplus网站

根据正方形对角的两顶点求另外两个顶点公式:

x2 = (x1+x3-y3+y1)/2; y2 = (x3-x1+y1+y3)/2;

x4= (x1+x3+y3-y1)/2; y4 = (-x3+x1+y1+y3)/2;

还有很多细节要处理

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
using namespace std; const double eps = 1e-8;
const double PI = acos(-1.0); int sgn(double x)
{
if(fabs(x) < eps) return 0;
return x < 0 ? -1:1;
} 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 p,q;
Line() {};
Line(Point _p,Point _q)
{
p = _p,q = _q;
}
}; //*判断线段相交
bool inter(Line l1,Line l2)
{
return
max(l1.p.x,l1.q.x) >= min(l2.p.x,l2.q.x) &&
max(l2.p.x,l2.q.x) >= min(l1.p.x,l1.q.x) &&
max(l1.p.y,l1.q.y) >= min(l2.p.y,l2.q.y) &&
max(l2.p.y,l2.q.y) >= min(l1.p.y,l1.q.y) &&
sgn((l2.p-l1.q)^(l1.p-l1.q))*sgn((l2.q-l1.q)^(l1.p-l1.q)) <= 0 &&
sgn((l1.p-l2.q)^(l2.p-l2.q))*sgn((l1.q-l2.q)^(l2.p-l2.q)) <= 0;
} const int maxn=30; struct Shape
{
char id;
int nump;
Point p[maxn];
} shp[maxn]; bool cmp(Shape a,Shape b)
{
return a.id<b.id;
} char str[20];
int num[maxn];
char inters[maxn][maxn];
map<string,int>mp; int main()
{
// freopen("in.txt","r",stdin);
mp["square"]=2;
mp["line"]=2;
mp["triangle"]=3;
mp["rectangle"]=3;
int cnt=0;
while(scanf("%s",str))
{
if(strcmp(str,".")==0) break;
if(strcmp(str,"-")!=0)
{
shp[cnt].id=str[0];
char str_sh[20];
scanf("%s",str_sh);
int n;
if(mp.count(str_sh)) n=mp[str_sh];
else scanf("%d",&n);
shp[cnt].nump=0;
for(int i=0; i<n; i++)
{
scanf("%s",str);
int x,y;
sscanf(str,"%*c%d%*c%d",&x,&y);
shp[cnt].p[shp[cnt].nump++]=Point(x,y);
}
if(strcmp(str_sh,"rectangle")==0)
{
Point p1=shp[cnt].p[0];
Point p2=shp[cnt].p[1];
Point p3=shp[cnt].p[2];
shp[cnt].p[shp[cnt].nump++]=Point(p1.x+p3.x-p2.x,p1.y+p3.y-p2.y);
}
if(strcmp(str_sh,"square")==0)
{
Point p1=shp[cnt].p[0];
Point p3=shp[cnt].p[1];
shp[cnt].p[shp[cnt].nump++]=Point((p1.x+p3.x-p3.y+p1.y)/2,(p3.x-p1.x+p1.y+p3.y)/2);
shp[cnt].p[shp[cnt].nump++]=Point((p1.x+p3.x+p3.y-p1.y)/2,(-p3.x+p1.x+p1.y+p3.y)/2);
swap(shp[cnt].p[1],shp[cnt].p[2]);
}
cnt++;
continue;
}
sort(shp,shp+cnt,cmp);
memset(num,0,sizeof(num));
for(int i=0; i<cnt; i++)
{
for(int j=i+1; j<cnt; j++)
{
bool flag=false;
int num1=shp[i].nump;
int num2=shp[j].nump;
for(int k1=0; k1<num1 && !flag; k1++)
{
for(int k2=0; k2<num2 && !flag; k2++)
{
if(inter(Line(shp[i].p[k1],shp[i].p[(k1+1)%num1]),Line(shp[j].p[k2],shp[j].p[(k2+1)%num2])))
{
inters[i][num[i]++]=shp[j].id;
inters[j][num[j]++]=shp[i].id;
flag=true;
}
}
}
}
}
for(int i=0; i<cnt; i++)
{
printf("%c ",shp[i].id);
if(num[i]==0)
printf("has no intersections\n");
else if(num[i]==1)
printf("intersects with %c\n",inters[i][0]);
else if(num[i]==2)
printf("intersects with %c and %c\n",inters[i][0],inters[i][1]);
else
{
printf("intersects with ");
for(int j=0;j<num[i]-1;j++)
printf("%c, ",inters[i][j]);
printf("and %c\n",inters[i][num[i]-1]);
}
}
cnt=0;
puts("");
}
return 0;
}

POJ 3449 Geometric Shapes 判断多边形相交的更多相关文章

  1. POJ 3449 Geometric Shapes(判断几个不同图形的相交,线段相交判断)

    Geometric Shapes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 1243   Accepted: 524 D ...

  2. TZOJ 2560 Geometric Shapes(判断多边形是否相交)

    描述 While creating a customer logo, ACM uses graphical utilities to draw a picture that can later be ...

  3. 简单几何(线段相交)+模拟 POJ 3449 Geometric Shapes

    题目传送门 题意:给了若干个图形,问每个图形与哪些图形相交 分析:题目说白了就是处理出每个图形的线段,然后判断是否相交.但是读入输出巨恶心,就是个模拟题加上线段相交的判断,我第一次WA不知道输出要按字 ...

  4. Geometric Shapes (poj3449多边形相交)

    题意:给你一些多边形的点,判断每个多边形和那些多边形相交,编号按照字典序输出 思路:枚举每个多边形的每条边看是否相交,这里的相交是包括端点的,关键是给你正方形不相邻两个点求另外两个点怎么求,长方形给你 ...

  5. POJ 3449 Geometric Shapes (求正方形的另外两点)

    Geometric Shapes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 1470   Accepted: 622 D ...

  6. POJ 3449 Geometric Shapes --计算几何,线段相交

    题意: 给一些多边形或线段,输出与每一个多边形或线段的有哪一些多边形或线段. 解法: 想法不难,直接暴力将所有的图形处理成线段,然后暴力枚举,相交就加入其vector就行了.主要是代码有点麻烦,一步一 ...

  7. POJ 3449 Geometric Shapes

    判断两个多边形是否相交,只需判断边是否有相交. 编码量有点大,不过思路挺简单的. #include<cstdio> #include<cstring> #include< ...

  8. POJ 3805 Separate Points (判断凸包相交)

    题目链接:POJ 3805 Problem Description Numbers of black and white points are placed on a plane. Let's ima ...

  9. POJ 2653 Pick-up sticks (判断线段相交)

    Pick-up sticks Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 10330   Accepted: 3833 D ...

随机推荐

  1. 一文详解 Linux 系统常用监控工一文详解 Linux 系统常用监控工具(top,htop,iotop,iftop)具(top,htop,iotop,iftop)

    一文详解 Linux 系统常用监控工具(top,htop,iotop,iftop)     概 述 本文主要记录一下 Linux 系统上一些常用的系统监控工具,非常好用.正所谓磨刀不误砍柴工,花点时间 ...

  2. Linux_进程管理的基本概述

    一.进程的基本概述 1️⃣:进程是已启动的可执行程序的运行中实例 2️⃣:/proc目录下以数字为名的目录,每一个目录代表一个进程,保存着进程的属性信息 3️⃣:每一个进程的PID是唯一的,就算进程退 ...

  3. MySQL 查询操作

    目录 基本语法 查询常量 查看表达式 查询函数 查询指定字段 查询所有列 列别名 表别名 条件查询 条件查询运算符 逻辑查询运算符 排序与分页 排序查询(order by) 排序方式 limit 分组 ...

  4. linux进阶之SVN集中式版本控制系统篇

    代码上线--版本控制系统 SVN             集中式版本控制系统 git                 分布式版本控制系统 SVN是subversion的缩写,即版本控制系统,是一个开放 ...

  5. 9.random_os_sys_shutil_shelve_xml_hashlib

    此章未能精读,待回顾random模块import randomrandom.random() 随机生成一个0-1之间随机的浮点数random.randint(a,b) 随机生成一个a-b之间的整数 a ...

  6. 9.2-3 pstree & pgrep

    9.2 pstree:显示进程状态树     pstree命令以树形结构显示进程和进程之间的关系.     如果不指定进程的PID号,或者不指定用户名称,则会以init进程为根进程,显示系统的所有进程 ...

  7. STM8的AIR与STM32的Keil的指定地址存数据

    [经验分享]KE02在IAR和KEIL中以常量形式初始化EEPROM值一, 经验分享描述        写这篇经验分享的原因是,之前有一个客户,他希望在KE02的芯片中,不要出现使用EEPROM操作命 ...

  8. IT行业新闻事件

    台积电: http://www.eefocus.com/component/394512 新闻合集: https://mail.qq.com/cgi-bin/frame_html?sid=q3Mhqr ...

  9. Java核心技术卷阅读随笔--第2章【Java 程序设计环境】

    Java 程序设计环境 本章主要介绍如何安装 Java 开发工具包( JDK ) 以及如何编译和运行不同类型的程序: 控制台程序. 图形化应用程序以及 applet.运行 JDK 工具的方法是在终端窗 ...

  10. TensorFlow常用Python扩展包

    TensorFlow常用Python扩展包 TensorFlow 能够实现大部分神经网络的功能.但是,这还是不够的.对于预处理任务.序列化甚至绘图任务,还需要更多的 Python 包. 下面列出了一些 ...