2013
11-27

# You can Solve a Geometry Problem too

Many geometry（几何）problems were designed in the ACM/ICPC. And now, I also prepare a geometry problem for this final exam. According to the experience of many ACMers, geometry problems are always much trouble, but this problem is very easy, after all we are now attending an exam, not a contest :)
Give you N (1<=N<=100) segments（线段）, please output the number of all intersections（交点）. You should count repeatedly if M (M>2) segments intersect at the same point.

Note:
You can assume that two segments would not intersect at more than one point.

Input contains multiple test cases. Each test case contains a integer N (1=N<=100) in a line first, and then N lines follow. Each line describes one segment with four float values x1, y1, x2, y2 which are coordinates of the segment’s ending.
A test case starting with 0 terminates the input and this test case is not to be processed.

For each case, print the number of intersections, and one line one case.

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

1
3

hdu 1086 求线段焦点数

#include<iostream>
#include<math.h>
#include<stdio.h>
using namespace std;
struct as
{
double x;
double y;
}p[210];
int is_cm(as a,as b,as c,as d)//判断是否相交
{
double Sabc=(b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
double Sabd=(b.x-a.x)*(d.y-a.y)-(b.y-a.y)*(d.x-a.x);
if(Sabc*Sabd>0)
return 0;
double Scda=(d.x-c.x)*(a.y-c.y)-(d.y-c.y)*(a.x-c.x);
double Scdb=(d.x-c.x)*(b.y-c.y)-(d.y-c.y)*(b.x-c.x);
if(Scda*Scdb<=0)
return 1;
else
return 0;
}
int main()
{
int n,i,j,sum;
while(cin>>n,n)
{
sum=0;
for(i=0;i<2*n;i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
for(i=0;i<2*n;i+=2)
for(j=i+2;j<2*n;j+=2)
if(is_cm(p[i],p[i+1],p[j],p[j+1]))
sum++;
cout<<sum<<endl;
}
}