You can Solve a Geometry Problem too
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3670 Accepted Submission(s): 1771
Problem Description
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
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.
Output
For each case, print the number of intersections, and one line one case.
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
Author
lcy
题意:
这题就是给出n组(x1,y1),(x2,y2),要我们求线段的交点的个数。
很少用结构体做题,今天参考了潇洒哥的解法照着打一遍,稍微对结构体的运用有点感觉,结构体在计算几何里运用是很广的因此结构体必须认真的学,用多了就好了。
具体的求法我就不说了,提醒一下就是要会求交点的公式就行A了,这里用的是差乘。
其实我还是想自己用刘汝佳黑书有关于交点的求法去做,敲了下老出问题,只好放弃。。
。 计算几何确实很难。
#include<stdio.h>
struct line
{
double x1,y1,x2,y2;
}line[100];
int judge(struct line line1,struct line line2)
{
double xa1=line1.x1 ;
double ya1=line1.y1 ;
double xa2=line1.x2 ;
double ya2=line1.y2 ;
double xb1=line2.x1 ;
double yb1=line2.y1 ;
double xb2=line2.x2 ;
double yb2=line2.y2 ;
if(((xa2-xa1)*(yb1-ya1)-(xb1-xa1)*(ya2-ya1))*((xa2-xa1)*(yb2-ya1)-(xb2-xa1)*(ya2-ya1))>0)
return 0;
if(((xb2-xb1)*(ya1-yb1)-(xa1-xb1)*(yb2-yb1))*((xb2-xb1)*(ya2-yb1)-(xa2-xb1)*(yb2-yb1))>0)
return 0;
return 1;
}
int main()
{
//struct line line[100];
int i,j,count,n;
while(scanf("%d",&n)!=EOF&&n!=0)
{
count=0;
for(i=0;i<n;i++)
scanf("%lf %lf %lf %lf",&(line[i].x1),&(line[i].y1 ),&line[i].x2 ,&line[i].y2 );
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(judge(line[i],line[j]))
count++;
}
}
printf("%d\n",count);
}
return 0;
}