判断两条线段是否相交,可以采用向量积的方式来判断,如下图所示:
现定义一个函数初步判断两线段是否相交,如下代码:
/// <summary>
/// 初步根据外围框大致判断两条线段是否相交
/// </summary>
/// <param name="line01Coords">线段1的坐标,长度为6</param>
/// <param name="line02Coords">线段2的坐标,长度为6</param>
/// <returns>返回类型为bool,如果为true表示两条线段可能相交,如果为false表示两条线段不相交</returns>
private bool JudgeAboutCrossStatus(double[] line01Coords, double[] line02Coords)
{
bool returnResult = true;
//先判断在XY方向的最值
double maxX1, minX1, maxY1, minY1;
maxX1 = minX1 = line01Coords[0];
maxY1 = minY1 = line01Coords[1];
if (line01Coords[0] < line01Coords[3])
maxX1 = line01Coords[3];
else
minX1 = line01Coords[3];
if (line01Coords[1] < line01Coords[4])
maxY1 = line01Coords[4];
else
minY1 = line01Coords[4];
double maxX2, minX2, maxY2, minY2;
maxX2 = minX2 = line02Coords[0];
maxY2 = minY2 = line02Coords[1];
if (line02Coords[0] < line02Coords[3])
maxX2 = line02Coords[3];
else
minX2 = line02Coords[3];
if (line02Coords[1] < line02Coords[4])
maxY2 = line02Coords[4];
else
minY2 = line02Coords[4];
//比较最值大小
if ((minX1 > maxX2) || (maxX1 < minX2) || (minY1 > maxY2) || (maxY1 < minY2))
{
returnResult = false;
}
return returnResult;
}
函数JudgeAboutCrossStatus()如果返回值为true则表示两条线段可能相交,则需要采用向量积的方式来判断是否相交,如果为false则表示两条线段不相交。现在定义一个函数Judge2LinesRelation ()用于判断两条线段是否相交,其代码如下:
/// <summary>
/// 判断两条线段是否相交
/// </summary>
/// <param name="line01Coords">线段1的坐标,长度为6</param>
/// <param name="line02Coords">线段2的坐标,长度为6</param>
/// <returns>返回类型为bool,如果为true表示两条线段相交,如果为false表示两条线段不相交</returns>
private bool Judge2LinesRelation(double[] line01Coords, double[] line02Coords)
{
bool returnResult = true;
returnResult = JudgeAboutCrossStatus(line01Coords, line02Coords);
if (returnResult)//初步判断两条线段可能相交
{
double BAx, BAy, BCx, BCy, BDx, BDy, BABCk, BABDk;
BAx = line01Coords[0] - line01Coords[3];
BAy = line01Coords[1] - line01Coords[4];
BCx = line02Coords[0] - line01Coords[3];
BCy = line02Coords[1] - line01Coords[4];
BABCk = BAx * BCy - BAy * BCx;
BDx = line02Coords[3] - line01Coords[3];
BDy = line02Coords[4] - line01Coords[4];
BABDk = BAx * BDy - BAy * BDx;
if (((BABCk > 0) && (BABDk > 0)) || ((BABCk < 0) && (BABDk < 0)))
{
returnResult = false;
}
else if (((BABCk > 0) && (BABDk < 0)) || ((BABCk < 0) && (BABDk > 0)))
{
double BCBDk;
BCBDk = BCx * BDy - BCy * BDx;
if (((BABDk > 0) && (BCBDk > 0)) || ((BABDk < 0) && (BCBDk < 0)))
{
returnResult = true;
}
else
{
returnResult = false;
}
}
else if ((BABCk == 0)||(BABDk==0))//点C或D在直线AB上
{
double[] templine02Coords = new double[3];
if (BABCk == 0)//点C在直线AB上
{
templine02Coords[0] = line02Coords[0];
templine02Coords[1] = line02Coords[1];
templine02Coords[2] = line02Coords[2];
}
else//点D在直线AB上
{
templine02Coords[0] = line02Coords[3];
templine02Coords[1] = line02Coords[4];
templine02Coords[2] = line02Coords[5];
}
if (line01Coords[0] == line01Coords[3])//是否垂直,是则比较Y值
{
double maxY, minY;
maxY = minY = line01Coords[1];
if (line01Coords[1] < line01Coords[4])
maxY = line01Coords[4];
else
minY = line01Coords[4];
if ((templine02Coords[1] >= minY) && (templine02Coords[1] <= maxY))//在线段上
returnResult = true;
else
returnResult = false;
}
else //比较X值
{
double maxX, minX;
maxX = minX = line01Coords[0];
if (line01Coords[0] < line01Coords[3])
maxX = line01Coords[3];
else
minX = line01Coords[3];
if ((templine02Coords[0] >= minX) && (templine02Coords[0] <= maxX))//在线段上
returnResult = true;
else
returnResult = false;
}
}
}
return returnResult;
}