| | |
| | | |
| | | Relation IntersectionOf(Line line1, Line line2) |
| | | { |
| | | // Fail if either line segment is zero-length. |
| | | if ((isEqual(line1.X1, line1.X2) && isEqual(line1.Y1, line1.Y2)) || (isEqual(line2.X1, line2.X2) && isEqual(line2.Y1, line2.Y2))) |
| | | return GM_None; |
| | | |
| | |
| | | if ((isEqual(line1.X1, line2.X2) && isEqual(line1.Y1, line2.Y2)) || (isEqual(line1.X2, line2.X2) && isEqual(line1.Y2, line2.Y2))) |
| | | return GM_Intersection; |
| | | |
| | | // 直线坐标变换重合 |
| | | // (1) Translate the system so that point A is on the origin. |
| | | line1.X2 -= line1.X1; line1.Y2 -= line1.Y1; |
| | | line2.X1 -= line1.X1; line2.Y1 -= line1.Y1; |
| | | line2.X2 -= line1.X1; line2.Y2 -= line1.Y1; |
| | | |
| | | // Discover the length of segment A-B. |
| | | double distAB = sqrt(line1.X2 * line1.X2 + line1.Y2 * line1.Y2); |
| | | |
| | | // 旋转到X轴 |
| | | // (2) Rotate the system so that point B is on the positive X axis. |
| | | double theCos = line1.X2 / distAB; |
| | | double theSin = line1.Y2 / distAB; |
| | | double newX = line2.X1 * theCos + line2.Y1 * theSin; |
| | |
| | | line2.Y2 = line2.Y2 * theCos - line2.X2 * theSin; |
| | | line2.X2 = newX; |
| | | |
| | | // Fail if segment C-D doesn't cross line A-B. |
| | | if ((line2.Y1 < 0 && line2.Y2 < 0) || (line2.Y1 >= 0 && line2.Y2 >= 0)) { |
| | | return GM_None; |
| | | } |
| | | |
| | | // (3) Discover the position of the intersection point along line A-B. |
| | | double posAB = line2.X2 + (line2.X1 - line2.X2) * line2.Y2 / (line2.Y2 - line2.Y1); |
| | | |
| | | // Fail if segment C-D crosses line A-B outside of segment A-B. |
| | | if (posAB < 0 || posAB > distAB) { |
| | | return GM_None; |
| | | } |
| | | |
| | | // (4) Apply the discovered position to line A-B in the original coordinate system. |
| | | return GM_Intersection; |
| | | } |
| | | |
| | |
| | | return p4; |
| | | } |
| | | |
| | | /************************************************************** |
| | | * p3于 p1---p2构成直线 |
| | | * @param p1 |
| | | * @param p2 |
| | | * @param p3 |
| | | * @return |
| | | */ |
| | | bool VerticalPointOnLine(PointF point, Line line) |
| | | { |
| | | PointF p1, p2; |
| | | |
| | | p1.X = line.X1; |
| | | p1.Y = line.Y1; |
| | | |
| | | p2.X = line.X2; |
| | | p2.Y = line.Y2; |
| | | |
| | | PointF pv = GetVerticalPoint(p1, p2, point); |
| | | |
| | | if (isEqual2(pv.X, MIN(p1.X, p2.X)) || isEqual2(pv.X, MAX(p1.X, p2.X)) || |
| | | (pv.X > MIN(p1.X, p2.X) && pv.X < MAX(p1.X, p2.X))) { |
| | | return true; |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | /**************************************************************** |
| | | * p3 |
| | | * | 'L' |