hdu 1086 You can Solve a Geometry Problem too

时间:2023-03-09 09:22:27
hdu 1086 You can Solve a Geometry Problem too

You can Solve a Geometry Problem too

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

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
1.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
Author
lcy
判断两个线段有没有交点  百度  叉积
/*
判断AB和CD两线段是否有交点:
同时满足两个条件:('x'表示叉积)
1.C点D点分别在AB的两侧.(向量(ABxAC)*(ABxAD)<=0)
2.A点和B点分别在CD两侧.(向量(CDxCA)*(CDxCB)<=0)
*/
 #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
struct node{
double x,y;
}a[],b[];
double chaji(node a,node b,node c){
return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
}
int judge(node a,node b,node c,node d){
if(max(a.x,b.x)<min(c.x,d.x)||max(c.x,d.x)<min(a.x,b.x))
return ;
if(max(a.y,b.y)<min(c.y,d.y)||min(a.y,b.y)>max(c.y,d.y))
return ;
if(chaji(a,c,d)*chaji(b,c,d)<=&&(chaji(c,a,b)*chaji(d,a,b)<=))
return ;
//if(chaji(c,d,a,b)<=0||chaji(c,d,b,a)<=0)
// return 1;
//if(chaji(d,c,a,b)<=0||chaji(d,c,b,a)<=0)
// return 1;
return ;
}
int main(){
int t;
int i,j;
while(scanf("%d",&t)!=EOF){
if(t==)
break;
for(i=;i<=t;i++){
scanf("%lf%lf%lf%lf",&a[i].x,&a[i].y,&b[i].x,&b[i].y);
}
int ans=;
for(i=;i<=t;i++){
for(j=i+;j<=t;j++){
if(judge(a[i],b[i],a[j],b[j]))
{
ans++;
//cout<<judge(a[i],b[i],a[j],b[j])<<endl;
}
}
}
printf("%d\n",ans);
} }