コード例 #1
0
        /// <summary>
        /// 合并可以合并的线段
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MergeTestBtn_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> mergeLines = GraphicAlgorithm.Merge(lines);

            if (mergeLines != null)
            {
                mergeLines.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);
                });
            }
        }
コード例 #2
0
        /// <summary>
        /// 对多边形进行扩张和缩放,range为负数为内缩小,为正数为外扩
        /// </summary>
        /// <param name="lines"></param>
        /// <param name="range"></param>
        /// <returns></returns>
        public List <Line2D> Elastic(List <Line2D> lines, double range, List <Line2D> neverOffset, bool allowDistortion = false)
        {
            if (range < 0)
            {
                //说明会引起多边形形状的变化
                Line2D line = lines.Find(x => x.Length < Math.Abs(range));

                //说明图形已经边型了,需要变形处理
                if (line != null && allowDistortion)
                {
                    //允许变形的缩放
                    this.ElasticDistortion(lines, range);
                }
            }


            List <Line2D> NLList = new List <Line2D>();
            //首先需要对线段进行一次合并,由于多边形可能存在共线的情况,那么偏移会出问题
            var mergelines = GraphicAlgorithm.Merge(lines);
            //获取当前线段的顺时针排序
            List <Line2D> sortLines = GraphicAlgorithm.Sort(mergelines, null, 1);

            //获取当前连续的顶点
            List <Vector2D> PList = new List <Vector2D>();

            //当前直线的方向
            List <Vector2D> DList = new List <Vector2D>();

            //记录所有的点
            sortLines.ForEach(x => {
                PList.Add(x.Start);
                DList.Add(x.Direction);
            });

            //所有点的列表
            List <Vector2D> NPList = new List <Vector2D>();

            int startIndex, endindex;

            //获取所有的点
            for (int i = 0; i < DList.Count; i++)
            {
                startIndex = i == 0 ? DList.Count - 1 : i - 1;
                endindex   = i;
                //两个线之间的夹角
                double sina = DList[startIndex].Cross(DList[endindex]);
                //用于判断当前是阴阳角
                var Corndir = 0;

                if (sina > 0)
                {
                    Corndir = 1;
                }
                else
                {
                    Corndir = -1;
                }
                //由于偏移出是平行四边形,则要移动的长度为
                var moveLen = -range / sina;

                Vector2D movedir = null;
                if (neverOffset != null)
                {
                    var nf = neverOffset.FindAll(x => x.Start.IsAlmostEqualTo(PList[i]) || x.End.IsAlmostEqualTo(PList[i]));

                    if (nf != null && nf.Count == 1)
                    {
                        if (nf[0].Direction.IsAlmostEqualTo(DList[startIndex]) || nf[0].Direction.IsAlmostEqualTo(-DList[startIndex]))
                        {
                            movedir = DList[startIndex];
                            Vector2D npt = PList[i] + movedir * range * Corndir;
                            NPList.Add(npt);
                        }
                        else
                        {
                            movedir = -DList[endindex];
                            Vector2D npt = PList[i] + movedir * range * Corndir;
                            NPList.Add(npt);
                        }

                        continue;
                    }
                    if (nf != null && nf.Count == 2)
                    {
                        NPList.Add(PList[i]);
                        continue;
                    }
                }
                //移动的方向为
                movedir = DList[endindex] - DList[startIndex];
                Vector2D np = PList[i] + movedir * moveLen;
                //添加新点
                NPList.Add(np);
            }

            ///形成新的线
            for (int i = 0; i < NPList.Count; i++)
            {
                var index = i == (NPList.Count - 1) ? 0 : i + 1;
                NLList.Add(Line2D.Create(NPList[i], NPList[index]));
            }

            return(NLList);
        }