判断一点在已知直线的左侧还是右侧。
f(x,y) = (y-y1)(x2-x1) - (x-x1)(y2-y1)
if f(x3,y3) > 0
点P(x3,y3)在P1P2的左侧
if f(x3,y3) < 0
点P(x3,y3)在P1P2的右侧
if f(x3,y3) = 0
点P(x3,y3)在P1P2线上。
判断两线段是否相交(网上资料):
bool IsLineSegmentCross(POINT pFirst1, POINT pFirst2, POINT pSecond1, POINT pSecond2)
{
//每个线段的两点都在另一个线段的左右不同侧,则能断定线段相交
//公式对于向量(x1,y1)->(x2,y2),判断点(x3,y3)在向量的左边,右边,还是线上.
long Linep1,Linep2;
//判断pSecond1和pSecond2是否在pFirst1->pFirst2两侧
Linep1 = pFirst1.x * (pSecond1.y - pFirst2.y) +
pFirst2.x * (pFirst1.y - pSecond1.y) +
pSecond1.x * (pFirst2.y - pFirst1.y);
Linep2 = pFirst1.x * (pSecond2.y - pFirst2.y) +
pFirst2.x * (pFirst1.y - pSecond2.y) +
pSecond2.x * (pFirst2.y - pFirst1.y);
if ( ((Linep1 ^ Linep2) >= 0 ) && !(Linep1==0 && Linep2==0))//符号位异或为0:pSecond1和pSecond2在pFirst1->pFirst2同侧
{
return false;
}
//判断pFirst1和pFirst2是否在pSecond1->pSecond2两侧
Linep1 = pSecond1.x * (pFirst1.y - pSecond2.y) +
pSecond2.x * (pSecond1.y - pFirst1.y) +
pFirst1.x * (pSecond2.y - pSecond1.y);
Linep2 = pSecond1.x * (pFirst2.y - pSecond2.y) +
pSecond2.x * (pSecond1.y - pFirst2.y) +
pFirst2.x * (pSecond2.y - pSecond1.y);
if ( ((Linep1 ^ Linep2) >= 0 ) && !(Linep1==0 && Linep2==0))//符号位异或为0:pFirst1和pFirst2在pSecond1->pSecond2同侧
{
return false;
}
//否则判为相交
return true;
}