From 46f56f26bfcc6ce26ffd8132ee11bf019eef3289 Mon Sep 17 00:00:00 2001 From: yy1717 <fctom1215@outlook.com> Date: 星期一, 13 四月 2020 11:48:28 +0800 Subject: [PATCH] 计算边距 --- lib/src/main/cpp/test_common/Geometry.cpp | 37 ++++++++++++++++++++++++++++++++++--- 1 files changed, 34 insertions(+), 3 deletions(-) diff --git a/lib/src/main/cpp/test_common/Geometry.cpp b/lib/src/main/cpp/test_common/Geometry.cpp index db6deb5..3b80f09 100644 --- a/lib/src/main/cpp/test_common/Geometry.cpp +++ b/lib/src/main/cpp/test_common/Geometry.cpp @@ -245,6 +245,7 @@ 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; @@ -254,14 +255,15 @@ 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); - // 鏃嬭浆鍒癤杞� + // (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; @@ -272,16 +274,19 @@ 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; } @@ -471,6 +476,32 @@ 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' -- Gitblit v1.8.0