POJ3449 Geometric Shapes

來源:互聯網
上載者:User

 

Time Limit:
2000MS
Memory Limit:
65536K
Total Submissions:
708
Accepted:
291

Description

While
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 intersecting shapes. 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.

Input

Input
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 shape and 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 (“.”).

Output

For
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

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

Source

CTU Open 2007幾何體相交問題,輸入輸出比較麻煩,但題比較簡單#include<stdio.h><br />#include<string.h><br />#include<math.h><br />#include<stdlib.h><br />const double eps=1e-6;<br />const double pi=3.1415926;<br />struct Point<br />{<br /> double x,y;<br />};<br />struct Segment<br />{<br /> Point u,v;<br />};<br />struct Shape<br />{<br /> char name,inters[30];<br /> int num,interNum;<br /> Segment p[20];<br />}s[30];<br />int cmp(const void* a, const void* b)<br />{<br /> return (*(Shape*)a).name - (*(Shape*)b).name;<br />}<br />double min(double a, double b)<br />{<br /> return a<b?a:b;<br />}<br />double max(double a, double b)<br />{<br /> return a>b?a:b;<br />}<br />double dist(Point a, Point b)<br />{<br /> return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));<br />}<br />double getAngle(Point a, Point o)<br />{<br /> return atan2(a.y-o.y,a.x-o.x);<br />}<br />Point getMidPoint(Point a, Point b)<br />{<br /> Point c;<br /> c.x=(a.x+b.x)*0.5;<br /> c.y=(a.y+b.y)*0.5;<br /> return c;<br />}<br />void rotate(Point a, Point o, Point &b, double angle)<br />{<br /> double an=getAngle(a,o)+angle;<br /> double d=dist(a,o);<br /> b.x=o.x+d*cos(an);<br /> b.y=o.y+d*sin(an);<br />}<br />double cross(Point a, Point b, Point o)<br />{<br /> return (a.x-o.x)*(b.y-o.y)-(a.y-o.y)*(b.x-o.x);<br />}<br />double dblcmp(double r)<br />{<br /> if(fabs(r)<eps)<br /> {<br /> return 0;<br /> }<br /> return r>0?1:-1;<br />}<br />void copyPoint(Point &a, Point b)<br />{<br /> a.x=b.x, a.y=b.y;<br />}<br />void inputPoint(Point &p)<br />{<br /> scanf(" (%lf,%lf)",&p.x,&p.y);<br />}<br />void inputSquare(char name, int n)<br />{<br /> s[n].name=name;<br /> s[n].num=4;<br /> inputPoint(s[n].p[0].u);<br /> inputPoint(s[n].p[2].u);<br /> rotate(s[n].p[0].u, getMidPoint(s[n].p[0].u, s[n].p[2].u),s[n].p[1].u, pi*0.5);<br /> rotate(s[n].p[2].u, getMidPoint(s[n].p[0].u, s[n].p[2].u),s[n].p[3].u, pi*0.5);<br /> copyPoint(s[n].p[0].v, s[n].p[1].u);<br /> copyPoint(s[n].p[1].v, s[n].p[2].u);<br /> copyPoint(s[n].p[2].v, s[n].p[3].u);<br /> copyPoint(s[n].p[3].v, s[n].p[0].u);<br />}<br />void inputRectangle(char name, int n)<br />{<br /> s[n].name=name;<br /> s[n].num=4;<br /> inputPoint(s[n].p[0].u);<br /> inputPoint(s[n].p[1].u);<br /> inputPoint(s[n].p[2].u);<br /> rotate(s[n].p[1].u, getMidPoint(s[n].p[0].u, s[n].p[2].u),s[n].p[3].u, pi);<br /> copyPoint(s[n].p[0].v, s[n].p[1].u);<br /> copyPoint(s[n].p[1].v, s[n].p[2].u);<br /> copyPoint(s[n].p[2].v, s[n].p[3].u);<br /> copyPoint(s[n].p[3].v, s[n].p[0].u);<br />}<br />void inputLine(char name, int n)<br />{<br /> s[n].name=name;<br /> s[n].num=1;<br /> inputPoint(s[n].p[0].u);<br /> inputPoint(s[n].p[0].v);<br />}<br />void inputTriangle(char name, int n)<br />{<br /> s[n].name=name;<br /> s[n].num=3;<br /> inputPoint(s[n].p[0].u);<br /> inputPoint(s[n].p[1].u);<br /> inputPoint(s[n].p[2].u);<br /> copyPoint(s[n].p[0].v, s[n].p[1].u);<br /> copyPoint(s[n].p[1].v, s[n].p[2].u);<br /> copyPoint(s[n].p[2].v, s[n].p[0].u);<br />}<br />void inputPolygon(char name, int n)<br />{<br /> s[n].name=name;<br /> scanf("%d",&s[n].num);<br /> inputPoint(s[n].p[0].u);<br /> for(int i=1;i<s[n].num;i++)<br /> {<br /> inputPoint(s[n].p[i].u);<br /> copyPoint(s[n].p[i].v, s[n].p[i-1].u);<br /> }<br /> copyPoint(s[n].p[0].v,s[n].p[s[n].num-1].u);<br />}<br />bool quick(Point a, Point b, Point c, Point d)<br />{<br /> return max(a.x,b.x)>=min(c.x,d.x) and max(a.y,b.y)>=min(c.y,d.y) and max(c.x,d.x)>=min(a.x,b.x) and max(c.y,d.y)>=min(a.y,b.y);<br />}<br />bool interSegment(Point a, Point b, Point c, Point d)<br />{<br /> if(quick(a,b,c,d))<br /> {<br /> int d1=dblcmp(cross(a,c,d));<br /> int d2=dblcmp(cross(b,c,d));<br /> int d3=dblcmp(cross(c,a,b));<br /> int d4=dblcmp(cross(d,a,b));<br /> return d1*d2<=0 and d3*d4<=0;<br /> }<br />}<br />bool interShape(Shape &a, Shape &b)<br />{<br /> for(int i=0;i<a.num;i++)<br /> {<br /> for(int j=0;j<b.num;j++)<br /> {<br /> if(interSegment(a.p[i].u, a.p[i].v, b.p[j].u, b.p[j].v))<br /> {<br /> return true;<br /> }<br /> }<br /> }<br /> return false;<br />}<br />void checkInter(int n)<br />{<br /> for(int i=0;i<n;i++)<br /> {<br /> for(int j=i+1;j<n;j++)<br /> {<br /> if(interShape(s[i],s[j]))<br /> {<br /> s[i].inters[s[i].interNum++]=s[j].name;<br /> s[j].inters[s[j].interNum++]=s[i].name;<br /> }<br /> }<br /> }<br />}<br />int main()<br />{<br /> int n;<br /> char name[30],shape[30];<br /> while(true)<br /> {<br /> n=0;<br /> while(scanf("%s",name),*name!='-' and *name!='.')<br /> {<br /> scanf("%s",shape);<br /> switch(*shape)<br /> {<br /> case 's':<br /> inputSquare(*name,n);<br /> break;<br /> case 'r':<br /> inputRectangle(*name,n);<br /> break;<br /> case 'l':<br /> inputLine(*name,n);<br /> break;<br /> case 't':<br /> inputTriangle(*name,n);<br /> break;<br /> case 'p':<br /> inputPolygon(*name,n);<br /> break;<br /> }<br /> s[n++].interNum=0;<br /> }<br /> if(*name=='.') break;<br /> qsort(s,n,sizeof(*s),cmp);<br /> checkInter(n);<br /> for(int i=0;i<n;i++)<br /> {<br /> switch (s[i].interNum)<br /> {<br /> case 0:<br /> printf("%c has no intersections/n",s[i].name);<br /> break;<br /> case 1:<br /> printf("%c intersects with %c/n",s[i].name,s[i].inters[0]);<br /> break;<br /> case 2:<br /> printf("%c intersects with %c and %c/n",s[i].name,s[i].inters[0],s[i].inters[1]);<br /> break;<br /> default:<br /> printf("%c intersects with ",s[i].name);<br /> for(int j=0;j<s[i].interNum-1;j++)<br /> {<br /> printf("%c, ",s[i].inters[j]);<br /> }<br /> printf("and %c/n",s[i].inters[s[i].interNum-1]);<br /> }<br /> }<br /> printf("/n");<br /> }<br /> return 0;<br />}<br />

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.