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) ...
随机推荐
- Batch - 重定向符号Redirection >
总结 Don't use a piping operator, which is what ">" Redirection is. 不要使用管道运算符,即“>”. Di ...
- Servlet - HTTP协议相关
1. 术语 : 请求 : 客户端根据用户所给的地址信息将数据发送给服务器的过程 响应 : 服务器将请求的处理结果发送给浏览器的过程 2. HTTP协议 : 超文本传输协议 ( Hyper Text T ...
- redis集群报错:(error) MOVED 5798 127.0.0.1:7001
原因 这种情况一般是因为启动redis-cli时没有设置集群模式所导致. 解决方案 启动时使用-c参数来启动集群模式,命令如下: redis-cli -c -p 7000 测试 127.0.0.1:7 ...
- idea bug解决
1.编译时错误:软件包 javax.servlet.http 不存在import javax.servlet.http.HttpServletRequest 解决办法:把servlet-api.jar ...
- delphi xe10 获取屏幕截图
//截取屏幕图片 function MakeScaleScreenshot(Sender: TControl): TBitmap; function GetScreenScale: Single; v ...
- delphi 获取本机IP地址和MAC地址 (转)
unit NetFunc; interface uses SysUtils, Windows, dialogs, winsock, Classes, ComObj, WinInet, Variants ...
- delphi 判断两个时间差是否在一个指定范围内
WithinPastYears.WithinPastMonths.WithinPastWeeks.WithinPastDays ... 判断两个时间差是否在一个指定范围内DateUtils.Withi ...
- Photon Server与Unity3D客户端的交互
Photon Server与Unity3D的交互分为3篇博文实现 (1)Photon Server的服务器端配置 (2)Photon Server的Unity3D客户端配置 (3)Photon Ser ...
- 【开篇】基于C#+EmguCV的机器视觉平台开发
市面上关于通用的机器视觉平台已有不少,一些大的视觉产品.设备制造商都有开发自己的一套系统.其通用性也都有一些行业局限,难以囊括所有可能性,一些需要经过二次开发,这也是难以攻克的问题.本人水平有限,再加 ...
- Redhat 7.0 安装桌面环境
1.安装桌面环境组件 #yum groupinstall "Server with GUI" 2.切换到图形界面 #startx 3.设置启动模式为图形界面 #rm /etc/sy ...