poj 3449 Geometric Shapes 计算几何
2016-08-02 21:08
288 查看
poj 3449 Geometric Shapes 计算几何Geometric Shapes
DescriptionWhile creating a customer logo, ACM uses graphical utilities to draw a picture that can later be cut into special fluorescent materials. To ensure proper processing, the shapes in the picture cannot intersect. However, some logos contain such intersectingshapes. It is necessary to detect them and decide how to change the picture.Given a set of geometric shapes, you are to determine all of their intersections. Only outlines are considered, if a shape is completely inside another one, it is not counted as an intersection.InputInput contains several pictures. Each picture describes at most 26 shapes, each specified on a separate line. The line begins with an uppercase letter that uniquely identifies the shape inside the corresponding picture. Then there is a kind of the shapeand two or more points, everything separated by at least one space. Possible shape kinds are:• square: Followed by two distinct points giving the opposite corners of the square.• rectangle: Three points are given, there will always be a right angle between the lines connecting the first point with the second and the second with the third.• line: Specifies a line segment, two distinct end points are given.• triangle: Three points are given, they are guaranteed not to be co-linear.• polygon: Followed by an integer number N (3 ≤ N ≤ 20) and N points specifying vertices of the polygon in either clockwise or anti-clockwise order. The polygon will never intersect itself and its sides will have non-zero length.All points are always given as two integer coordinates X and Y separated with a comma and enclosed in parentheses. You may assume that |X|, |Y | ≤ 10000.The picture description is terminated by a line containing a single dash (“-”). After the last picture, there is a line with one dot (“.”).OutputFor each picture, output one line for each of the shapes, sorted alphabetically by its identifier (X). The line must be one of the following:• “X has no intersections”, if X does not intersect with any other shapes.• “X intersects with A”, if X intersects with exactly 1 other shape.• “X intersects with A and B”, if X intersects with exactly 2 other shapes.• “X intersects with A, B, . . ., and Z”, if X intersects with more than 2 other shapes.Please note that there is an additional comma for more than two intersections. A, B, etc. are all intersecting shapes, sorted alphabetically.Print one empty line after each picture, including the last one.Sample Input
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 1704 | Accepted: 719 |
A square (1,2) (3,2) F line (1,3) (4,4) W triangle (3,5) (5,5) (4,3) X triangle (7,2) (7,4) (5,3) S polygon 6 (9,3) (10,3) (10,4) (8,4) (8,1) (10,2) B rectangle (3,3) (7,5) (8,3) - B square (1,1) (2,2) A square (3,3) (4,4) - .Sample Output
A has no intersections B intersects with S, W, and X F intersects with W S intersects with B W intersects with B and F X intersects with B A has no intersections B has no intersections
给你一些几何图形,求每个图形与其他图形的相交状况
可以把这些图形看成线段,再判断是否线段相交
其中矩形题目只给你3个顶点,我们用中点可以求出第四个点,正方形只给你对角线上的两点,我们可以①作图得到一个公式,或者②由一个顶点作正方形内切圆(内切圆的圆心和半径很好求)的两条切线即 过此点两条相邻边的中点,然后用中点等于两点坐标和的一半,求得另两点
第一种
#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<cctype>#include<algorithm>#include<map>#include<queue>#include<stack>#include<cstdlib>#include<vector>using namespace std;const double PI = 3.14159265;const double eps=0.0000001;struct Point{double x,y;Point (double a=0,double b=0):x(a),y(b) {}};struct Line_segment{Point s,e;Line_segment() {}Line_segment(Point a,Point b):s(a),e(b) {}};inline double Max(double a,double b){return a>b?a:b;}inline double Min(double a,double b){return a<b?a:b;}double multiply(Point sp,Point ep,Point op){return((sp.x-op.x)*(ep.y-op.y)-(ep.x-op.x)*(sp.y-op.y));}bool intersect(Line_segment u,Line_segment v){return((Max(u.s.x,u.e.x)>=Min(v.s.x,v.e.x))&&(Max(v.s.x,v.e.x)>=Min(u.s.x,u.e.x))&&(Max(u.s.y,u.e.y)>=Min(v.s.y,v.e.y))&&(Max(v.s.y,v.e.y)>=Min(u.s.y,u.e.y))&&(multiply(v.s,u.e,u.s)*multiply(u.e,v.e,u.s)>=0)&&(multiply(u.s,v.e,v.s)*multiply(v.e,u.e,v.s)>=0));}void DoneSq(Point a, Point c,Point& b,Point &d){double x,y,mx, my;mx = (a.x+c.x)/2.0, my = (a.y+c.y)/2.0;x = a.x - mx;y = a.y - my;b.x = -y + mx;b.y = x + my;x = c.x - mx;y = c.y - my;d.x = - y + mx;d.y = x + my;}struct node{char jug;Line_segment x[26];int num;char inter[26];int in;} a[26];bool cmp(const node a,const node b){return a.jug<b.jug;}Point o,tem[26],kk,xx,qq,ww,q1,q2;int main(){char T[3],s[22];double r;int t=0,mm,i,j,n,tempo,we;while(~scanf("%s",T)){if(T[0]=='.') break;if(T[0]=='-'){for(i=0; i<t; i++)a[i].in=0;sort(a,a+t,cmp);for(i=0; i<t; i++)//遍历每个图形{for(j=i+1; j<t; j++) //和后面图形比较{mm=0;for(tempo=0; tempo<a[i].num; tempo++) //枚举此图形每条线段{for(we=0; we<a[j].num; we++)if(intersect(a[i].x[tempo],a[j].x[we])==1){mm=1;break;}if(mm==1) break;}if(mm==1){a[i].inter[a[i].in]=a[j].jug;a[i].in++;a[j].inter[a[j].in]=a[i].jug;a[j].in++;}}}for(i=0; i<t; i++){if(a[i].in==0) printf("%c has no intersections\n",a[i].jug);else if(a[i].in==1) printf("%c intersects with %c\n",a[i].jug,a[i].inter[0]);else if(a[i].in==2){printf("%c intersects with %c and %c\n",a[i].jug,a[i].inter[0],a[i].inter[1]);}else{printf("%c intersects with",a[i].jug);for(j=0; j<a[i].in-1; j++){printf(" %c,",a[i].inter[j]);}printf(" and %c\n",a[i].inter[a[i].in-1]);}}t=0;printf("\n");continue;}scanf("%s",s);if(s[0]=='s'&&s[1]=='q'){scanf(" (%lf,%lf)",&q1.x,&q1.y);scanf(" (%lf,%lf)",&q2.x,&q2.y);DoneSq(q1,q2,kk,xx);a[t].x[0].s=q1;a[t].x[0].e=xx;a[t].x[1].s=xx;a[t].x[1].e=q2;a[t].x[2].s=q2;a[t].x[2].e=kk;a[t].x[3].s=kk;a[t].x[3].e=q1;a[t].jug=T[0];a[t].num=4;}else if(s[0]=='r'&&s[1]=='e'){for(i=0; i<3; i++)scanf(" (%lf,%lf)",&tem[i].x,&tem[i].y);o.x=(tem[0].x+tem[2].x)/2;o.y=(tem[0].y+tem[2].y)/2;tem[3].x=2*o.x-tem[1].x;tem[3].y=2*o.y-tem[1].y;for(i=0; i<3; i++){a[t].x[i].s=tem[i];a[t].x[i].e=tem[i+1];}a[t].x[3].s=tem[3];a[t].x[3].e=tem[0];a[t].jug=T[0];a[t].num=4;}else if(s[0]=='l'&&s[1]=='i'){scanf(" (%lf,%lf)",&a[t].x[0].s.x,&a[t].x[0].s.y);scanf(" (%lf,%lf)",&a[t].x[0].e.x,&a[t].x[0].e.y);a[t].jug=T[0];a[t].num=1;}else if(s[0]=='t'&&s[1]=='r'){for(i=0; i<3; i++)scanf(" (%lf,%lf)",&tem[i].x,&tem[i].y);for(i=0; i<2; i++){a[t].x[i].s=tem[i];a[t].x[i].e=tem[i+1];}a[t].x[2].s=tem[2];a[t].x[2].e=tem[0];a[t].jug=T[0];a[t].num=3;}else if(s[0]=='p'&&s[1]=='o'){scanf("%d",&n);for(i=0; i<n; i++)scanf(" (%lf,%lf)",&tem[i].x,&tem[i].y);for(i=0; i<n-1; i++){a[t].x[i].s=tem[i];a[t].x[i].e=tem[i+1];}a[t].x[n-1].s=tem[n-1];a[t].x[n-1].e=tem[0];a[t].num=n;a[t].jug=T[0];}t++;}return 0;}
第二种
#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<cctype>#include<algorithm>#include<map>#include<queue>#include<stack>#include<cstdlib>#include<vector>using namespace std;const double PI = 3.14159265;const double eps=0.0000000001;struct Point{double x,y;Point (double a=0,double b=0):x(a),y(b) {}};struct Line_segment{Point s,e;Line_segment() {}Line_segment(Point a,Point b):s(a),e(b) {}};inline double Max(double a,double b){return a>b?a:b;}inline double Min(double a,double b){return a<b?a:b;}double multiply(Point sp,Point ep,Point op){return((sp.x-op.x)*(ep.y-op.y)-(ep.x-op.x)*(sp.y-op.y));}bool intersect(Line_segment u,Line_segment v){return((Max(u.s.x,u.e.x)>=Min(v.s.x,v.e.x))&&(Max(v.s.x,v.e.x)>=Min(u.s.x,u.e.x))&&(Max(u.s.y,u.e.y)>=Min(v.s.y,v.e.y))&&(Max(v.s.y,v.e.y)>=Min(u.s.y,u.e.y))&&(multiply(v.s,u.e,u.s)*multiply(u.e,v.e,u.s)>=0)&&(multiply(u.s,v.e,v.s)*multiply(v.e,u.e,v.s)>=0));}void c2point(Point p1,double r1,Point p2,double r2,Point &rp1,Point &rp2){double a,b,r;a=p2.x-p1.x;b=p2.y-p1.y;r=(a*a+b*b+r1*r1-r2*r2)/2;if(a==0&&b!=0){rp1.y=rp2.y=r/b;rp1.x=sqrt(r1*r1-rp1.y*rp1.y);rp2.x=-rp1.x;}else if(a!=0&&b==0){rp1.x=rp2.x=r/a;rp1.y=sqrt(r1*r1-rp1.x*rp2.x);rp2.y=-rp1.y;}else if(a!=0&&b!=0){double delta;delta=b*b*r*r-(a*a+b*b)*(r*r-r1*r1*a*a);rp1.y=(b*r+sqrt(delta))/(a*a+b*b);rp2.y=(b*r-sqrt(delta))/(a*a+b*b);rp1.x=(r-b*rp1.y)/a;rp2.x=(r-b*rp2.y)/a;}rp1.x+=p1.x;rp1.y+=p1.y;rp2.x+=p1.x;rp2.y+=p1.y;}void cutpoint(Point p,double r,Point sp,Point &rp1,Point &rp2){Point p2;p2.x=(p.x+sp.x)/2;p2.y=(p.y+sp.y)/2;double dx2,dy2,r2;dx2=p2.x-p.x;dy2=p2.y-p.y;r2=sqrt(dx2*dx2+dy2*dy2);c2point(p,r,p2,r2,rp1,rp2);}double Dist(Point a,Point b){return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}struct node{char jug;Line_segment x[26];int num;char inter[26];int in;} a[26];bool cmp(const node a,const node b){return a.jug<b.jug;}Point o,tem[26],kk,xx,qq,ww,q1,q2;int main(){char T[3],s[101];double r;int t=0,mm,i,j,n,tempo,we;while(~scanf("%s",T)){if(T[0]=='.') break;if(T[0]=='-'){for(i=0; i<t; i++)a[i].in=0;sort(a,a+t,cmp);for(i=0; i<t; i++)//遍历每个图形{for(j=i+1; j<t; j++) //和后面图形比较{mm=0;for(tempo=0; tempo<a[i].num; tempo++) //枚举此图形每条线段{for(we=0; we<a[j].num; we++)if(intersect(a[i].x[tempo],a[j].x[we])==1){mm=1;break;}if(mm==1) break;}if(mm==1){a[i].inter[a[i].in]=a[j].jug;a[i].in++;a[j].inter[a[j].in]=a[i].jug;a[j].in++;}}}for(i=0; i<t; i++){if(a[i].in==0) printf("%c has no intersections\n",a[i].jug);else if(a[i].in==1) printf("%c intersects with %c\n",a[i].jug,a[i].inter[0]);else if(a[i].in==2){printf("%c intersects with %c and %c\n",a[i].jug,a[i].inter[0],a[i].inter[1]);}else{printf("%c intersects with",a[i].jug);for(j=0; j<a[i].in-1; j++){printf(" %c,",a[i].inter[j]);}printf(" and %c\n",a[i].inter[a[i].in-1]);}}t=0;printf("\n");continue;}scanf("%s",s);if(s[0]=='s'&&s[1]=='q'){scanf(" (%lf,%lf)",&q1.x,&q1.y);scanf(" (%lf,%lf)",&q2.x,&q2.y);o.x=(q1.x+q2.x)/2;o.y=(q1.y+q2.y)/2;r=Dist(o,q1);r=(r*sqrt(2.0))/2;cutpoint(o,r,q1,qq,ww);kk.x=2*qq.x-q1.x;kk.y=2*qq.y-q1.y;xx.x=2*ww.x-q1.x;xx.y=2*ww.y-q1.y;a[t].x[0].s=q1;a[t].x[0].e=xx;a[t].x[1].s=xx;a[t].x[1].e=q2;a[t].x[2].s=q2;a[t].x[2].e=kk;a[t].x[3].s=kk;a[t].x[3].e=q1;a[t].jug=T[0];a[t].num=4;}else if(s[0]=='r'&&s[1]=='e'){for(i=0; i<3; i++)scanf(" (%lf,%lf)",&tem[i].x,&tem[i].y);o.x=(tem[0].x+tem[2].x)/2;o.y=(tem[0].y+tem[2].y)/2;tem[3].x=2*o.x-tem[1].x;tem[3].y=2*o.y-tem[1].y;for(i=0; i<3; i++){a[t].x[i].s=tem[i];a[t].x[i].e=tem[i+1];}a[t].x[3].s=tem[3];a[t].x[3].e=tem[0];a[t].jug=T[0];a[t].num=4;}else if(s[0]=='l'&&s[1]=='i'){scanf(" (%lf,%lf)",&a[t].x[0].s.x,&a[t].x[0].s.y);scanf(" (%lf,%lf)",&a[t].x[0].e.x,&a[t].x[0].e.y);a[t].jug=T[0];a[t].num=1;}else if(s[0]=='t'&&s[1]=='r'){for(i=0; i<3; i++)scanf(" (%lf,%lf)",&tem[i].x,&tem[i].y);for(i=0; i<2; i++){a[t].x[i].s=tem[i];a[t].x[i].e=tem[i+1];}a[t].x[2].s=tem[2];a[t].x[2].e=tem[0];a[t].jug=T[0];a[t].num=3;}else if(s[0]=='p'&&s[1]=='o'){scanf("%d",&n);for(i=0; i<n; i++)scanf(" (%lf,%lf)",&tem[i].x,&tem[i].y);for(i=0; i<n-1; i++){a[t].x[i].s=tem[i];a[t].x[i].e=tem[i+1];}a[t].x[n-1].s=tem[n-1];a[t].x[n-1].e=tem[0];a[t].num=n;a[t].jug=T[0];}t++;}return 0;}
相关文章推荐
- poj 3449 Geometric Shapes(计算几何)
- POJ 3449 Geometric Shapes【计算几何+判线段相交】
- POJ 1269 Intersecting Lines(计算几何 直线交点 点在直线上 直线平行)
- poj 2398 Toy Storage (计算几何)
- POJ 2007 Scrambled Polygon(计算几何---凸包)
- JOJ 2109 && POJ 1981 Circle and Points 计算几何 单位圆覆盖问题
- POJ 2653 Pick-up sticks [线段相交]【计算几何】
- POJ 1584-A Round Peg in a Ground Hole(计算几何-凸包、点到线段距离)
- POJ 2318 计算几何 题解
- poj 2398 Toy Storage 计算几何+二分
- POJ 3565 Ants(计算几何,KM)
- poj1039--Pipe(计算几何)
- POJ 2405 Beavergnaw (计算几何-简单题)
- POJ 2187 Beauty Contest [旋转卡壳]【计算几何】
- poj 2007 Scrambled Polygon(计算几何)
- 平行x 轴的线段 是否 遮掩 计算几何的扩大数据运算的典型应用,扩大根号2倍之后就避免了小数。 poj 3347 Kadj Squares
- POJ 3347 (思维、计算几何)
- POJ 1106 Transmitters(计算几何 极角排序啊)
- poj 1696 (计算几何基础)
- POJ 3528(计算几何+三维凸包表面积)