HDOJ 1086 You can Solve a Geometry Problem too (判断直线交点个数)

来源:互联网 发布:淘宝详情页模板套用 编辑:程序博客网 时间:2024/05/18 03:55

You can Solve a Geometry Problem too

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9206    Accepted Submission(s): 4509


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
20.00 0.00 1.00 1.000.00 1.00 1.00 0.0030.00 0.00 1.00 1.000.00 1.00 1.00 0.0000.00 0.00 1.00 0.000
 

Sample Output
13
 

Author
lcy


题意:给出n条线段的起点和终点,求n条线段共有多少交点

思路:有可能会存在多条线段相交于一点,也有可能没有交点,用数组储存交点坐标,然后排序去重就行了。。
(注意没有交点时的特判)


ac代码:
#include<stdio.h>#include<string.h>#include<math.h>#include<iostream>#include<algorithm>//#define fab(a) (a)>0?(a):(-a)#define LL long long#define MAXN 1000100#define mem(x) memset(x,0,sizeof(x))#define INF 0xfffffff #define PI acos(-1.0)using namespace std;struct s{double x1,x2,y1,y2;}line[MAXN];struct ss{double x,y;}list[MAXN];int cnt;double xx,yy;double fab(double x){return x>0?x:-x;}bool cmp(ss a,ss b){if(a.y==b.y)return a.x<b.x;return a.y<b.y;}int check(int j,int i){double a1=line[i].y1-line[i].y2,b1=line[i].x2-line[i].x1,c1=line[i].x1*line[i].y2-line[i].x2*line[i].y1;    double a2=line[j].y1-line[j].y2,b2=line[j].x2-line[j].x1,c2=line[j].x1*line[j].y2-line[j].x2*line[j].y1;if(fab((line[i].x1-line[i].x2)*(line[j].y1-line[j].y2)-(line[i].y1-line[i].y2)*(line[j].x1-line[j].x2))<1e-9){return 0;}else{xx=(c1*b2-c2*b1)/(a2*b1-a1*b2);yy=(a2*c1-a1*c2)/(a1*b2-a2*b1);//printf("xx=%lf yy=%lf\n",xx,yy);if(xx<min(line[i].x1,line[i].x2)||xx>max(line[i].x1,line[i].x2))return 0;if(yy<min(line[i].y1,line[i].y2)||yy>max(line[i].y1,line[i].y2))return 0;if(xx<min(line[j].x1,line[j].x2)||xx>max(line[j].x1,line[j].x2))return 0;if(yy<min(line[j].y1,line[j].y2)||yy>max(line[j].y1,line[j].y2))return 0;return 1;}}int main(){int n,k,i,j;while(scanf("%d",&n)!=EOF,n){cnt=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(check(i,j)){list[cnt].x=xx,list[cnt].y=yy;cnt++;}}}if(cnt==0){printf("0\n");continue;}sort(list,list+cnt,cmp);ss k;k.x=list[0].x;k.y=list[0].y;int ans=1;for(i=1;i<cnt;i++){if((fab(list[i].x-k.x)<1e-10)&&(fab(list[i].y-k.y)<1e-10))continue;k=list[i];ans++;}printf("%d\n",ans);}return 0;}


0 0