判断两条线是否平行,若平行,则求出平行距离
// 判断两条线是否平行,若平行,则求出平行距离static double lines采用parellel(const AcGeVector2d l1, const AcGeVector2d l2)
{
double x1 = l1, y1 = l1, x2 = l1, y2 = l1; // 两点式:(y-y1)/(y2-y1)=(x-x1)/(x2-x1)
double a1 = -(y2 - y1), b1 = x2 - x1, c1 = (y2 - y1) * x1 - (x2 - x1) * y1; // 一般式:a1x+b1y1+c1=0
double x3 = l2, y3 = l2, x4 = l2, y4 = l2; // 两点式:(y-y3)/(y4-y3)=(x-x3)/(x4-x3)
double a2 = -(y4 - y3), b2 = x4 - x3, c2 = (y4 - y3) * x3 - (x4 - x3) * y3; // 一般式:a2x+b2y1+c2=0
AcGePoint2d p3(x3, y3), p4(x4, y4);
double d = 0;
if (!(p3 != p4 && a1 * x3 + b1 * y3 + c1 == 0 && a1 * x4 + b1 * y4 + c1 == 0)) // 排除重合的情况
{
if (b2 == 0 && b1 == 0) // l2,l1垂直x轴
{
d = abs(c1 / a1 - c2 / a2);
}
else if (b2 != 0 && b1 != 0 && a2 / b2 == a1 / b1) // l2,l1斜率相同
{
d = abs(c1 - b1 * c2 / b2) / sqrt(a1 * a1 + b1 * b1);
}
}
return d;
}
页:
[1]