Geometry determines whether the geometry is divided into segments by another geometry / line segment

Keywords: C# Windows

As shown in the following figure, how to judge that the geometric polygon A is cut into multi segment geometry by the polygon B?

Geometry A is cut by geometry B

1. Obtain intersection C of geometry A and geometry B

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

 

 

2. Geometry A excludes intersection C to get the remaining blank area D

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

 

3. Judge whether the geometry D area contains multi segment geometry

Geometry D is divided into two segments, and the set of border approximate points of the domain is obtained. It is found that there are two segments of line description (two segments of M - > Z text), corresponding to the real geometry segment.

Therefore, the number of geometric segments can be determined by the number of "z" characters at the end of the line.

  • Get approximate polygon value of geometry
  • Get the set of points in its path
  • Judge whether the point set contains two or more line drawing end characters "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     }

See the following code for the complete function

 1     /// <summary>
 2     /// Check if the geometry is segmented by another geometry
 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     }

4. Get the multi segment geometric content after the geometry is divided

Analyze "M" and "z" to obtain two pieces of geometric data respectively

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     }

Geometry divided by lines

How to judge or get the segmented multi segment geometry?

It is problematic to repeat the above steps directly with line segments and geometry.

The line segment is similar to "m150130l1501300 150170z" to intersect with the geometry. The data in the combined geometry is empty

You need to add a thickness of 1 to the line:

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

The results are as follows:

Posted by johnnyblaze9 on Tue, 19 Nov 2019 11:20:49 -0800