intersection--poj1410 (determining the relationship between a segment and a rectangle)

Source: Internet
Author: User

http://poj.org/problem?id=1410

The main topic: give you a line and a rectangle diagonal two points if the intersection of the output ' T ' Don't want to cross is ' F '

Attention:

1, given the rectangle may not be left upper and lower right so to judge first

2, the segment is in the inner output of the rectangle T

3, if the intersection is a vertex of a rectangle, it is disjoint.

Situation a little more just start thinking too little WA's I'm distressed

#include <stdio.h>#include<string.h>#include<stdlib.h>#include<ctype.h>#include<math.h>#include<queue>#include<algorithm>#include<iostream>using namespacestd;#defineN 20Const DoubleESP = 1e-8;#defineINF 0x3f3f3f3f#defineMemset (A, B) memset (A,b,sizeof (a))structpoint{Doublex, y; Point (Doublex=0,Doubley=0): X (x), Y (y) {} pointoperator- (ConstPoint &temp)Const    {        returnPoint (x-temp.x,y-temp.y); }    int operator* (ConstPoint &temp)Const    {        DoubleT= (X*TEMP.Y)-(y*temp.x); if(t>ESP)return 1; if(Fabs (T) <ESP)return 0; Else            return-1; }} p[n],j; Point Line (Point U1,point u2,point v1,point v2)///finding the intersection template{point ret=U1; DoubleT= ((u1.x-v1.x) * (V1.Y-V2.Y)-(U1.Y-V1.Y) * (v1.x-v2.x))/((u1.x-u2.x) * (V1.Y-V2.Y)-(U1.Y-U2.Y) * (v1.x-v2.x)); Ret.x+ = (u2.x-u1.x) *T; Ret.y+ = (U2.Y-U1.Y) *T; returnret;}intMain () {intN; scanf ("%d",&N);  while(n--) {j.x=j.y=INF; Memset (P,0); DoubleX1,x2,y1,y2; Doublea,b,c,d; scanf ("%lf%lf%lf%lf",&x1,&y1,&x2,&y2); scanf ("%lf%lf%lf%lf",&a,&b,&c,&d); if(a>c) Swap (A,C), swap (B,D); intu,d; U=Max (b,d); D=min (b,d); if(X1>=a && x1<=c && x2>=a && x2<=c && y1>=d && y1<=u && y 2>=d && y2<=u) {printf ("t\n"); Continue; }        ///determines whether a line segment intersects an edge of a rectanglep[1]=Point (X1,Y1); p[2]=Point (X2,y2); p[3]=Point (A, b); p[4]=Point (C,B); p[5]=Point (C,B); p[6]=Point (C,D); p[7]=Point (A, b); p[8]=Point (A,D); p[9]=Point (A,D); p[Ten]=Point (C,D); intK,kk; intflag=0;  for(intI=2; i<=5; i++) {k=abs ((p[1]-p[i*2-1]) * (p[i*2]-p[i*2-1]) + (p[2]-p[i*2-1]) * (p[i*2]-p[i*2-1])); KK=abs ((p[i*2-1]-p[1]) * (p[2]-p[1]) + (p[i*2]-p[1]) * (p[2]-p[1])); if(k!=2&& kk!=2)///if the intersection            {                if((p[1]-p[i*2-1]) * (p[i*2]-p[i*2-1])==0&& (p[2]-p[i*2-1]) * (p[i*2]-p[i*2-1])==0)///co-line                {                    if(i==2|| i==5)                    {                        if((p[1].x>a && p[1].X&LT;C) | | (p[2].x>a && p[2].X&LT;C) | | (p[1].x<=a && p[2].X&GT;=C) | | (p[2].x<=a && p[1].x>=c)) {flag=1;  Break; }                    }                    Else                    {                        if((p[1].y>d && p[1].y<u) | | (p[2].y>d && p[2].y<u) | | (p[1].y<=d && p[2].y>=u) | | (p[2].y<=d && p[1].y>=u)) {flag=1;  Break; } }} J=line (p[1],p[2],p[i*2],p[i*2-1]); if(i==2)                if(J.x>a && j.x<c && j.y==b)///if the intersection is between A and C (not including a C){flag=1;  Break; }                if(i==3)                {                    if(J.x==c && j.y>d && j.y<u) {flag=1;  Break; }                }                if(i==4)                {                    if(J.x==a && j.y>d && j.y<u) {flag=1;  Break; }                }                if(i==5)                {                    if(J.x>a && j.x<c && j.y==d) {flag=1;  Break; }                }            }        }        if(flag==0) printf ("f\n"); Elseprintf ("t\n"); }    return 0;}

intersection--poj1410 (determining the relationship between a segment and a rectangle)

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.