主要用叉积来求,感觉计算几何用叉积的地方很多
计算几何这块,黑书上讲得比较好,可以看下,直线相交,有规范相交和非规范相交,求交点,求交点个数等类型
#include<iostream>
#include<stdio.h>
#include<math.h>
struct point
{
double x,y;
}a[110];
double det(double x1,double y1,double x2,double y2)
{
return x1*y2-x2*y1;
}
double cross(point a,point b,point c)
{
return det(b.x-a.x,b.y-a.y,c.x-a.x,c.y-a.y);
}
int dblcmp(double d)
{
if(fabs(d)<1e-6)
return 0;
return d>0?1:-1;
}
int seg(point a,point b,point c,point d)
{
if(cross(a,c,d)*cross(b,c,d)>0)
return 0;
if(cross(c,a,b)*cross(d,a,b)>0)
return 0;
return 1;
}
int main()
{
int n;
while(scanf("%d",&n)==1&&n)
{
int i,j;
for(i=0;i<2*n;i+=2)
{
scanf("%lf%lf%lf%lf",&a[i].x,&a[i].y,&a[i+1].x,&a[i+1].y);
}
int count=0;
for(j=0;j<2*n;j+=2)
{
for(i=j+2;i<2*n;i+=2)
{
if(seg(a[j],a[j+1],a[i],a[i+1]))
count++;
}
}
printf("%d\n",count);
}
return 0;
}