POJ 3449 /// 判断线段相交
题目大意:
给出多个多边形及其编号
按编号顺序输出每个多边形与其相交的其他多边形编号
注意一个两个多个的不同输出
将每个多边形处理成多条边 然后去判断与其他多边形的边是否相交
计算正方形另外两点的方法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 /// 判断线段相交的更多相关文章
- C - Segments POJ - 3304 (判断线段相交)
题目链接:https://vjudge.net/contest/276358#problem/C 题目大意:给你n条线段,问你是否存在一条线段使得所有的线段在这条直线的投影至少具有一个交点? 具体思路 ...
- POJ 2826 An Easy Problem? 判断线段相交
POJ 2826 An Easy Problem?! -- 思路来自kuangbin博客 下面三种情况比较特殊,特别是第三种 G++怎么交都是WA,同样的代码C++A了 #include <io ...
- 【POJ 2653】Pick-up sticks 判断线段相交
一定要注意位运算的优先级!!!我被这个卡了好久 判断线段相交模板题. 叉积,点积,规范相交,非规范相交的简单模板 用了“链表”优化之后还是$O(n^2)$的暴力,可是为什么能过$10^5$的数据? # ...
- POJ 2653 Pick-up sticks(判断线段相交)
Pick-up sticks Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 7699 Accepted: 2843 De ...
- POJ 1066--Treasure Hunt(判断线段相交)
Treasure Hunt Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7857 Accepted: 3247 Des ...
- 还记得高中的向量吗?leetcode 335. Self Crossing(判断线段相交)
传统解法 题目来自 leetcode 335. Self Crossing. 题意非常简单,有一个点,一开始位于 (0, 0) 位置,然后有规律地往上,左,下,右方向移动一定的距离,判断是否会相交(s ...
- 判断线段相交(hdu1558 Segment set 线段相交+并查集)
先说一下题目大意:给定一些线段,这些线段顺序编号,这时候如果两条线段相交,则把他们加入到一个集合中,问给定一个线段序号,求在此集合中有多少条线段. 这个题的难度在于怎么判断线段相交,判断玩相交之后就是 ...
- hdu 1086(判断线段相交)
传送门:You can Solve a Geometry Problem too 题意:给n条线段,判断相交的点数. 分析:判断线段相交模板题,快速排斥实验原理就是每条线段代表的向量和该线段的一个端点 ...
- POJ_1066_Treasure Hunt_判断线段相交
POJ_1066_Treasure Hunt_判断线段相交 Description Archeologists from the Antiquities and Curios Museum (ACM) ...
随机推荐
- JavaWeb学习篇之----Servlet
今天来继续学习JavaWeb的相关知识,之前都是都介绍一些基本知识,从今天开始我们来说一下如何在服务器编写程序,这里就需要来介绍一下Servlet的相关知识了.Servlet就是一个能够运行在服务器端 ...
- Android中的SrollView滚动详解
今天开发遇到一个需求就是ScrollView中嵌套一个ListView,同时需要实现滑动到底部自动加载更多,我们知道ListView滑动到底部简单实现onScrollListener()监听器即可,但 ...
- Android中使用占位符
Android中占位符的使用 有些朋友可能会动态的修改Android中strings.xml文件中的值,在这里给大家推荐一种简单的方法. strings.xml中节点是支持占位符的,如下所示: < ...
- NX二次开发-NXOpen读取工程图注释note1->GetText();
NX9+VS2012 #include <uf.h> #include <uf_drf.h> #include <NXOpen/Annotations_Note.hxx& ...
- flutter 显示HTML代码
需求是后台返回的是富文本,所以需要吧富文本转成flutter 能识别的内容 找了几个插件只有这个比较好用写下来 dependencies: flutter_html: ^0.9.8 安装 下剩下的就比 ...
- go modules学习
https://github.com/golang/go/wiki/Modules https://tonybai.com/2018/07/15/hello-go-module/ https://ww ...
- 使用Docker创建数据容器
使用Docker创建数据容器 翻译自: Data-only container madness 1.什么是数据容器? 数据容器就是本身只创建一个volume供其他容器共享,创建完后即退出,不执行任何任 ...
- Golang 交叉编译跨平台的可执行程序 (Mac、Linux、Windows )
起因: 在项目中,我们每一次提交都需要添加commit 信息,而且我们的commit 信息,比较繁琐.我决定用golang语言编写一个小工具. 我决定使用语言:golang,使用工具:gox包. go ...
- Thread状态
- 22-MySQL-Ubuntu-备份与恢复
案例: 将数据库jing_dong_1的数据库备份,然后恢复到数据库jing_dong_2; 步骤如下: 1.备份 注意: mysqldump 不是 mysql; 尖括号的方向; python.sql ...