分类: 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)
}
//叉积
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范围内