0
点赞
收藏
分享

微信扫一扫

HDOJ 1086 线段相交

覃榜言 2023-02-20 阅读 51


You can Solve a Geometry Problem too

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


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


1 3


/*
hdoj 1086
就是线段相交的个数
*/
#include<iostream>
#include<stdio.h>
using namespace std;

struct point{
double x,y;
};

point point1[101],point2[101];

double xmult(point a,point b,point c)//大于零代表a,b,c左转
{
return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
}

bool OnSegment(point a,point b,point c)//a,b,c共线时有效
{
return c.x>=min(a.x,b.x)&&c.x<=max(a.x,b.x)&&c.y>=min(a.y,b.y)&&c.y<=max(a.y,b.y);
}

int cross(point a,point b,point c,point d){//判断ab 与cd是否相交
double d1,d2,d3,d4;
d1=xmult(c,d,a);
d2=xmult(c,d,b);
d3=xmult(a,b,c);
d4=xmult(a,b,d);
if(d1*d2<0&&d3*d4<0) return 1;
else if(d1==0&&OnSegment(c,d,a)) return 1;
else if(d2==0&&OnSegment(c,d,b)) return 1;
else if(d3==0&&OnSegment(a,b,c)) return 1;
else if(d4==0&&OnSegment(a,b,d)) return 1;
return 0;
}
int main()
{
int n,i,j,sum,f;

// freopen("test.txt","r",stdin);
while(scanf("%d",&n)&&n)
{
for(i=0;i<n;i++)
scanf("%lf%lf%lf%lf",&point1[i].x,&point1[i].y,&point2[i].x,&point2[i].y);
sum=0;
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
{
f=cross(point1[i],point2[i],point1[j],point2[j]);//2条线段
if(f==1) sum++;
}
printf("%d\n",sum);
}
return 0;
}




举报

相关推荐

0 条评论