Geometry 判斷幾何是否被另外一個幾何/線段分割成多段

以下圖,如何判斷幾何多邊形A被多邊形B,切割爲多段幾何?ide

 幾何A被幾何B切割

1. 獲取幾何A與幾何B的交集C函數

 var intersectGeometry = new CombinedGeometry(GeometryCombineMode.Intersect, geometry1, geometry2); spa

 

 

2.幾何A排除交集C,獲得餘下空白區域D3d

 var combinedGeometry = new CombinedGeometry(GeometryCombineMode.Exclude, geometry1, intersectGeometry); code

 

 3.判斷幾何D區域是否包含多段幾何blog

幾何D區分爲倆段,獲取域的邊框近似點集,發現含有倆段線條的描述(倆段M->z的文本),與真實幾何分段對應。string

因此,能夠經過線條終止字符"z"個數,來判斷幾何的分段數量。it

  • 獲取幾何的近似多邊形值
  • 獲取其路徑內的點集
  • 判斷點集中是否含有2個及以上的線條繪製結束字符"z"
1     var flattenedPathGeometry = combinedGeometry.GetFlattenedPathGeometry();
2     var outerPointsString = flattenedPathGeometry.Figures.ToString();
3     if (outerPointsString.Length > 2
4         && outerPointsString.Replace("z", string.Empty).Length == outerPointsString.Length - 2)
5     {
6         return true;
7     }

 完整函數見下方代碼io

 1     /// <summary>
 2     /// 檢查幾何是否被另外一個幾何分割成多段
 3     /// </summary>
 4     /// <param name="geometry1"></param>
 5     /// <param name="geometry2"></param>
 6     /// <returns></returns>
 7     private bool CheckGeometryIsDividedByAnotherGeometry(PathGeometry geometry1, Geometry geometry2)
 8     {
 9         var intersectGeometry = new CombinedGeometry(GeometryCombineMode.Intersect, geometry1, geometry2);
10         var combinedGeometry = new CombinedGeometry(GeometryCombineMode.Exclude, geometry1, intersectGeometry);
11         var flattenedPathGeometry = combinedGeometry.GetFlattenedPathGeometry();
12         var outerPointsString = flattenedPathGeometry.Figures.ToString();
13         var geometryList = outerPointsString.Split(new[] { 'M' }, StringSplitOptions.RemoveEmptyEntries).Where(i => i.Contains("z")).Select(i => $"M{i}").ToList();
14         if (geometryList.Count >= 2 && HintStrokePath.Data == null)
15         {
16             var a = Geometry.Parse(geometryList[0]); ;
17             var b = Geometry.Parse(geometryList[1]); ;
18         }
19         if (outerPointsString.Length > 2
20             && outerPointsString.Replace("z", string.Empty).Length == outerPointsString.Length - 2)
21         {
22             return true;
23         }
24         return false;
25     }
View Code

4. 獲取幾何被分割後的多段幾何內容event

解析"M"、"z",分別獲取倆段幾何數據

1     var geometryList = outerPointsString.Split(new[] { 'M' }, StringSplitOptions.RemoveEmptyEntries).Where(i => i.Contains("z")).Select(i => $"M{i}").ToList();
2     if (geometryList.Count >= 2)
3     {
4         var geometry1 = Geometry.Parse(geometryList[0]); ;
5         var geometry2 = Geometry.Parse(geometryList[1]); ;
6     }

幾何被直線分割

幾何被線段分割,如何判斷或者獲取分割後的多段幾何?

直接用線段與幾何重複上面的步驟,是有問題的。

線段相似「M150,130L150,1300 150,170z」去與幾何去交集,CombinedGeometry中的數據是空的

須要給線條添加1的粗細:

  var geometry2 = lineGeometry.GetWidenedPathGeometry(new System.Windows.Media.Pen(System.Windows.Media.Brushes.Black, 1)); 

結果以下圖:

相關文章
相關標籤/搜索