You can Solve a Geometry Problem too
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9279 Accepted Submission(s): 4546
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.
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.
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
推荐
向量叉乘
线段1:A1---A2
线段2:B1---B2
判定条件:
(A1A2*A1B1)(A1A2*A1B2)<=0;
(B1B2*B1A1)(B1B2*B1A2)<=0;
能过HDU 不能过1 1 2 2 4 4 5 5
#include<iostream>//c++
#include<cmath>//数学公式
#include<cstdlib>//malloc
#include<cstring>
#include<string>
#include<cstdio>//输入输出
#include<algorithm>//快排
#include<queue>//队列
#include<functional>//优先队列
#include<stack>//栈
#include<vector>//容器
#include<map>//地图 if continue
typedef long long ll;
const int N=105;
const int inf=0x7fffffff;
using namespace std;
int main()
{
double x1[N];
double y1[N];
double x2[N];
double y2[N];
// freopen("C:\\Users\\ch\\Desktop\\1.txt","r",stdin);
//freopen("C:\\Users\\lenovo\\Desktop\\2.txt","w",stdout);
int i,j,k;
int m,n;
int a,b;
while(cin>>n,n)
{
int ans=0;
cin>>x1[0]>>y1[0]>>x2[0]>>y2[0];
for(b=1;b<n;b++)
{
cin>>x1[b]>>y1[b]>>x2[b]>>y2[b];
for(a=0;a<b;a++)
{
if( ((x2[a]-x1[a])*(y1[b]-y1[a])-(y2[a]-y1[a])*(x1[b]-x1[a]))*
((x2[a]-x1[a])*(y2[b]-y1[a])-(y2[a]-y1[a])*(x2[b]-x1[a]))<=0)
if(((x2[b]-x1[b])*(y1[a]-y1[b])-(y2[b]-y1[b])*(x1[a]-x1[b]))*
((x2[b]-x1[b])*(y2[a]-y1[b])-(y2[b]-y1[b])*(x2[a]-x1[b]))<=0)
ans++;
}
}
cout<<ans<<endl;
}
return 0;
}
学习后的
#include<iostream>//c++
#include<cmath>//数学公式
#include<cstdlib>//malloc
#include<cstring>
#include<string>
#include<cstdio>//输入输出
#include<algorithm>//快排
#include<queue>//队列
#include<functional>//优先队列
#include<stack>//栈
#include<vector>//容器
#include<map>//地图 if continue
typedef long long ll;
const int N=105;
const int inf=0x7fffffff;
using namespace std;
typedef struct
{
double x,y;
}G;
double direction(G p1,G p2,G p )
{
return ( p1.x -p.x )*( p2.y-p.y) - ( p2.x -p.x )*( p1.y-p.y);
}
int on_segment(G p1,G p2 ,G p )
{
double ma=max(p1.x , p2.x );
double mi=min(p1.x , p2.x );
if( p.x >=mi && p.x <=ma ) return 1;
return 0;
}
int segments_intersert(G p1,G p2,G p3,G p4 )
{
double d1,d2,d3,d4;
d1 = direction ( p1,p2,p3 );
d2 = direction ( p1,p2,p4 );
d3 = direction ( p3,p4,p1 );
d4 = direction ( p3,p4,p2 );
if( d1*d2<0 && d3*d4<0 ) return 1;
else if( d1==0 && on_segment( p1,p2,p3 ) ) return 1;//防止共线没交点 (1 1 2 2) (4 4 5 5)
else if( d2==0 && on_segment( p1,p2,p4 ) ) return 1;
else if( d3==0 && on_segment( p3,p4,p1 ) ) return 1;
else if( d4==0 && on_segment( p3,p4,p2 ) ) return 1;
return 0;
}
int main()
{
int i,j,n,ans;
G begin[105],end[105];
while(cin>>n,n)
{
ans=0;
for( i=0;i<n;i++) cin>>begin[i].x>>begin[i].y>>end[i].x>>end[i].y;
for( i=0;i<n;i++)
for(j=i+1;j<n;j++)
if( segments_intersert( begin[i],end[i],begin[j],end[j] ) )
ans++;
cout<<ans<<endl;
}
return 0;
}
http://acm.hdu.edu.cn/showproblem.php?pid=1086