예제 #1
0
    private void HandleJarvisHull()
    {
        List <Vector2> xy = new List <Vector2>();

        foreach (var item in origins)
        {
            xy.Add(item);
        }
        List <Vector2> vertices = JarvisConvex.BuildHull(xy, 1);

        DrawPolygon(vertices, Color.red);
    }
예제 #2
0
        // 对于环状问题,暂时没处理。处理起来,稍作修改即可。后期遇到时加入(检测环状,然后分离环状,及带孔的多边形环形区域。)
        public static void PolygonExpand(List <Vector2> original, List <Vector2> results, float length = 1.0f, bool useHull = true)
        {
            List <Vector2> origin = new List <Vector2>();

            origin.AddRange(original);
            GeoPointsArray2 hullArray = null;

            if (useHull)
            {
                List <Vector2> hull = JarvisConvex.BuildHull(original);
                GeoPolygonUtils.ReverseIfCW(ref hull);
                hullArray = new GeoPointsArray2(hull);
            }
            float len = length;

            while (results.Count == 0)
            {
                List <Vector2> segs = ExpandPolygonSegments(origin, len);
                float          min  = CheckSegmentsIntersect(segs);
                if (min > len)
                {
                    min = len;
                }
                if (min > 1e-5f)
                {
                    List <Vector2> expands = ExpandPolygon(origin, min);
                    //List<int> vLst = GeoUtils.VertexIndexList(expands);
                    //List<List<int>> circles = CommonUtils.CheckCircle(vLst);
                    // GeoUtils.Split(origin, circles);
                    origin = PolygonIntersectRemove(expands);
                    len    = len - min;
                    if (len < 0.01f)
                    {
                        results.AddRange(origin);
                    }
                }
                else
                {
                    for (int i = 1; i < segs.Count; i += 2)
                    {
                        results.Add(segs[i]);
                    }
                }
                if (useHull && results.Count == 0 && hullArray != null)
                {
                    bool flag = false;
                    for (int i = 0; i < origin.Count; i++)
                    {
                        Vector2 refV = origin[i];
                        if (GeoPolygonUtils.IsPointInConvexPolygon2(hullArray, ref refV))
                        {
                            flag = true;
                            break;
                        }
                    }
                    if (!flag)
                    {
                        List <Vector2> split = JarvisConvex.BuildHull(origin);
                        GeoPolygonUtils.ReverseIfCW(ref split);
                        split = ConvexMarginalExpand(split, len);
                        results.AddRange(split);
                        len = 0;
                    }
                }
            }
        }