Uva11343-isolated segments (two segments intersect)

Source: Internet
Author: User

Test instructions: give you some line segments to find the number of segments that don't intersect other segments

Formula: P1*p2= (X1*X2,Y1*Y2) (inner product), p1xp2= (x1*y2,x2*y1) (outer product)

Determine if q is on the line p1-p2 above, according to (P1-Q) x (p2-q) = "Q" is the linear p1-p2.

Use the inner product (p1-q) * (P2-Q) <0 to determine if q is between p1-p2.

Intersection of P1-P2,Q1-Q2:

(x, Y) =p1+ (P2-P1) * ((Q2-Q1) x (Q1-P1)/((Q2-Q1) x (P2-P1)));

Reasoning: The P1-P2 line is written in point P1+t (P2-P1), then point and Q1-q2 line, and finally calculate the above.

Finally, verify that the intersection is not on two segments.

This problem is mainly precision, look at the absolute value of <1000, in fact, when the calculation will appear more than int, so need to use a long long, if it is used double, pay attention to precision, because all are integers, EPS should be set a little bigger, Too small will be due to double precision and WA. Double Judge A==0, use ABS (a) <eps.







#include <iostream> #include <cstdio> #include <cmath> #include <vector> #include <list>#    include<cstring>using namespace std; #define EPS 1e-3#define N 1005int ans[n];struct point{Double A, B;    Point () {}-point (double a,double b): A (a), B (b) {}-operator + (point P) {return-point (P.A+A,P.B+B);    } Point operator – (point P) {return point (A-P.A,B-P.B);    } Point operator * (double p) {return point (a*p,b*p);    } double dot (point P)//inner product {return (P.A*A+P.B*B);    } double det (point P)//outer product {return (A*P.B-B*P.A);    }};struct seg{int index; Point P1,p2;}; Point p1,p2;vector<seg>v;//Determines if q is p1-p2 on the line segment bool On_str (p1,point p2,point q) {return (ABS (P1-Q). Det (p2-q)) & Lt eps&& (p1-q). dot (p2-q) <eps);} Ask for a two-line intersection point intersection (Point P1,point p2,point q1,point Q2) {return p1+ (P2-P1) * ((Q2-Q1). Det (Q1-P1)/(Q2-Q1). Det ( P2-P1));} void solve (int a) {for (int i=0;i<v.siZe (); i++) {point q1=v[i].p1;         Point Q2=v[i].p2; if (ABS (P1-P2) det (q1-q2)) <eps)//Determine if the outer product is 0 {if (on_str (p1,p2,q1) | | On_str (P1,P2,Q2) | | On_str (Q1,Q2,P1) | |                  On_str (Q1,Q2,P2))//Determine if there is a coincidence {ans[a]=1;              Ans[v[i].index]=1;             }} else {point r=intersection (P1,P2,Q1,Q2);                 if (On_str (p1,p2,r) &&on_str (q1,q2,r)) {ans[a]=1;             Ans[v[i].index]=1;    }}}}int Main () {int t;    scanf ("%d", &t);            while (t--) {int n;            V.clear ();            memset (ans,0,sizeof (ans));        scanf ("%d", &n);                    for (int i=1;i<=n;i++) {scanf ("%lf%lf%lf%lf", &p1.a,&p1.b,&p2.a,&p2.b);                Solve (i);        V.push_back (SEG{I,P1,P2});        } int t=0;      for (int i=1;i<=n;i++) {if (ans[i]==0)          t++;    } printf ("%d\n", t); } return 0;}


Uva11343-isolated segments (two segments intersect)

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.