/// <summary> /// 打断测试 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void DecomposeTestBtn_Click(object sender, RoutedEventArgs e) { List <Geometry2D> gss = this.drawingKernel.GeometryShapes; List <Line2D> lines = new List <Line2D>(); foreach (var g in gss) { if (g is DrawingKernel.Geometries.Primitives.LineGeometry) { DrawingKernel.Geometries.Primitives.LineGeometry line = g as DrawingKernel.Geometries.Primitives.LineGeometry; lines.Add(Line2D.Create(line.Start, line.End)); } if (g.GeometryId == "tp") { this.drawingKernel.RemoveGeometryShape(g); } } //查找封闭区域 List <Line2D> decomposes = GraphicAlgorithm.Decompose(lines); if (decomposes != null) { decomposes.ForEach(x => { DrawingKernel.Geometries.Primitives.LineGeometry lg = new DrawingKernel.Geometries.Primitives.LineGeometry(x.Start, x.End); lg.PenColor = KernelProperty.GetRandomColor(); lg.GeometryId = "tp"; this.drawingKernel.AddShape(lg); }); } }
/// <summary> /// 获取无法形成封闭区域的线 /// </summary> /// <param name="lines"></param> /// <returns></returns> public List <Line2D> WeedLess(List <Line2D> ilines) { var lines = GraphicAlgorithm.Decompose(ilines); List <Line2D> mweed = new List <Line2D>(lines); //去掉了无端点线段 List <Line2D> weeds = this.Weed(mweed); var weedLess = lines.FindAll(x => !weeds.Contains <Line2D>(x, new Line2DEqualityComparer())); return(weedLess); }
/// <summary> /// 查找最大和最小封闭区域 /// </summary> /// <param name="searchLines"></param> /// <param name="isLargeRegion"></param> /// <param name="isDecompose"></param> /// <returns></returns> public List <List <Line2D> > Lookup(List <Line2D> searchLines, bool isLargeRegion, bool isDecompose = false) { //最少有三条线,不然无法组成封闭区域 if (searchLines.Count < 3) { return(null); } List <List <Line2D> > closets = new List <List <Line2D> >(); //需要处理的所有线 List <Line2D> readyLines = new List <Line2D>(searchLines); //对线进行打断 if (isDecompose) { readyLines = GraphicAlgorithm.Decompose(readyLines); } //剔除有一个点没有连接点的点 readyLines = GraphicAlgorithm.Weed(readyLines); //去除相同的元素 readyLines = readyLines.Distinct(new Line2DEqualityComparer()).ToList(); if (readyLines == null || readyLines.Count == 0) { return(null); } List <Line2D> Large = null; //查找最大的多边形 FindMaxOne(readyLines, ref Large); if (isLargeRegion) { if (Large != null) { closets.Add(Large); } return(closets); } else { if (Large != null) { FindMins(readyLines, Large, closets); } return(closets); } }
/// <summary> /// 进行矩形划分出任意多个矩形元素 /// </summary> /// <param name="lines"></param> /// <returns></returns> public List <List <Line2D> > Division(List <Line2D> lines) { if (lines != null && lines.Count > 4) { //首先要把所有的线打断 List <Line2D> decomposesLines = GraphicAlgorithm.Decompose(lines); //查找最大的封闭区域,且不需要打断 List <List <Line2D> > large = GraphicAlgorithm.ClosedLookup(decomposesLines, true, false); if (large != null && large.Count > 0) { //获取所有外部的线段 List <Line2D> outerSide = large.FirstOrDefault(); //获取所有无端点的线段 List <Line2D> weeds = GraphicAlgorithm.WeedLess(decomposesLines); //获取内部线段,要去掉 //List<Line2D> innerSide = GraphicAlgorithm.RejectLines(decomposesLines,); } } return(null); }
/// <summary> /// 剔除端点无效的点 /// </summary> /// <param name="lines"></param> /// <returns></returns> public List <Line2D> Weed(List <Line2D> ilines) { var lines = GraphicAlgorithm.Decompose(ilines); if (lines != null) { List <Line2D> rets = new List <Line2D>(); for (int i = 0; i < lines.Count; i++) { var start = lines[i].Start; var end = lines[i].End; var one = lines.FindAll(x => x.Start.IsAlmostEqualTo(start) || x.End.IsAlmostEqualTo(start)); var two = lines.FindAll(x => x.Start.IsAlmostEqualTo(end) || x.End.IsAlmostEqualTo(end)); if (one.Count == 1 || two.Count == 1) { //添加一个需要移除的图形元素 rets.Add(lines[i]); } } if (rets.Count > 0) { rets.ForEach(x => { lines.Remove(x); }); Weed(lines); } return(lines); } return(null); }