想到了这个东东,写了一程序。希望对大家有用。
也请大家指点,可能很多情况没考虑到
算法:
对于直线的特殊状态(水平,垂直)作特殊处理
所以接下来直线可以看作一是矩形的对角线
现在考虑在矩形的情况(如下图两种情况)
(对角线为题中所说的直线,判断的点为(a,b) )
现在只要分别判断两种情况下的b' 与b 的值大小就可作出点在线上,线左、线右了
程序如下:
/// <summary>
/// 判断点相对于直线的状态
/// </summary>
/// <param name="dPtX"></param>
/// <param name="dPtY"></param>
/// <param name="dLineX1"></param>
/// <param name="dLineY1"></param>
/// <param name="dLineX2"></param>
/// <param name="dLineY2"></param>
/// <returns>0:在线上1:在线的左边2:在线的右边 3:在线的上面4:在线的下面</returns>
public int PtInLine( double dPtX,double dPtY,double dLineX1,double dLineY1,double dLineX2,double dLineY2 )
{
if( dLineX1 == dLineX2 )
{//垂直
if( dPtX == dLineX1 )
return 0;
if( dPtX < dLineX1 )
return 1;
return 2;
}
if( dLineY1 == dLineY2 )
{//水平
if( dPtY == dLineY1 )
return 0;
if( dPtY < dLineY1 )
return 4;
return 3;
}
if( dLineX1 < dLineX2 )
{
if( dLineY1 > dLineY2 )
{
return PtInLineStd_Up( dPtX,dPtY,dLineX1,dLineY1,dLineX2,dLineY2 );
}
else
{
return PtInLineStd_Down( dPtX,dPtY,dLineX1,dLineY1,dLineX2,dLineY2 );
}
}
else
{
if( dLineY1 > dLineY2 )
{
return PtInLineStd_Down( dPtX,dPtY,dLineX2,dLineY2,dLineX1,dLineY1 );
}
else
{
return PtInLineStd_Up( dPtX,dPtY,dLineX2,dLineY2,dLineX1,dLineY1 );
}
}
}
/// <summary>
/// 点(dLineX1,dLineY1) 在另一点的上面
/// </summary>
/// <param name="dPtX"></param>
/// <param name="dPtY"></param>
/// <param name="dLineX1"></param>
/// <param name="dLineY1"></param>
/// <param name="dLineX2"></param>
/// <param name="dLineY2"></param>
/// <returns></returns>
int PtInLineStd_Up( double dPtX,double dPtY,double dLineX1,double dLineY1,double dLineX2,double dLineY2 )
{
double dLeft = ( dPtY - dLineY2 ) * ( dLineX2 - dLineX1 ) ;
double dRight= ( dLineX2 - dPtX ) * ( dLineY1 - dLineY2 );
if( dLeft > dRight )
return 2;
else if( dRight > dLeft )
return 1;
else
return 0;
}
/// <summary>
/// 点(dLineX1,dLineY1) 在另一点的下面
/// </summary>
/// <param name="dPtX"></param>
/// <param name="dPtY"></param>
/// <param name="dLineX1"></param>
/// <param name="dLineY1"></param>
/// <param name="dLineX2"></param>
/// <param name="dLineY2"></param>
/// <returns></returns>
int PtInLineStd_Down( double dPtX,double dPtY,double dLineX1,double dLineY1,double dLineX2,double dLineY2 )
{
double dLeft = ( dPtY - dLineY1 ) * ( dLineX2 - dLineX1 ) ;
double dRight= ( dPtX - dLineX1 ) * ( dLineY2 - dLineY1 );
if( dLeft > dRight )
return 1;
else if( dRight > dLeft )
return 2;
else
return 0;
}
附:
代码贴上来真TMD难看