Example #1
0
    public SilentQuadTree(Boundary2 _boundary, int _capacity, IEnumerable <Vector2> PointsInParent, List <Vector2> Result, int deph = 0)
    {
        Center      = new Vector2(_boundary.Center.x + _boundary.Height / 2f, _boundary.Center.y + _boundary.Width / 2f);
        this.Result = Result;
        this.deph   = deph;

        boundary = _boundary;
        capacity = _capacity;
        Point2Ds = new List <Vector2>();
        divided  = false;

        foreach (var item in PointsInParent)
        {
            if (boundary.Contains(item))
            {
                Point2Ds.Add(item);
            }
        }
    }
Example #2
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);
        }
    }