题意:给你一些多边形的点,判断每个多边形和那些多边形相交,编号按照字典序输出

思路:枚举每个多边形的每条边看是否相交,这里的相交是包括端点的,关键是给你正方形不相邻两个点求另外两个点怎么求,长方形给你3个点求第四个点怎么求?

因为对角线的交点为两条对角线的中点,所以

x0 + x2 =  x1 + x3

y0 + y2 =  y1 + y3

可以证明分割的这几个小三角形是全等的所以有

x1 - x3 = y2 - y1

y1 - y3 = x2 - x0

根据这几个式子可以推出 另外两个点的坐标

剩下的就是枚举每两个多边形的每条边是否相交

就是输入输出格式要细心点

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
struct Point
{
double x,y;
Point(double x = 0,double y = 0):x(x),y(y){}
};
typedef Point Vector;
Vector operator + (Vector a, Vector b) { return Vector(a.x+b.x,a.y+b.y) ;}
Vector operator - (Vector a, Vector b) { return Vector(a.x-b.x,a.y-b.y) ;}
Vector operator * (Vector a,double p) { return Vector(a.x*p,a.y*p) ;}
Vector operator / (Vector a,double p) { return Vector(a.x/p,a.y/p) ;}
double Dot(Vector a,Vector b) { return a.x*b.x + a.y*b.y ;}
double Length(Vector a) { return sqrt(Dot(a,a)) ;}
double Cross(Vector a, Vector b) { return a.x*b.y - a.y*b.x ;}
const double eps = 1e-8;
int dcmp(double x)
{
if(fabs(x) < eps) return 0;
else return x < 0 ? -1 : 1;
}
bool operator == (Point a,Point b)
{
return dcmp(a.x-b.x) == 0&& dcmp(a.y-b.y) == 0;
}
bool operator < (Point a,Point b)
{
return a.x < b.x || (a.x == b.x && a.y < b.y);
} bool Onsegment(Point p,Point a,Point b)
{
return dcmp(Cross(b-a,p-a)) == 0 && dcmp(Dot(b-p,a-p)) < 0 || (p == a) || (p == b);
} bool OnLine(Point p,Point a,Point b)
{
return fabs(Cross(p-a,a-b)) / Length(b-a);
} bool Segmentsection(Point a,Point b,Point c,Point d)
{
double d1 = Cross(b-a,c-a),d2 = Cross(b-a,d-a),d3 = Cross(d-c,a-c),d4 = Cross(d-c,b-c);
if(dcmp(d1)*dcmp(d2) < 0 && dcmp(d3)*dcmp(d4) < 0) return true;
else if(dcmp(d1) == 0 && Onsegment(c,a,b) ) return true;
else if(dcmp(d2) == 0 && Onsegment(d,a,b) ) return true;
else if(dcmp(d3) == 0 && Onsegment(a,c,d) ) return true;
else if(dcmp(d4) == 0 && Onsegment(b,c,d) ) return true;
else return false;
} Point Segment(Point p,Vector v,Point q,Vector w)
{
Vector u = p-q;
double t = Cross(w,u) / Cross(v,w);
return p + v*t;
} double Max(double a,double b)
{
return a > b ? a : b;
}
struct Line
{
Point s,e;
Line(Point s = 0,Point e = 0) :s(s),e(e){}
}; struct polygon
{
Point p[30];
int num;
}poly[50]; bool Ispoly(polygon a,polygon b)
{
if(a.num != 0 && b.num != 0)
{
for(int i = 0; i < a.num; i++)
{
for(int j = 0; j < b.num; j++)
{
if( Segmentsection(a.p[i],a.p[(i+1)%a.num],b.p[j],b.p[(j+1)%b.num]) )
return true;
}
}
}
return false;
}
int main()
{
char str[10],strr[20];
memset(poly,0,sizeof(poly));
while(scanf("%s",str) != EOF)
{
if(strcmp(str,".") == 0)
{
break;
}
if(strcmp(str,"-") == 0)
{
char c[30];
int k,j;
for(int i = 0; i < 26; i++)
{
k = 0;
for(j = 0; j < 26; j++)
{
if( i != j && Ispoly(poly[i],poly[j]))
{
c[k++] = j + 'A';
}
}
if(k == 0 && poly[i].num != 0)
{
printf("%c has no intersections\n",i+'A');
}
else if(poly[i].num != 0)
{
printf("%c intersects with %c",i+'A',c[0]);
if(k == 2)
{
printf(" and %c",c[1]);
}
else if(k > 2)
{
for(int m = 1; m < k-1; m++)
{
printf(", %c",c[m]);
}
printf(", and %c",c[k-1]);
}
printf("\n");
}
}
printf("\n");
memset(poly,0,sizeof(poly));
continue;
}
scanf("%s",strr);
int temp = str[0]-'A';
double x,y;
if(strcmp(strr,"square") == 0)
{
poly[temp].num = 4;
scanf(" (%lf,%lf)",&x,&y);
poly[temp].p[0].x = x, poly[temp].p[0].y = y;
scanf(" (%lf,%lf)",&x,&y);
poly[temp].p[2].x = x, poly[temp].p[2].y = y; poly[temp].p[1].x = (poly[temp].p[0].x+poly[temp].p[2].x +poly[temp].p[2].y-poly[temp].p[0].y)/2;
poly[temp].p[1].y = (poly[temp].p[0].y+poly[temp].p[2].y+poly[temp].p[0].x-poly[temp].p[2].x)/2;
poly[temp].p[3].x = (poly[temp].p[0].x+poly[temp].p[2].x +poly[temp].p[0].y-poly[temp].p[2].y)/2;
poly[temp].p[3].y = (poly[temp].p[0].y+poly[temp].p[2].y+poly[temp].p[2].x-poly[temp].p[0].x)/2; }
else if(strcmp(strr,"rectangle") == 0)
{
poly[temp].num = 4;
scanf(" (%lf,%lf)",&x,&y);
poly[temp].p[0].x = x, poly[temp].p[0].y = y;
scanf(" (%lf,%lf)",&x,&y);
poly[temp].p[1].x = x, poly[temp].p[1].y = y;
scanf(" (%lf,%lf)",&x,&y);
poly[temp].p[2].x = x, poly[temp].p[2].y = y;
poly[temp].p[3].x = (poly[temp].p[0].x + poly[temp].p[2].x - poly[temp].p[1].x);
poly[temp].p[3].y = ( poly[temp].p[2].y - poly[temp].p[1].y + poly[temp].p[0].y);
}
else if(strcmp(strr,"line") == 0)
{
poly[temp].num = 2;
scanf(" (%lf,%lf)",&x,&y);
poly[temp].p[0].x = x, poly[temp].p[0].y = y;
scanf(" (%lf,%lf)",&x,&y);
poly[temp].p[1].x = x, poly[temp].p[1].y = y;
}
else if(strcmp(strr,"polygon") == 0)
{
int n;
scanf("%d",&n);
poly[temp].num = n;
for(int i = 0; i < n; i++)
{
scanf(" (%lf,%lf)",&x,&y);
poly[temp].p[i].x = x, poly[temp].p[i].y = y;
}
}
else
{ poly[temp].num = 3;
scanf(" (%lf,%lf)",&x,&y);
poly[temp].p[0].x = x, poly[temp].p[0].y = y;
scanf(" (%lf,%lf)",&x,&y);
poly[temp].p[1].x = x, poly[temp].p[1].y = y;
scanf(" (%lf,%lf)",&x,&y);
poly[temp].p[2].x = x, poly[temp].p[2].y = y;
}
}
return 0;
}

