POJ 1410 intersection (computational geometry)

Source: Internet
Author: User

Title: The topic is very simple, that is, a matrix is solid, give a line segment, ask if line and matrix intersect
Problem-solving ideas: The use of line segments and segments are crossed, and then determine whether the line is inside the matrix, it should be noted that the coordinates of the matrix he gives is obviously not the upper left and right coordinates, it is necessary to judge the bottom left and right point of the coordinates.

#include <map> #include <set> #include <list> #include <cmath> #include <queue> #include <stack> #include <vector> #include <cstdio> #include <string> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm>using namespace std;const int inf=0x3f3f3f3f; typedef long long ll; #define PrN printf ("\ n") #define SI (n) scanf ("%d",& (n)) #define SII (n,m) scanf ("%d%d",& (n), & amp; (M)) #define SIII (n,m,k) scanf ("%d%d%d",& (N),& (M),& (K)) #define CLE (A,val) memset (A, (Val), sizeof (a)) #define REP (i,b) for (int i=0;i< (b), i++) #define REP (i,a,b) for (int i= (a), i<= (b); i++) #define REREP (I,A,B) for (int i    = (a); i>= (b); i--) Const double EPS = 1e-8;//to determine the positive or negative of doubule type or 0int sgn (double x) {if (Fabs (x) < EPS) return 0;    if (x < 0) return-1; else return 1;}    Build point, and overloaded operator struct point{double x, y;    Point () {}, point (double _x,double _y) {x = _x;y = _y; }//Reload minus because P is used when two points are subtracted to form a vectorOint operator-(const point &b) const {return point (X-B.X,Y-B.Y);    }//This is a cross product operation, it is important not to say double operator ^ (const point &b) const {return x*b.y-y*b.x;    } Double operator * (const point &b) Const {return x*b.x + y*b.y;    }};struct line{Point S,e;    Line () {_s,point _e) {s = _s;e = _e; }};//Judgment Segment intersection bool Inter (line L1,line L2) {return//This is 2 rectangles intersect max (l1.s.x,l1.e.x) >= min (l2.s.x,l2.e.x) &  amp;& Max (l2.s.x,l2.e.x) >= min (l1.s.x,l1.e.x) && max (l1.s.y,l1.e.y) >= min (l2.s.y,l2.e.y) && Max (l2.s.y,l2.e.y) >= min (l1.s.y,l1.e.y) &&//This is the determination of the cross product of the different sgn (L2.S-L1.S) ^ (l1.e-l 1.S) *SGN ((l2.e-l1.s) ^ (l1.e-l1.s)) <= 0 && sgn ((l1.s-l2.s) ^ (L2.E-L2.S)) *SGN ((L1.E-L2.S) ^ (L2.E-L2.S)) &lt ; = 0;} The distance double dist (point A,point b) {return sqrt ((b-a) * (b-a));} int n; Line Line[4]; Line Endli;double A1,a2,a3,a4,xtop,ytop,xbott,yBott;int Main () {#ifndef Online_judge freopen ("C:\\users\\zmy\\desktop\\in.txt", "R", stdin);//Freopen ("c:\\users\\    Zmy\\desktop\\out.txt "," w ", stdout); #endif//Online_judge SI (n); Rep (t,n) {scanf ("%LF%LF%LF%LF%LF%LF%LF%LF",&a1,&a2,&a3,&a4,&xtop,&ytop,&xbott,&        Ybott);        Endli=line (Point (A1,A2), point (A3,A4));        Put 4 sides into line Line[0]=line (Point (Xtop,ytop), point (Xtop,ybott));        Line[1]=line (Point (Xtop,ytop), point (Xbott,ytop));        Line[2]=line (Point (Xtop,ybott), point (Xbott,ybott));        Line[3]=line (Point (Xbott,ytop), point (Xbott,ybott));        int fl=0;            Determine if two lines intersect rep (i,4) {if (Inter (Line[i],endli)) {fl=1;break; }}//Determine if within rectangle if (max (A1,A3) <max (xbott,xtop) &&min (A1,A3) >min (xbott,xtop) &&        Max (A2,A4) <max (Ytop,ybott) &&min (A2,A4) >min (Ytop,ybott)) {fl=1; } if (fl) puts ("T");    Else puts ("F"); } return 0;}

POJ 1410 intersection (computational geometry)

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.