题目大意:

给出多个多边形及其编号

按编号顺序输出每个多边形与其相交的其他多边形编号

注意一个两个多个的不同输出

将每个多边形处理成多条边 然后去判断与其他多边形的边是否相交

计算正方形另外两点的方法https://blog.csdn.net/qq_33328072/article/details/51655746

根据对角线

x0 + x2 =  x1 + x3

y0 + y2 =  y1 + y3

根据全等三角形

x1 - x3 = y2 - y1

y1 - y3 = x2 - x0

得到

x1 = (x0 + x2 + y2 - y0) / 2

x3 = (x0 + x2 + y0 - y2) / 2

y1 = (y0 + y2 + x0 - x2) / 2

y3 = (y0 + y2 - x0 + x2) / 2

而且注意这里算出了x1后 不能利用x1的结果来计算x3的结果

也就是不能 x3 = x0 + x2 - x1

原因(猜测)可能是x1计算时存在细微的误差

那么用有误差的x1再计算x3的话 误差就更大了 y1y3同理

#include <cstdio>
#include <string.h>
#include <cmath>
#include <algorithm>
#include <map>
#include <vector>
#include <string>
#include <iostream>
using namespace std; const double eps=1e-;
double add(double a,double b) {
if(abs(a+b)<eps*(abs(a)+abs(b))) return ;
return a+b;
}
struct P {
double x, y;
P(){};
P(double _x, double _y):x(_x),y(_y){};
P operator - (P p) {
return P(add(x,-p.x),add(y,-p.y)); }
P operator + (P p) {
return P(add(x,p.x),add(y,p.y)); }
P operator * (double d) {
return P(x*d,y*d); }
double dot (P p) {
return add(x*p.x,y*p.y); }
double det (P p) {
return add(x*p.y,-y*p.x); }
};
struct L {
P s,e;
L(){};
L(P _s,P _e):s(_s),e(_e){};
}; vector <L> vec[];
vector <int> ans[]; bool onSeg(P a,P b,P c) {
return (a-c).det(b-c)== && (a-c).dot(b-c)<=;
}
P ins(P a,P b,P c,P d) {
return a+(b-a)*((d-c).det(c-a)/(d-c).det(b-a));
}
bool insSS(int a,int b)
{
for(int i=;i<vec[a].size();i++) {
L s1=vec[a][i];
for(int j=;j<vec[b].size();j++) {
L s2=vec[b][j];
bool flag=;
if((s1.s-s1.e).det(s2.s-s2.e)==) {
flag= onSeg(s1.s,s1.e,s2.s) || onSeg(s1.s,s1.e,s2.e)
|| onSeg(s2.s,s2.e,s1.s) || onSeg(s2.s,s2.e,s1.e);
}
else {
P t=ins(s1.s,s1.e,s2.s,s2.e); //printf("%.2f %.2f\n",t.x,t.y);
flag= onSeg(s1.s,s1.e,t) && onSeg(s2.s,s2.e,t);
}
if(flag) return ;
}
}
return ;
}
void solve()
{
for(int i=;i<;i++) {
if(vec[i].size()==) continue;
for(int j=i+;j<;j++) {
if(vec[j].size()==) continue;
if(insSS(i,j)) {
ans[i].push_back(j);
ans[j].push_back(i);
}
}
} for(int i=;i<;i++) {
if(vec[i].size()==) continue;
if(ans[i].size()==) {
printf("%c has no intersections\n",i+'A');
continue;
}
printf("%c intersects with %c",i+'A',ans[i][]+'A');
int n=ans[i].size();
if(n==) {
printf(" and %c",ans[i][]+'A');
}
else {
for(int j=;j<n;j++) {
if(j+==n) printf(", and %c",ans[i][j]+'A');
else printf(", %c",ans[i][j]+'A');
}
} printf("\n");
} printf("\n");
}
void init()
{
for(int i=;i<;i++)
vec[i].clear(), ans[i].clear();
} int main()
{
string id,ty;
while(cin>>id) {
if(id[]=='-') {
solve(); init(); continue;
}
if(id[]=='.') break;
cin>>ty;
if(ty=="square") {
P p0,p1,p2,p3;
scanf(" (%lf,%lf) (%lf,%lf)",&p0.x,&p0.y,&p2.x,&p2.y);
p3.x=(p0.x+p2.x-p2.y+p0.y)/2.0; p1.x=(p0.x+p2.x+p2.y-p0.y)/2.0;
p3.y=(p0.y+p2.y-p0.x+p2.x)/2.0; p1.y=(p0.y+p2.y+p0.x-p2.x)/2.0;
vec[id[]-'A'].push_back(L(p0,p1));
vec[id[]-'A'].push_back(L(p2,p1));
vec[id[]-'A'].push_back(L(p2,p3));
vec[id[]-'A'].push_back(L(p0,p3));
}
else if(ty=="rectangle") {
P p0,p1,p2,p3;
scanf(" (%lf,%lf) (%lf,%lf) (%lf,%lf)",&p0.x,&p0.y,&p1.x,&p1.y,&p2.x,&p2.y);
p3.x=p0.x-p1.x+p2.x; p3.y=p0.y+p2.y-p1.y;
vec[id[]-'A'].push_back(L(p0,p1));
vec[id[]-'A'].push_back(L(p2,p1));
vec[id[]-'A'].push_back(L(p2,p3));
vec[id[]-'A'].push_back(L(p0,p3));
}
else if(ty=="line") {
P s,e; scanf(" (%lf,%lf) (%lf,%lf)",&s.x,&s.y,&e.x,&e.y);
vec[id[]-'A'].push_back(L(s,e));
}
else if(ty=="triangle") {
P p0,p1,p2;
scanf(" (%lf,%lf) (%lf,%lf) (%lf,%lf)",&p0.x,&p0.y,&p1.x,&p1.y,&p2.x,&p2.y);
vec[id[]-'A'].push_back(L(p0,p1));
vec[id[]-'A'].push_back(L(p2,p1));
vec[id[]-'A'].push_back(L(p2,p0));
}
else if(ty=="polygon") {
int n; scanf("%d",&n);
P a,b; scanf(" (%lf,%lf)",&a.x,&a.y);
P c=a;
for(int i=;i<n;i++) {
b=c;
scanf(" (%lf,%lf)",&c.x,&c.y);
vec[id[]-'A'].push_back(L(b,c));
}
vec[id[]-'A'].push_back(L(c,a));
}
} return ;
}

