Example #1
0
    static private List <QuadTree2> returnAllQuadsForNeighbor(QuadTree2 neighbor, QuadTree2 currentQuad, int axis)
    {
        List <QuadTree2> neighborQuads = new List <QuadTree2>();
        int       levelDifference = neighbor.level - currentQuad.level;
        int       numberOfQuads = (int)Mathf.Pow(2.0f, (float)levelDifference);
        float     min, max, offset;
        float     a = 0.0f, b = 0.0f, c = 0.0f, d = 0.0f;
        QuadTree2 nextNeighbor;

        if (axis == 1)
        {
            min    = currentQuad.minz;
            max    = currentQuad.maxz;
            offset = (max - min) / numberOfQuads;
        }
        else
        {
            min    = currentQuad.minx;
            max    = currentQuad.maxx;
            offset = (max - min) / numberOfQuads;
        }

        for (int index = 0; index < numberOfQuads; ++index)
        {
            float testPoint = min + (index * offset + 0.5f);
            if (axis == 0)
            {
                nextNeighbor = refTree.Find(testPoint, neighbor.minz + 0.5f, ref a, ref b, ref c, ref d);
            }
            else
            {
                nextNeighbor = refTree.Find(neighbor.minx + 0.5f, testPoint, ref a, ref b, ref c, ref d);
            }
            neighborQuads.Add(nextNeighbor);
        }
        return(neighborQuads);
    }
Example #2
0
    // To find the quad-tree grid which encloses the point(xx, zz)
    public QuadTree2 Find(float xx, float zz, ref float tminx, ref float tmaxx, ref float tminz, ref float tmaxz)
    {
        if (xx >= minx && xx < maxx && zz >= minz && zz < maxz && a == null)
        {
            tminx = minx;
            tmaxx = maxx;
            tminz = minz;
            tmaxz = maxz;
            return(this);
        }
        else if (a != null)
        {
            QuadTree2 retval = null;

            retval = a.Find(xx, zz, ref tminx, ref tmaxx, ref tminz, ref tmaxz);
            if (retval != null)
            {
                return(retval);
            }

            retval = b.Find(xx, zz, ref tminx, ref tmaxx, ref tminz, ref tmaxz);
            if (retval != null)
            {
                return(retval);
            }

            retval = c.Find(xx, zz, ref tminx, ref tmaxx, ref tminz, ref tmaxz);
            if (retval != null)
            {
                return(retval);
            }

            retval = d.Find(xx, zz, ref tminx, ref tmaxx, ref tminz, ref tmaxz);
            if (retval != null)
            {
                return(retval);
            }
        }

        return(null);
    }