Determine whether two line segments are Intersection

Source: Internet
Author: User

Template (hdu1086)


SPAN style = "FONT-SIZE: 18px"> include <iostream> using namespace std; struct point {double x, y ;}; struct segment {point begin, end ;}; double min (double x, double y) {return x <y? X: y;} double max (double x, double y) {return x> y? X: y;} bool onsegment (point pi, point pj, point pk) // determines whether the point pk is on the pi pj of the online segment {if (min (pi. x, pj. x) <= pk. x & pk. x <= max (pi. x, pj. x) {if (min (pi. y, pj. y) <= pk. y & pk. y <= max (pi. y, pj. y) {return true ;}} return false;} double direction (point pi, point pj, point pk) // calculate the cross product {return (pi. x-pk.x) * (pi. y-pj.y)-(pi. y-pk.y) * (pi. x-pj.x);} bool judge (point p1, point p2, point p3, point p4) // determine whether the p1p2 and p3p4 line segments intersect {double d1 = direction (p3, p4, p1); double d2 = direction (p3, p4, p2); double d3 = direction (p1, p2, p3); double d4 = direction (p1, p2, p4 ); if (d1 * d2 <0 & d3 * d4 <0) return true; if (d1 = 0 & onsegment (p3, p4, p1) return true; if (d2 = 0 & onsegment (p3, p4, p2) return true; if (d3 = 0 & onsegment (p1, p2, p3) return true; if (d4 = 0 & onsegment (p1, p2, p4) return true; return false;} int main () {int n, count; segment seg [101]; while (cin> n & n) {count = 0; for (int I = 1; I <= n; I ++) {cin> seg [I]. begin. x> seg [I]. begin. y> seg [I]. end. x> seg [I]. end. y ;}for (int I = 1; I <n; I ++) for (int j = I + 1; j <= n; j ++) {if (judge (seg [I]. begin, seg [I]. end, seg [j]. begin, seg [j]. end) {count ++ ;}}cout <count <endl ;}return 0 ;}</SPAN>

 

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.