Hdu1086 (intersection of line segments)

Source: Internet
Author: User

Question meaning:

Returns N line segments to determine the logarithm of the intersection of these N line segments.

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1086

Question Analysis:

This topic mainly writes a function to determine the intersection of line segments, and then judges each line segment. The time complexity is O (n * n ). For more information, see the code.


AC code:

/**
* Determine whether there is an intersection between the line AB and the CD:
* Both conditions are met: ('x' indicates the cross product)
* 1. Point C is located on both sides of AB. (vector (abxac) * (abxad) <= 0)
* 2. points A and B are on both sides of the CD, respectively. (vector (cdxca) * (cdxcb) <= 0)
* 3. vector (abxac) * (abxad) <0 indicates that the vector is on both sides of the straight line, and = 0 indicates that the vector is on the straight line.
*/
# Include <iostream>
# Include <cmath>
# Include <cstring>
# Include <cstdio>
# Include <algorithm>
Using namespace STD;
Typedef struct node {
Double X, Y;
} Node;
Node P [105], P1 [101];
Double Direction (node Pi, node PJ, node PK) {// calculate the cross Multiplication
Return (Pk. x-pi.x) * (PJ. y-pi.y)-(PJ. x-pi.x) * (Pk. y-pi.y );
}

Bool segments_x (node P1, node P2, node P3, node P4) {// determine whether the two line segments overlap
Double D1, D2, D3, D4;
D1 = direction (P3, P4, P1 );
D2 = direction (P3, P4, P2 );
D3 = direction (P1, P2, P3 );
D4 = direction (P1, P2, P4 );
If (D1 * D2 <= 0 & D3 * D4 <= 0) return true;
Return false;
}
Int main ()
{
Int N;
While (CIN> N & N ){
For (INT I = 1; I <= N; I ++ ){
Cin> P [I]. x> P [I]. Y> P1 [I]. x> P1 [I]. Y;
}
Int K = 0;
For (INT I = 1; I <= n-1; I ++ ){
For (Int J = I + 1; j <= N; j ++ ){
If (segments_x (P [I], P1 [I], p [J], P1 [J]) K ++;
}
}
Cout <k <Endl;
}
Return 0;
}


Hdu1086 (intersection of line segments)

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.