Geometry 判断几何是否被另一个几何/线段分割成多段
程序员文章站
2022-07-01 17:43:58
如下图,如何判断几何多边形A被多边形B,切割为多段几何? 几何A被几何B切割 1. 获取几何A与几何B的交集C var intersectGeometry = new CombinedGeometry(GeometryCombineMode.Intersect, geometry1, geometr ......
如下图,如何判断几何多边形a被多边形b,切割为多段几何?
几何a被几何b切割
1. 获取几何a与几何b的交集c
var intersectgeometry = new combinedgeometry(geometrycombinemode.intersect, geometry1, geometry2);
2.几何a排除交集c,得到余下空白区域d
var combinedgeometry = new combinedgeometry(geometrycombinemode.exclude, geometry1, intersectgeometry);
3.判断几何d区域是否包含多段几何
几何d区分为俩段,获取域的边框近似点集,发现含有俩段线条的描述(俩段m->z的文本),与真实几何分段对应。
所以,可以通过线条终止字符"z"个数,来判断几何的分段数量。
- 获取几何的近似多边形值
- 获取其路径内的点集
- 判断点集中是否含有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 }
完整函数见下方代码
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 }
4. 获取几何被分割后的多段几何内容
解析"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));
结果如下图: