HDU 1558:segment set, compute geometry + and search set

Source: Internet
Author: User

Topic Link:

http://acm.hdu.edu.cn/showproblem.php?pid=1558

Topic type: Compute the set, and check the collection

Topic:

A segment and all segments which are connected with it compose a segment set. The size of a segment set is the number of segments in it. The problem is to find the size of some segment set.

Sample Input

1 P 1.00 1.00 4.00 2.00 P 1.00-2.00 8.00 4.00 q 1 P 2.00 3.00 3.00 1.00 Q 1 Q 3 p 1.00 4.00 8.00 2.00 q 2 p 3.00 3.00 6.00-2.00 Q 5

Sample Output

1 2 2 2 5

The main effect of the topic:

Enter a few line segments, which consist of two points on the coordinates, each with a x,y, representing the values of the X and Y axes.

When the input is Q K, the output is directly or indirectly connected with the number of line segments of the K line.

Analysis and Summary:

This topic is a comparison of the basis of the application, but the key is to determine whether two lines intersect.

The way to break two segments: we determine whether two segments intersect in two steps:

1. Fast rejection test: Set the rectangle with the diagonal line p1p2 as R, set the rectangle with the diagonal line q1q2 as T, and if R and T do not intersect, it is obvious that the two segments do not intersect.

2. Cross-Test: If two segments intersect, then the two segments must cross each other. If the P1P2 cross the Q1Q2, then the vector (P1-Q1) and (P2-Q1) are located on either side of the vector (Q2-Q1), i.e. ((P1-Q1) x (Q2-Q1)) * ((P2-Q1) x (Q2-Q1)) < 0. (using the Vector Fork product) when (P1-Q1) x (q2-q1) = 0 o'clock, description (P1-Q1) and (Q2-Q1) collinear, but because the fast rejection test has been passed, P1 must be on the line q1q2; similarly, (Q2-Q1) X (P2-Q1) = 0 indicates that P2 must be on the segment q1q2.

Therefore, the basis for judging p1p2 q1q2 is: ((p1-q1) x (Q2-Q1)) * ((Q2-Q1) x (p2-q1)) >= 0. Similarly, the basis for judging q1q2 p1p2 is: ((Q1-P1) x (P2-P1)) * ((P2-P1) x (Q2-P1)) >= 0.

The details are as follows: (this uses the vector cross product to determine whether two vectors are on either side of the other) Note: Only meet the above two conditions, that is, to cross each other, two segments to 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.