Geometric Shapes (poj3449多边形相交)的更多相关文章

  1. POJ 3449 Geometric Shapes 判断多边形相交

    题意不难理解,给出多个多边形,输出多边形间的相交情况(嵌套不算相交),思路也很容易想到.枚举每一个图形再枚举每一条边 恶心在输入输出,不过还好有sscanf(),不懂可以查看cplusplus网站 根 ...

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

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

  3. poj3449 Geometric Shapes【计算几何】

    含[判断线段相交].[判断两点在线段两侧].[判断三点共线].[判断点在线段上]模板   Geometric Shapes Time Limit: 2000MS   Memory Limit: 655 ...

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

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

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

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

  6. Inheritance - SGU 129(线段与多边形相交的长度)

    题目大意:给一个凸多边形(点不是按顺序给的),然后计算给出的线段在这个凸多边形里面的长度,如果在边界不计算. 分析:WA2..WA3...WA4..WA11...WA的无话可说,总之细节一定考虑清楚, ...

  7. hdu 5130(2014广州 圆与多边形相交模板)

    题意:一个很多个点p构成的多边形,pb <= pa * k时p所占区域与多边形相交面积 设p(x,y),       (x - xb)^2+(y - yb)^2 / (x - xa)^2+(y ...

  8. dtIntersectSegmentPoly2D 2D上的线段与多边形相交计算 产生结果:是否相交,线段跨越的开始和结束百分比,相交的边

    dtIntersectSegmentPoly2D(startPos, endPos, verts, nv, tmin, tmax, segMin, segMax): http://geomalgori ...

  9. Geometric Shapes - POJ 3449(多边形相交)

    题目大意:给一些几何图形的编号,求出来这些图形都和那些相交.   分析:输入的正方形对角线上的两个点,所以需要求出来另外两个点,公式是: x2:=(x1+x3+y3-y1)/2; y2:=(y1+y3 ...

随机推荐

  1. oracle触发器调试

    1.如下图位置点击触发器,会出现调试窗口 2.执行编译并调试 3.点击小虫,将画红位置,加入会触发此触发器的语句.如果触发器执行成功,不会出现第4个图,不成功,会出现数据调试信息,具体报错位置会定位到 ...

  2. CoreMotion(加速计)

    加速计的作用 用于检测设备的运动(比如摇晃) 加速计的经典应用场景 摇一摇 计步器 ********************************** Core Motion获取数据的两种方式 pu ...

  3. php定时执行任务的几个方法

    PHP的实现决定了它没有Java和.Net这种AppServer的概念, 而http协议是一个无状态的协议, php只能被用户触发, 被调用, 调用后会自动退出内存, 没有常驻内存, 就没有办法准确的 ...

  4. Spring(一)简述(转载)

    原文出自:http://www.cnblogs.com/liunanjava/p/4396794.html 一.Spring简述 Spring是一个开源框架,Spring是于2003 年兴起的一个轻量 ...

  5. 利用WebRequest类上传文件

    说明:1.WebRequest类是一个抽象类,所以上传类实际使用的是其子类 2.打开Fiddler软件,监视正常网页的文件上传,可以看到http协议的请求和响应信息,简略说明 (第一行:请求说明 PO ...

  6. PHP图片操作

    <?php $filename="http://pic.nipic.com/2007-12-06/2007126102233577_2.jpg";//图片地址//获取图片信息 ...

  7. 已安装好的tengine编译添加未被安装的模块

    nginx -V 可以查看原来编译时都带了哪些参数 原来的参数: --prefix=/usr/local/tengine 需添加的参数: --with-http_stub_status_module ...

  8. cygwin编译ffmpeg移植到android平台问题集锦

    编译环境: windows xp Cygwin 1.1.3.1 NDK r9 1.提示各种command not found 比如 ./config.sh: line 6: $'--arch=arm\ ...

  9. 全国省市区Json文件 ,做省市区联动很轻松

    省份 [{"name":"安徽省", "code":"340000"},{"name":" ...

  10. BZOJ 2432 兔农

    Description 农夫栋栋近年收入不景气,正在他发愁如何能多赚点钱时,他听到隔壁的小朋友在讨论兔子繁殖的问题. 问题是这样的:第一个月初有一对刚出生的小兔子,经过两个月长大后,这对兔子从第三个月 ...