Exemplo n.º 1
0
        /// <summary>
        /// 获取有效的多边形
        /// </summary>
        /// <param name="PG">多边形PG</param>
        /// <returns>返回多边形</returns>
        public static PolygonD?GetValidatePolygon(PolygonD PG)
        {
            List <PointD> pts = new List <PointD>();

            for (Int32 i = 0; i < PG.Vertex.Count; ++i)
            {
                if (pts.Contains(PG.Vertex[i]) == false)
                {
                    if (pts.Count > 2)
                    {
                        //斜率相等
                        if (LineAlgorithm.Gradient(new LineD(pts[pts.Count - 2], pts[pts.Count - 1])) == LineAlgorithm.Gradient(new LineD(pts[pts.Count - 1], PG.Vertex[i])))
                        {
                            pts[pts.Count - 1] = PG.Vertex[i];
                        }
                        else
                        {
                            pts.Add(PG.Vertex[i]);
                        }
                    }
                }
            }
            if (pts.Count < 3)
            {
                return(null);
            }
            return(new PolygonD(pts.ToArray()));
        }
        public void UpdateRegion(FenceRegionsInfo info, string[] gateIds)
        {
            if (gateIds == null || gateIds.Length == 0)
            {
                return;
            }
            lock (_obj)
            {
                Name    = info.Name;
                IsInner = info.IsInner;
                GateIds = gateIds;
                Regions = new PolygonD();
                Regions.AddPoints(new PointDArray(info.Region));
                RegionId = info.ID;

                double left   = info.Region.Min(r => r.X);
                double right  = info.Region.Max(r => r.X);
                double top    = info.Region.Min(r => r.Y);
                double bottom = info.Region.Max(r => r.Y);
                ValidPoly        = RectangleD.FromLTRB(left - 0.04, top - 0.04, right + 0.04, bottom + 0.04);
                _strictValidPoly = RectangleD.FromLTRB(left - 0.01, top - 0.01, right + 0.01, bottom + 0.01);
                string gateStrs = "";
                if (GateIds != null)
                {
                    foreach (var gate in GateIds)
                    {
                        gateStrs += " - " + gate;
                    }
                }
                trace($"更新区域,{Name} - 区域ID {RegionId} - 闸机数量 {GateIds?.Length}\r\n{gateStrs}。");
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// 判断是否为凸多边形
 /// </summary>
 /// <param name="PG">PG多边形</param>
 /// <returns>如果是凸多边形返回True,否则返回False。</returns>
 public static Boolean IsConvexPolygon(PolygonD PG)
 {
     if (PG.Vertex == null)
     {
         return(false);
     }
     if (PG.Vertex.Count < 4)
     {
         return(true);
     }
     for (Int32 i = 0; i < PG.Vertex.Count; ++i)
     {
         LineD  line = new LineD(PG.Vertex[i], PG.Vertex[(i + 1) % PG.Vertex.Count]);
         Double flag = 0;
         for (Int32 j = 0; j < PG.Vertex.Count; ++j)
         {
             Double result = LineAlgorithm.Position(line, PG.Vertex[j]);
             if ((flag * result) < 0)//点在线的两侧则返回失败。
             {
                 return(false);
             }
             if (result != 0)
             {
                 flag = result;
             }
         }
     }
     return(true);
 }
Exemplo n.º 4
0
        /// <summary>
        /// 计算点P到多边形PG的最近距离
        /// </summary>
        /// <param name="P">点P</param>
        /// <param name="PG">多边形PG</param>
        /// <returns>返回最近距离</returns>
        public static Double ClosestDistance(PointD P, PolygonD PG)
        {
            Double result = Double.MaxValue;

            foreach (LineD S in PG)
            {
                result = System.Math.Min(result, ClosestDistance(P, S));
            }
            return(result);
        }
Exemplo n.º 5
0
        /// <summary>
        /// 计算偏移多边形PG
        /// </summary>
        /// <param name="PG">多边形PG</param>
        /// <param name="velocity">偏移速度</param>
        /// <returns>返回偏移多边形</returns>
        public static PolygonD Offset(PolygonD PG, PointD velocity)
        {
            List <PointD> list = new List <PointD>();

            for (Int32 i = 0; i < PG.Vertex.Count; ++i)
            {
                list.Add(PG.Vertex[i] + velocity);
            }
            return(new PolygonD(list.ToArray()));
        }
Exemplo n.º 6
0
        /// <summary>
        /// 计算多边形PG面积
        /// </summary>
        /// <param name="PG">多边形PG</param>
        /// <returns>返回面积。</returns>
        public static Double Area(PolygonD PG)
        {
            //formula  (1/2) *( (Xi*Yi+1 -Xi+1*Yi) +...)
            Double result = 0;

            for (Int32 i = 0; i < PG.Vertex.Count; ++i)
            {
                result += PointAlgorithm.Multiple(PG.Vertex[i % PG.Vertex.Count], PG.Vertex[(i + 1) % PG.Vertex.Count]);
            }
            return(System.Math.Abs(0.5 * result));
        }
Exemplo n.º 7
0
 /// <summary>
 /// 判断矩形R是否在多边形内
 /// </summary>
 /// <param name="PG">PG多边形</param>
 /// <param name="R">R矩形</param>
 /// <returns>如果R矩形在区域内返回True,否则返回False.</returns>
 public static Boolean InPolygon(PolygonD PG, RectangleD R)
 {
     foreach (LineD L in R)
     {
         if (InPolygon(PG, L) == false)
         {
             return(false);
         }
     }
     return(true);
 }
Exemplo n.º 8
0
 /// <summary>
 /// 判断多边形PL是否在多边形内
 /// </summary>
 /// <param name="PG">PG多边形</param>
 /// <param name="PL">PL多边形</param>
 /// <returns>如果PL多边形在区域内返回True,否则返回False.</returns>
 public static Boolean InPolygon(PolygonD PG, PolygonD PL)
 {
     foreach (LineD L in PL)
     {
         if (InPolygon(PG, L) == false)
         {
             return(false);
         }
     }
     return(true);
 }
Exemplo n.º 9
0
        /// <summary>
        /// 点P是否在多边形区域内
        /// </summary>
        /// <param name="PG">多边形PL</param>
        /// <param name="P">点P</param>
        /// <returns>如果点P在区域内返回True,否则返回False.</returns>
        /// <remarks>此处不考虑平行边的情况</remarks>
        public static Boolean InPolygon(PolygonD PG, PointD P)
        {
            //count ← 0;
            //以P为端点,作从右向左的射线L;
            //for 多边形的每条边s
            // do if P在边s上
            //      then return true;
            //    if s不是水平的
            //      then if s的一个端点在L上
            //             if 该端点是s两端点中纵坐标较大的端点
            //               then count ← count+1
            //           else if s和L相交
            //             then count ← count+1;
            //if count mod 2 = 1
            //  then return true;
            //else return false;
            Int32 count = 0;
            LineD L     = new LineD(P, new PointD(Double.MaxValue, P.Y));

            foreach (LineD S in PG)
            {
                if (LineAlgorithm.OnLine(S, P) == true)
                {
                    return(true);
                }
                if (LineAlgorithm.Gradient(S) != 0)//不是水平线段
                {
                    if (LineAlgorithm.OnLine(L, S.Starting) || LineAlgorithm.OnLine(L, S.End))
                    {
                        if (LineAlgorithm.OnLine(L, S.Starting) && S.Starting.Y > S.End.Y)
                        {
                            ++count;
                        }
                        if (LineAlgorithm.OnLine(L, S.End) && S.End.Y > S.Starting.Y)
                        {
                            ++count;
                        }
                    }
                    else if (LineAlgorithm.HasIntersection(S, L) > 0)
                    {
                        ++count;
                    }
                }
            }
            //如果X的水平射线和多边形的交点数是奇数个则点在多边形内,否则不在区域内。
            if (count % 2 == 0)
            {
                return(false);
            }
            return(true);
        }
Exemplo n.º 10
0
        /// <summary>
        /// 判断圆C是否在多边形PG内
        /// </summary>
        /// <param name="PG">多边形PG</param>
        /// <param name="C">圆C</param>
        /// <returns>如果圆C在区域内返回True,否则返回False.</returns>
        public static Boolean InPolygon(PolygonD PG, CircleD C)
        {
            //如果圆心不在多边形内则返回不在多边形内
            if (false == InPolygon(PG, C.Center))
            {
                return(false);
            }
            Double D = PointAlgorithm.ClosestDistance(C.Center, PG);

            if (D > C.Radius || DoubleAlgorithm.Equals(D, C.Radius))
            {
                return(true);
            }
            return(false);
        }
Exemplo n.º 11
0
 /// <summary>
 /// 判断多边形PG是否在圆内
 /// </summary>
 /// <param name="C">圆C</param>
 /// <param name="PG">多边形PG</param>
 /// <returns>如果在圆内返回True,否则返回False。</returns>
 public static Boolean InCircle(CircleD C, PolygonD PG)
 {
     if (PG.Vertex == null)
     {
         return(false);
     }
     for (Int32 i = 0; i < PG.Vertex.Count; ++i)
     {
         if (PointAlgorithm.Distance(PG.Vertex[i], C.Center) > C.Radius)
         {
             return(false);
         }
     }
     return(true);
 }
Exemplo n.º 12
0
 /// <summary>
 /// 多边形是否在区域内
 /// </summary>
 /// <param name="Rect">矩形区域</param>
 /// <param name="PL">多边形PL</param>
 /// <returns>
 /// 返回True表示多边形PL在区域内,返回False则不在区域内.
 /// </returns>
 public static Boolean InRectangle(RectangleD Rect, PolygonD PL)
 {
     if (PL.Vertex == null)
     {
         return(false);
     }
     for (Int32 i = 0; i < PL.Vertex.Count; ++i)
     {
         if (false == InRectangle(Rect, PL.Vertex[i]))
         {
             return(false);
         }
     }
     return(true);
 }
Exemplo n.º 13
0
        /// <summary>
        /// 获取线段L与多边形PG的交点集合
        /// </summary>
        /// <param name="L">线段L</param>
        /// <param name="PG">多边形PG</param>
        /// <returns>返回交点集合,如果有无数个交点则返回null.</returns>
        public static PointD[] Intersection(LineD L, PolygonD PG)
        {
            List <PointD> result = new List <PointD>();

            foreach (LineD S in PG)
            {
                PointD[] P = Intersection(L, S);
                if (P == null)
                {
                    return(null);
                }
                if (P.Length == 0)
                {
                    continue;
                }
                if (result.Contains(P[0]) == false)
                {
                    result.Add((PointD)P[0]);
                }
            }
            return(result.ToArray());
        }
Exemplo n.º 14
0
        /// <summary>
        /// 判断多边形PG与线段L的交点个数
        /// </summary>
        /// <param name="L">线段L</param>
        /// <param name="PG">多边形PG</param>
        /// <returns>相交返回交点数目,否则返回0</returns>
        public static Int32?HasIntersection(LineD L, PolygonD PG)
        {
            Int32 count = 0;

            foreach (LineD S in PG)
            {
                Int32?result = HasIntersection(L, S);
                if (result == null)
                {
                    return(null);
                }
                count += (Int32)result;
            }
            foreach (PointD P in PG.Vertex)
            {
                if (OnLine(L, P) == true)
                {
                    count--;
                }
            }
            return(count);
        }
Exemplo n.º 15
0
            public PolygonD ToPolygonD(bool autocomplete, double width, double height)
            {
                if (Complete)
                {
                    return(new PolygonD(_processedPoints));
                }

                if (!autocomplete)
                {
                    return(null);
                }

                var firstPointEdge  = rectEdge(_processedPoints[0], width, height);
                var lastPoint       = _processedPoints[_processedPoints.Count - 1];
                var lastPointEdge   = rectEdge(lastPoint, width, height);
                var origNumVertices = _processedPoints.Count;
                var origUnprocessed = _unprocessedEdges.ToList();

                // Clockwise
                while (true)
                {
                    if (lastPointEdge == firstPointEdge)
                    {
                        break;
                    }

                    var upe = _unprocessedEdges.Count == 0 ? null : _unprocessedEdges
                              .SelectTwo(e => new { Edge = e, Start = true, Point = e.Start.Value }, e => new { Edge = e, Start = false, Point = e.End.Value })
                              .FirstOrDefault(inf =>
                                              lastPointEdge == 0 ? (inf.Point.Y == 0 && inf.Point.X > lastPoint.X) :
                                              lastPointEdge == 1 ? (inf.Point.X == width && inf.Point.Y > lastPoint.Y) :
                                              lastPointEdge == 2 ? (inf.Point.Y == height && inf.Point.X < lastPoint.X) : (inf.Point.X == 0 && inf.Point.Y < lastPoint.Y));

                    if (upe != null)
                    {
                        _unprocessedEdges.Remove(upe.Edge);
                        _processedPoints.Add(upe.Start ? upe.Edge.Start.Value : upe.Edge.End.Value);
                        AddEdge(upe.Edge);
                        if (_unprocessedEdges.Count > 0)
                        {
                            throw new Exception("Assertion failed: There should be no unprocessed edges left.");
                        }
                        lastPointEdge = rectEdge(_processedPoints[_processedPoints.Count - 1], width, height);
                    }
                    else
                    {
                        lastPoint =
                            lastPointEdge == 0 ? new PointD(width, 0) :
                            lastPointEdge == 1 ? new PointD(width, height) :
                            lastPointEdge == 2 ? new PointD(0, height) : new PointD(0, 0);
                        _processedPoints.Add(lastPoint);
                        lastPointEdge = (lastPointEdge + 1) % 4;
                    }
                }
                if (_unprocessedEdges.Count == 0)
                {
                    var polygon = new PolygonD(_processedPoints);
                    if (polygon.ContainsPoint(Site))
                    {
                        return(polygon);
                    }
                }
                _processedPoints.RemoveRange(origNumVertices, _processedPoints.Count - origNumVertices);
                _unprocessedEdges = origUnprocessed;

                // Counter-clockwise
                lastPoint     = _processedPoints[_processedPoints.Count - 1];
                lastPointEdge = rectEdge(lastPoint, width, height);
                while (true)
                {
                    if (lastPointEdge == firstPointEdge)
                    {
                        break;
                    }

                    var upe = _unprocessedEdges.Count == 0 ? null : _unprocessedEdges
                              .SelectTwo(e => new { Edge = e, Start = true, Point = e.Start.Value }, e => new { Edge = e, Start = false, Point = e.End.Value })
                              .FirstOrDefault(inf =>
                                              lastPointEdge == 0 ? (inf.Point.Y == 0 && inf.Point.X < lastPoint.X) :
                                              lastPointEdge == 1 ? (inf.Point.X == width && inf.Point.Y < lastPoint.Y) :
                                              lastPointEdge == 2 ? (inf.Point.Y == height && inf.Point.X > lastPoint.X) : (inf.Point.X == 0 && inf.Point.Y > lastPoint.Y));

                    if (upe != null)
                    {
                        _unprocessedEdges.Remove(upe.Edge);
                        _processedPoints.Add(upe.Start ? upe.Edge.Start.Value : upe.Edge.End.Value);
                        AddEdge(upe.Edge);
                        if (_unprocessedEdges.Count > 0)
                        {
                            throw new Exception("Assertion failed: There should be no unprocessed edges left.");
                        }
                        lastPointEdge = rectEdge(_processedPoints[_processedPoints.Count - 1], width, height);
                    }
                    else
                    {
                        lastPoint =
                            lastPointEdge == 0 ? new PointD(0, 0) :
                            lastPointEdge == 1 ? new PointD(width, 0) :
                            lastPointEdge == 2 ? new PointD(width, height) : new PointD(0, height);
                        _processedPoints.Add(lastPoint);
                        lastPointEdge = (lastPointEdge + 3) % 4;
                    }
                }
                //if (_unprocessedEdges.Count > 0)
                //    throw new Exception("Assertion failed: There should be no unprocessed edges left.");
                return(new PolygonD(Enumerable.Reverse(_processedPoints)));
            }
Exemplo n.º 16
0
        /// <summary>
        /// 线段L是否在多边形区域内
        /// </summary>
        /// <param name="PG">多边形PG</param>
        /// <param name="L">线段L</param>
        /// <returns>如果线段L在区域内返回True,否则返回False.</returns>
        public static Boolean InPolygon(PolygonD PG, LineD L)
        {
            //if 线端PQ的端点不都在多边形内
            //  then return false;
            //点集pointSet初始化为空;
            //for 多边形的每条边s
            //  do if 线段的某个端点在s上
            //       then 将该端点加入pointSet;
            //     else if s的某个端点在线段PQ上
            //       then 将该端点加入pointSet;
            //     else if s和线段PQ相交 // 这时候已经可以肯定是内交了
            //       then return false;
            //将pointSet中的点按照X-Y坐标排序;
            //for pointSet中每两个相邻点 pointSet[i] , pointSet[ i+1]
            //  do if pointSet[i] , pointSet[ i+1] 的中点不在多边形中
            //       then return false;
            //return true;
            List <PointD> PointList = new List <PointD>();

            if (InPolygon(PG, L.Starting) == false)
            {
                return(false);
            }
            foreach (LineD S in PG)
            {
                if (LineAlgorithm.OnLine(S, L.Starting) == true)
                {
                    PointList.Add(L.Starting);
                }
                else if (LineAlgorithm.OnLine(S, L.End) == true)
                {
                    PointList.Add(L.End);
                }
                else if (LineAlgorithm.OnLine(L, S.Starting) == true)
                {
                    PointList.Add(S.Starting);
                }
                else if (LineAlgorithm.OnLine(L, S.End) == true)
                {
                    PointList.Add(S.End);
                }
                else if (LineAlgorithm.HasIntersection(L, S) > 0)
                {
                    return(false);
                }
            }
            PointD[] OrderedPointList = PointList.ToArray();
            for (Int32 i = 0; i < (OrderedPointList.Length - 1); ++i)
            {
                for (Int32 j = 0; j < (OrderedPointList.Length - i - 1); j++)
                {
                    MinMax <PointD> MM = new MinMax <PointD>(OrderedPointList[j], OrderedPointList[j + 1]);
                    OrderedPointList[j]     = MM.Min;
                    OrderedPointList[j + 1] = MM.Max;
                }
            }
            for (Int32 i = 0; i < (OrderedPointList.Length - 1); ++i)
            {
                if (false == InPolygon(PG, PointAlgorithm.MidPoint(OrderedPointList[i], OrderedPointList[i + 1])))
                {
                    return(false);
                }
            }
            return(true);
        }