Chinaunix首页 | 论坛 | 博客
  • 博客访问: 405701
  • 博文数量: 68
  • 博客积分: 2500
  • 博客等级: 少校
  • 技术积分: 728
  • 用 户 组: 普通用户
  • 注册时间: 2006-10-14 00:19
文章分类

全部博文(68)

文章存档

2011年(1)

2009年(1)

2008年(17)

2007年(30)

2006年(19)

我的朋友

分类: C/C++

2008-07-23 13:52:26

    下面这些模版很常见,不建议直接复制->粘贴->打印。

struct point
{
double x,y;
};

double max(double x,double y)
{
return (x>y)?x:y;
}
    
double min(double x,double y)
{
return (x}

//浮点精度处理,三出口函数
int dblcmp(double d)
{
if (fabs(d)return (d>0)?1:-1;
}

//叉积
double det(double x1,double y1,double x2,double y2)
{
return x1*y2-x2*y1;
}

double cross(struct point a,struct point b,struct point c)
{
return det(b.x-a.x,b.y-a.y,c.x-a.x,c.y-a.y);
}
//叉积

//点积(使用较少)
double dotdet(double x1,double y1,double x2,double y2)
{
return x1*x2+y1*y2;
}

double dot(struct point a,struct point b,struct point c)
{
return dotdet(b.x-a.x,b.y-a.y,c.x-a.x,c.y-a.y);
}

点积的三点共线判定
int betweencmp(struct point a,struct point b,struct point c)
{
return dblcmp(dot(a,b,c));
}
//点积(使用较少)

//判断a在不在bc范围内
int xycmp(double p,double mini,double maxi)
{
return dblcmp(p-mini)*dblcmp(p-maxi);
}

int betweencmp(struct point a,struct point b,struct point c)
{
if (fabs(b.x-c.x)>fabs(b.y-c.y))
   return xycmp(a.x,min(b.x,c.x),max(b.x,c.x));
else return xycmp(a.y,min(b.y,c.y),max(b.y,c.y));
}
//判断a在不在bc范围内

阅读(2103) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~