POJ 3449 Geometric Shapes

Source: Internet
Author: User

To determine whether two polygons intersect, simply determine if the edges intersect.

The amount of coding is a bit large, but the idea is quite simple.

#include <cstdio>#include<cstring>#include<vector>#include<cmath>#include<string>#include<queue>#include<list>#include<algorithm>#include<iostream>using namespacestd;strings;structpoint{Doublex; Doubley; Point (DoubleADoubleb) {x=a;y=b;}};structsharp{stringname; Vector<point>v; Vector<string>ans;} sharp[ -];inttot;BOOLcmpConstSharp&a,Constsharp&b) {    returna.name<B.name;}voidWork () {inta,b,c,d,e,f; if(s=="Square") {scanf ("(%d,%d)",&a,&b); scanf ("(%d,%d)",&c,&d); Point P1 (1.0*a,1.0*b); Point P2 (1.0* (a+b+c-d)/2.0,1.0* (-A+B+C+D)/2.0); Point P3 (1.0*c,1.0*d); Point P4 (1.0* (A-B+C+D)/2.0,1.0* (A+B-C+D)/2.0);        Sharp[tot].v.push_back (p1);        Sharp[tot].v.push_back (p2);        Sharp[tot].v.push_back (p3);    Sharp[tot].v.push_back (p4); }    if(s=="Rectangle") {scanf ("(%d,%d)",&a,&b); scanf ("(%d,%d)",&c,&d); scanf ("(%d,%d)",&e,&f); Point P1 (1.0*a,1.0*b); Point P2 (1.0*c,1.0*d); Point P3 (1.0*e,1.0*f); Point P4 (p1.x+p3.x-p2.x,p1.y+p3.y-p2.y);        Sharp[tot].v.push_back (p1);        Sharp[tot].v.push_back (p2);        Sharp[tot].v.push_back (p3);    Sharp[tot].v.push_back (p4); }    if(s==" Line")    {         for(intI=1; i<=2; i++) {scanf ("(%d,%d)",&a,&b); Point P (1.0*a,1.0*b);        Sharp[tot].v.push_back (P); }    }    if(s=="Triangle")    {         for(intI=1; i<=3; i++) {scanf ("(%d,%d)",&a,&b); Point P (1.0*a,1.0*b);        Sharp[tot].v.push_back (P); }    }    if(s=="Polygon")    {        intx; scanf ("%d",&x);  for(intI=1; i<=x;i++) {scanf ("(%d,%d)",&a,&b); Point P (1.0*a,1.0*b);        Sharp[tot].v.push_back (P); }} tot++;}Const Doubleeps=1e-8;#defineZero (x) (((x) >0? ( X):(-X)) <eps)DoubleXmult (Point p1,point p2,point p0) {return(p1.x-p0.x) * (P2.Y-P0.Y)-(p2.x-p0.x) * (p1.y-p0.y);}intdots_inline (Point p1,point p2,point p3) {returnZero (Xmult (P1,P2,P3));}intsame_side (Point p1,point p2,point l1,point L2) {returnXmult (L1,P1,L2) *xmult (L1,P2,L2) >EPS;}intdot_online_in (Point p,point l1,point L2) {returnZero (Xmult (P,L1,L2)) && (l1.x-p.x) * (l2.x-p.x) <eps&& (L1.Y-P.Y) * (L2.Y-P.Y) <EPS;}intintersect_in (Point u1,point u2,point v1,point v2) {if(!dots_inline (U1,U2,V1) | |! Dots_inline (U1,U2,V2))return!same_side (U1,U2,V1,V2) &&!same_side (V1,V2,U1,U2); returnDot_online_in (u1,v1,v2) | | Dot_online_in (u2,v1,v2) | | Dot_online_in (V1,U1,U2) | |dot_online_in (V2,U1,U2);}BOOLJudgeintAintb) {     for(intI=0; I<sharp[a].v.size (); i++)    {         for(intj=0; J<sharp[b].v.size (); j + +) {point P1=Sharp[a].v[i]; Point P2=sharp[a].v[(i+1)%sharp[a].v.size ()]; Point P3=Sharp[b].v[j]; Point P4=sharp[b].v[(j+1)%sharp[b].v.size ()]; if(Intersect_in (P1,P2,P3,P4))return 1; }    }    return 0;}voidinit () {tot=0;  for(intI=0;i< -; i++) {sharp[i].ans.clear ();    Sharp[i].v.clear (); }}intMain () { while(1) {cin>>s; if(s==".") Break;        Init (); Sharp[tot].name=s; CIN>>s; work ();  while(1) {cin>>R; if(s=="-") Break; Sharp[tot].name=s; CIN>>s; work (); } sort (Sharp,sharp+tot,cmp);  for(intI=0; i<tot;i++)        {             for(intj=i+1; j<tot;j++)            {                if(judge (I,j)) {sharp[i].ans.push_back (sharp[j].name);                Sharp[j].ans.push_back (Sharp[i].name); }            }        }         for(intI=0; i<tot;i++) {cout<<sharp[i].name<<" "; if(sharp[i].ans.size () = =0) printf ("Has no intersections\n"); Else if(sharp[i].ans.size () = =1) cout<<"intersects with"<<sharp[i].ans[0]<<Endl; Else if(sharp[i].ans.size () = =2) cout<<"intersects with"<<sharp[i].ans[0]<<" and"<<sharp[i].ans[1]<<Endl; Else{printf ("intersects with");  for(intk=0; K<sharp[i].ans.size (); k++)                {                    if(K<sharp[i].ans.size ()-1) cout<<" "<<sharp[i].ans[k]<<","; Elsecout<<" and"<<sharp[i].ans[k]<<Endl; }}} printf ("\ n"); }    return 0;}

POJ 3449 Geometric Shapes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.