[Intersection of line segments] HDU 1086

Source: Internet
Author: User

Calculate the number of intersection times of a bunch of line segments, even if the intersection is overlapped.

For more information, see http://dev.gameres.com/program/abstract/geometry.htm?

 

Sample Input

2

0.00 0.00 1.00 1.00

0.00 1.00 1.00 0.00

3

0.00 0.00 1.00 1.00

0.00 1.00 1.00 0.000

0.00 0.00 1.00 0.00

0

 

Sample Output

1

3

 

 

C ++ code

# Include <iostream>

# Include <fstream>

# Include <algorithm>

# Include <string>

# Include <set>

// # Include <map>

# Include <queue>

# Include <utility>

# Include <iomanip>

# Include <stack>

# Include <list>

# Include <vector>

# Include <cstdio>

# Include <cstdlib>

# Include <cstring>

# Include <cmath>

# Include <ctime>

# Include <ctype. h>

Using namespace std;

# Define L _ int64

 

Struct point {// point structure

Double x, y;

Point (double a = 0, double B = 0) {x = a, y = B ;}

};

 

Struct line {// line Segment Structure

Point s, e;

Line (point a, point B) {s = a, e = B ;}

Line (){}

} L [2, 105];

 

Double multi (point a, point B, point c) // cross product to determine the point-to-line relationship

{

Double x1, y1, x2, y2;

X1 = B. x-a. x;

Y1 = B. y-a. y;

X2 = c. x-B. x;

Y2 = c. y-B. y;

Return x1 * y2-x2 * y1;

}

 

Bool intersect (line a, line B) // determines whether the two line segments are intersecting.

{

If (max (a. s. x, a. e. x)> = min (B. s. x, B. e. x) & // fast Rejection Test

Max (B. s. x, B. e. x)> = min (a. s. x, a. e. x )&&

Max (a. s. y, a. e. y)> = min (B. s. y, B. e. y )&&

Max (B. s. y, B. e. y)> = min (a. s. y, a. e. y )&&

Multi (a. s, B. s, B. e) * multi (a. e, B. s, B. e) <= 0 & // cross-site test

Multi (B. s, a. s, a. e) * multi (B. e, a. s, a. e) <= 0)

Return true;

Return false;

}

 

Int main ()

{

Int n, I, res, j;

While (scanf ("% d", & n), n)

{

For (I = 0; I <n; I ++)

Scanf ("% lf", & l [I]. s. x, & l [I]. s. y, & l [I]. e. x, & l [I]. e. y );

Res = 0;

For (I = 0; I <n; I ++)

For (j = I + 1; j <n; j ++)

If (intersect (l [I], l [j])

Res ++;

Printf ("% d \ n", res );

}

Return 0;

}

Related Article

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.