Poj 3304 segments (calculation of geometric judgment of the intersection of a straight line and a line segment)

Source: Internet
Author: User
Segments
Time limit:1000 ms   Memory limit:65536 K
Total submissions:7833   Accepted:2363
Description

GivenNSegments in the two dimen1_space, write a program, which determines if there exists a line such that after projecting these segments on it, all projected segments have at least one point in common.

Input

Input begins with a numberTShowing the number of test cases and then,TTest Cases Follow. Each test case begins with a line containing a positive integerN≤0. 100 showing the number of segments. After that,NLines containing
Four Real NumbersX1Y1X2Y2 follow, in which (X1,Y1) and (X2,Y2) are the coordinates of
Two endpoints for one of the segments.

Output

For each test case, your program must output "Yes! ", If a line with desired property exists and must output" No! "Otherwise. You must assume that two floating point numbersAAndBAre equal if |A-B| <10-8.

Sample Input

321.0 2.0 3.0 4.04.0 5.0 6.0 7.030.0 0.0 0.0 1.00.0 1.0 0.0 2.01.0 1.0 2.0 1.030.0 0.0 0.0 1.00.0 2.0 0.0 3.01.0 1.0 2.0 1.0

Sample output

Yes!Yes!No!

Source

Amirkabir University of Technology local contest 2006

I personally think this question is not simple. The situations and precision control that need to be considered also involve the conversion of a problem. I wa many times

The question is to give you a series of line segments and ask if you have a straight line so that the projection of all line segments on this line has a common point.

This problem is converted to whether there is a straight line that makes each line segment have a point of intersection with this line.

Explanations:

Now suppose there is a straight line that has an intersection with all the line segments here, So now another line is perpendicular to this line.

All the line segments in the question are projected into this line, and all the projection areas must be at the intersection of the two straight lines, that is, the vertical

In this way, the problem is successfully converted.

The following are some notes:

The first is precision control, which is a common problem in computational ry.

The second is to determine whether the two points are equal after enumerating a straight line composed of any two points. If the two points are equal, the two points will be eliminated directly because the X multiplication of the line segment with a length of 0 must be 0, which leads to misleading results.

# Include <iostream> # include <stdio. h> # include <cmath> # include <string. h >#include <algorithm> using namespace STD; # define EPS 1e-8struct point {Double X; Double Y ;}; struct line {point A; point B ;}my_line [150]; int rec [150]; int N; double xmulit (point & A, point & B, point & C) // a B x a c {return (B. x-a.x) * (C. y-a.y)-(B. y-a.y) * (C. x-a.x);} bool sans SS (point & A, point & B, point & C, point & D) // whether the line AB and the line CD intersection {Double P = xmuli T (A, B, C), P1 = xmulit (a, B, d); If (FABS (P1) <= EPS | FABS (p) <= EPS) return true; If (p * P1 <= 1e-8) return true; return false;} bool is_equal (point & A, point & B) {return (FABS (. x-b.x) <= EPS) & (FABS (. y-b.y) <= EPS);} int main () {int T, I, J, K; bool flag; scanf ("% d", & T); flag = false; while (t --) {flag = false; memset (REC, 0, sizeof (REC); scanf ("% d", & N); for (I = 0; I <n; I ++) {scanf ("% lf", & my_line [I]. a. x, & my_line [I]. A. Y, & my_line [I]. b. x, & my_line [I]. b. y) ;}for (I = 0; I <n; I ++) {for (k = 0; k <n; k ++) // you do not need to judge here, because the line segment will not be two points close to equal {If (K! = I &&! Upload SS (my_line [I]. a, my_line [I]. b, my_line [K]. a, my_line [K]. b) break;} If (n = k) {I = 1000; j = 1000; flag = true; break;} For (j = I + 1; j <n; j ++) {If (! Is_equal (my_line [I]. A, my_line [J]. A) {for (k = 0; k <n; k ++) {If (! Upload SS (my_line [I]. a, my_line [J]. a, my_line [K]. a, my_line [K]. b) break;} If (n = k) {I = 1000; j = 1000; flag = true; break ;}} if (! Is_equal (my_line [I]. A, my_line [J]. B) {for (k = 0; k <n; k ++) {If (! Upload SS (my_line [I]. a, my_line [J]. b, my_line [K]. a, my_line [K]. b) break;} If (n = k) {I = 1000; j = 1000; flag = true; break;} If (! Is_equal (my_line [I]. B, my_line [J]. A) {for (k = 0; k <n; k ++) {If (! Upload SS (my_line [I]. b, my_line [J]. a, my_line [K]. a, my_line [K]. b) break;} If (n = k) {I = 1000; j = 1000; flag = true; break ;}} if (! Is_equal (my_line [I]. B, my_line [J]. B) {for (k = 0; k <n; k ++) {If (! Upload SS (my_line [I]. b, my_line [J]. b, my_line [K]. a, my_line [K]. b) break;} If (n = k) {I = 1000; j = 1000; flag = true; break ;}}} if (! Flag) printf ("No! \ N "); elseprintf (" Yes! \ N ") ;}return 0 ;}

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.