POJ 3449 /// 判断线段相交的更多相关文章

  1. C - Segments POJ - 3304 (判断线段相交)

    题目链接:https://vjudge.net/contest/276358#problem/C 题目大意:给你n条线段,问你是否存在一条线段使得所有的线段在这条直线的投影至少具有一个交点? 具体思路 ...

  2. POJ 2826 An Easy Problem? 判断线段相交

    POJ 2826 An Easy Problem?! -- 思路来自kuangbin博客 下面三种情况比较特殊,特别是第三种 G++怎么交都是WA,同样的代码C++A了 #include <io ...

  3. 【POJ 2653】Pick-up sticks 判断线段相交

    一定要注意位运算的优先级!!!我被这个卡了好久 判断线段相交模板题. 叉积,点积,规范相交,非规范相交的简单模板 用了“链表”优化之后还是$O(n^2)$的暴力,可是为什么能过$10^5$的数据? # ...

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

    Pick-up sticks Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 7699   Accepted: 2843 De ...

  5. POJ 1066--Treasure Hunt(判断线段相交)

    Treasure Hunt Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7857   Accepted: 3247 Des ...

  6. 还记得高中的向量吗?leetcode 335. Self Crossing(判断线段相交)

    传统解法 题目来自 leetcode 335. Self Crossing. 题意非常简单,有一个点,一开始位于 (0, 0) 位置,然后有规律地往上,左,下,右方向移动一定的距离,判断是否会相交(s ...

  7. 判断线段相交(hdu1558 Segment set 线段相交+并查集)

    先说一下题目大意:给定一些线段,这些线段顺序编号,这时候如果两条线段相交,则把他们加入到一个集合中,问给定一个线段序号,求在此集合中有多少条线段. 这个题的难度在于怎么判断线段相交,判断玩相交之后就是 ...

  8. hdu 1086(判断线段相交)

    传送门:You can Solve a Geometry Problem too 题意:给n条线段,判断相交的点数. 分析:判断线段相交模板题,快速排斥实验原理就是每条线段代表的向量和该线段的一个端点 ...

  9. POJ_1066_Treasure Hunt_判断线段相交

    POJ_1066_Treasure Hunt_判断线段相交 Description Archeologists from the Antiquities and Curios Museum (ACM) ...

随机推荐

  1. 配置类一@Configuration

    import org.springframework.context.annotation.Configuration; @Configuration用于定义配置类,可替换xml配置文件,被注解的类内 ...

  2. 字符串dp——牛客多校第五场G

    比赛的时候脑瘫了没想出来..打多校以来最自闭的一场 显然从s中选择大于m个数组成的数必然比t大,所以只要dp求出从s中选择m个数大于t的方案数 官方题解是反着往前推,想了下反着推的确简单,因为高位的数 ...

  3. 好用的日期控件jeDate

    最近做公司后台系统关于仓库的一些东西,需要根据时间范围来导出一些数据,我们使用的后台框架是基于bs的,bs也有时间控件:bootstrap-datepicker是只能选择日期的, daterangep ...

  4. BZOJ 4817: [Sdoi2017]树点涂色(lct+线段树)

    传送门 解题思路 跟重组病毒这道题很像.只是有了一个询问\(2\)的操作,然后询问\(2\)的答案其实就是\(val[x]+val[y]-2*val[lca(x,y)]+1\)(画图理解).剩下的操作 ...

  5. NOIp2018集训test-10-6/test-10-7 (联考五day1/day2)

    昨天考完月考,明天初赛,dcoi2017级今天终于开始停课准备noip了,大概没有比本弱校停课更晚的学校了吧.本来就够菜了,怕是要凉透哦. DAY1 T1石头剪刀布 据说爆搜随便做,但是我觉得我的O( ...

  6. NX二次开发-UFUN获取显示在NX交互界面的对象UF_OBJ_is_displayable

    NX9+VS2012 #include <uf.h> #include <uf_disp.h> #include <uf_obj.h> #include <u ...

  7. NX二次开发-算法篇-判断找到两个数组里不相同的对象

    NX9+VS2012 #include <uf.h> #include <uf_curve.h> #include <uf_modl.h> #include < ...

  8. Android studio 安装apk时报错:INSTALL_FAILED_NO_MATCHING_ABIS: Failed to extract native libraries

    flutter项目 华为手机真机安装报错,解决 办法 app build.gradle android {...}内添加一下代码 splits { abi { enable true reset() ...

  9. PAT_A1020#Tree Traversals

    Source: PAT A1020 Tree Traversals (25 分) Description: Suppose that all the keys in a binary tree are ...

  10. Linux下rsync的安装及简单使用

    2018-09-25 15:39:04 一.RSYNC安装环境: centos6.5 iptables关闭和selinux为disabled 源码安装:到rsync官网下载rsync源码安装包,上传到 ...