|
- // 判断两条线是否平行,若平行,则求出平行距离
- static double lines采用parellel(const AcGeVector2d l1, const AcGeVector2d l2)
- {
- double x1 = l1[0], y1 = l1[1], x2 = l1[2], y2 = l1[3]; // 两点式:(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[0], y3 = l2[1], x4 = l2[2], y4 = l2[3]; // 两点式:(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;
- }
复制代码 |
|