示例#1
0
    void Subdivide()
    {
        var w = boundary.Width;
        var h = boundary.Height;

        var tr = new Boundary2(Center.x, Center.y, w / 2, h / 2);

        topRight = new SilentQuadTree(tr, capacity, Point2Ds, Result, deph + 1);
        var tl = new Boundary2(Center.x - w / 2, Center.y, w / 2, h / 2);

        topLeft = new SilentQuadTree(tl, capacity, Point2Ds, Result, deph + 1);
        var br = new Boundary2(Center.x, Center.y - h / 2, w / 2, h / 2);

        bottomRight = new SilentQuadTree(br, capacity, Point2Ds, Result, deph + 1);
        var bl = new Boundary2(Center.x - w / 2, Center.y - h / 2, w / 2, h / 2);

        bottomLeft = new SilentQuadTree(bl, capacity, Point2Ds, Result, deph + 1);
        divided    = true;
    }
    void MeshLayer(List <Pixel> layer, int i)
    {
        if (!meshed[i])
        {
            var line = new Dictionary <float, List <Pixel> >();

            List <float> row = layer.Select(a => a.coords.y).Distinct().ToList();

            foreach (float r in row)
            {
                line.Add(r, GetMaximumRange(layer.Where(a => a.coords.y == r)));
            }

            ListofPoints = new List <Point2D>();
            foreach (var l in line)
            {
                foreach (Pixel px in l.Value)
                {
                    ListofPoints.Add(MakePoint(px.coords.x, px.coords.y));
                }
            }

            Vector2 middle = new Vector2(layer.Min(a => a.coords.x), layer.Min(a => a.coords.y));
            float   height = layer.Max(a => a.coords.x) - middle.x;
            float   width  = layer.Max(a => a.coords.y) - middle.y;

            Boundary2 boundaryy = new Boundary2(middle, Mathf.Max(height, width), Mathf.Max(height, width));

            List <Vector2> res = new List <Vector2>();

            SilentQuadTree quadTree = new SilentQuadTree(boundaryy, 4, new Vector2[] { }, res);

            foreach (KeyValuePair <float, List <Pixel> > l in line)
            {
                foreach (Pixel v in l.Value)
                {
                    quadTree.Insert(v.coords);
                }
            }

            quadTree.Deep(ref BoundarySize);


            foreach (var item in res)
            {
                float lval = line.Keys.First();

                foreach (var k in line.Keys)
                {
                    if (Mathf.Abs(k - item.y) < Mathf.Abs(lval - item.y))
                    {
                        lval = k;
                    }
                }

                List <Pixel> range = line[lval];

                if (range.Count > 1)
                {
                    if (range[1].coords.x > item.x && range[0].coords.x < item.x)
                    {
                        var pktt = gameObject.MakePoint(item.x, item.y, Prefab);
                        ListofPoints.Add(pktt);
                    }
                }
            }
            meshed[i] = true;

            DelaunayTriangulation(ListofPoints);
        }
    }