Пример #1
0
        /// <summary>
        /// 线和多边形的关系
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PInsideTestBtn_Click(object sender, RoutedEventArgs e)
        {
            List <Geometry2D> gss   = this.drawingKernel.GeometryShapes;
            List <Line2D>     lines = new List <Line2D>();

            List <Line2D> vts = new List <Line2D>();

            foreach (var g in gss)
            {
                if (g is PolygonGeometry)
                {
                    PolygonGeometry poly = g as DrawingKernel.Geometries.Primitives.PolygonGeometry;
                    g.FillColor = Colors.Azure;
                    g.Opacity   = 0.3;
                    poly.Update();
                    lines.AddRange(poly.Lines);
                }
                if (g is DrawingKernel.Geometries.Primitives.LineGeometry)
                {
                    DrawingKernel.Geometries.Primitives.LineGeometry l = g as DrawingKernel.Geometries.Primitives.LineGeometry;
                    vts.Add(Line2D.Create(l.Start, l.End));
                }
                if (g.GeometryId == "tp")
                {
                    this.drawingKernel.RemoveGeometryShape(g);
                }
            }

            if (vts != null)
            {
                for (int i = 0; i < vts.Count; i++)
                {
                    var rs = GraphicAlgorithm.LineInsideOfRegion(vts[i], lines, null);
                    if (rs == 1)
                    {
                        DrawingKernel.Geometries.Primitives.LineGeometry lg = new DrawingKernel.Geometries.Primitives.LineGeometry();
                        lg.Start      = vts[i].Start;
                        lg.End        = vts[i].End;
                        lg.PenColor   = Colors.Blue;
                        lg.GeometryId = "tp";
                        this.drawingKernel.AddShape(lg);
                    }
                    else if (rs == 0)
                    {
                        DrawingKernel.Geometries.Primitives.LineGeometry lg = new DrawingKernel.Geometries.Primitives.LineGeometry();
                        lg.PenColor   = Colors.Red;
                        lg.Start      = vts[i].Start;
                        lg.End        = vts[i].End;
                        lg.GeometryId = "tp";
                        this.drawingKernel.AddShape(lg);
                    }
                    else
                    {
                        DrawingKernel.Geometries.Primitives.LineGeometry lg = new DrawingKernel.Geometries.Primitives.LineGeometry();
                        lg.PenColor   = Colors.Black;
                        lg.Start      = vts[i].Start;
                        lg.End        = vts[i].End;
                        lg.GeometryId = "tp";
                        this.drawingKernel.AddShape(lg);
                    }
                }
            }
        }
        /// <summary>
        /// 检测当前多边形和另外一个多边形的关系,
        /// 0图形相交
        /// 1 表示包容,返回较小的多边形区域,为共有区域
        /// -1 表示相离
        /// </summary>
        /// <param name="clines">需要检测的多边形</param>
        /// <param name="outLines">目标多边形</param>
        /// <param name="innerLines">相交的区域</param>
        /// <returns></returns>
        public int Check(List <Line2D> clines, List <Line2D> outLines, out List <List <Line2D> > innerLines)
        {
            int ins = 0, ous = 0, gon = 0;

            //相交的区域
            innerLines = new List <List <Line2D> >();

            //循环当前所有的线段
            for (int i = 0; i < clines.Count; i++)
            {
                //当前线段是否在指定多边形内部
                var result = GraphicAlgorithm.LineInsideOfRegion(clines[i], outLines, null);

                //假如是内部,则内部数量+1
                if (result == 1)
                {
                    ins++;
                }
                //假如在外部,则外部数量+1
                else if (result == -1)
                {
                    ous++;
                }
                else if (result == 2)
                {
                    gon++;
                }
                //否则有相交区域,则不记录
                else
                {
                    break;
                }
            }
            //都在内部,说明当前图形被包含在指定图形内部
            if ((ins + gon) == clines.Count)
            {
                innerLines.Add(clines);
                return(1);
            }

            //全部在外部,有两种情况,一种是两个图形分离,一种是当前图形包含了指定图形
            else if ((ous + gon) == clines.Count)
            {
                //开始检测两个之间的关系,找到目标图形的一个线,判断在当前图形的关系
                var r = GraphicAlgorithm.LineInsideOfRegion(outLines[0], clines, null);
                //说明当前outLines就是在clines的内部
                if (r == 1)
                {
                    innerLines.Add(outLines);
                    return(1);
                }
                //两个图形属于相离的状态
                else
                {
                    innerLines = null;
                    return(-1);
                }
            }
            //计算没有记录
            else
            {
                //没有找到一个相交的线,说明两个图形相连
                if (ins + ous + gon == clines.Count)
                {
                    innerLines = null;
                    return(-1);
                }
                //说明图形确实相交,需要获取相交区域
                else
                {
                    innerLines = GetSuperposition(clines, outLines);
                    return(0);
                }
            }
        }