计算几何——叉积
叉积是计算几何的基础,首先我们要知道点,和线的表达。
struct point{
double x,y;
};//点
struct line{
point a,b;
};//线
struct Vector{
point a,b;
};//向量
叉积和点乘
点乘
可用于计算两个角的夹角的cos值。
叉积
1、计算三角形面积
2、计算多边形面积
将一个多边形上各点连接一个点,多个三角形加减可得多边形面积。
3、判位置?
已知一条边,AB,有一点C, 计算叉积AB X AC,如果>0,则C点在AB前侧,若<0(夹角超过180°),则C点在AB后侧。
来道题呗。POJ 2398
不过这道题要二分找点
4、极角排序
链接:计算几何——极角排序
叉积的计算
三个点的叉积
double cross(point a,point b,point c)
{
return (a.x-b.x)*(c.y-b.y)-(a.y-b.y)*(c.x-b.x);
}
两条边(向量)的叉积
double cross(double x1,double y1,double x2,double y2)
{
return x1*y2-x2*y1;
}
或者写进结构体里?
struct Vector{
double x,y;
point(double x=0, double y=0):x(x), y(y){}
point operator - (const point &t)const{
return point(x-t.x, y-t.y);
}//a - b
double operator *(const point &t)const{
return x*t.x + y*t.y;
}//a * b
double operator ^(const point &t)const{
return x*t.y - y*t.x;
}//a X b
};