Beispiel #1
0
 public override float GetDistance(KDShape another)
 {
     if (another.MaxDimensions < MaxDimensions)
     {
         float _x = x - another.GetPosition(0);
         return(_x * _x);
     }
     else
     {
         return(new Vector2(x - another.GetPosition(0), y - another.GetPosition(1)).sqrMagnitude);
     }
 }
Beispiel #2
0
 public override int CompareTo(KDShape other, int dimension)
 {
     if (dimension == 0)
     {
         return((int)Mathf.Sign(x - other.GetPosition(dimension)));
     }
     else if (dimension == 1)
     {
         return((int)Mathf.Sign(y - other.GetPosition(dimension)));
     }
     else
     {
         throw new Exception("There is not more dimensions to get");
     }
 }
Beispiel #3
0
    private void GetClosestNeighborsInRange(List <KDShape> closeShapes, KDShape shape, float sqrdistance)
    {
        if (!IsLeaf())
        {
            float currentPos = shape.GetPosition(dimensionalDepth);
            bool  smaller    = currentPos <= median;

            (smaller ? smallerBranch : biggerBranch).GetClosestNeighborsInRange(closeShapes, shape, sqrdistance);

            if (shape.DistanceToWall(median, dimensionalDepth) >= sqrdistance)
            {
                return;
            }

            (smaller ? biggerBranch : smallerBranch).GetClosestNeighborsInRange(closeShapes, shape, sqrdistance);
        }
        else
        {
            for (int i = 0; i < shapes.Count; i++)
            {
                if (shapes[i] != shape)
                {
                    float dis = shape.GetDistance(shapes[i]);
                    if (dis < sqrdistance)
                    {
                        closeShapes.Add(shapes[i]);
                    }
                }
            }
        }
    }
Beispiel #4
0
    /// <summary>
    /// Return the closest shape to "shape".
    /// </summary>
    /// <param name="shape">the shape.</param>
    /// <returns></returns>
    public KDShape GetClosestNeighbor(KDShape shape)
    {
        KDShape closeShape    = null;
        float   closeDistance = float.NaN;

        if (!IsLeaf())
        {
            float currentPos = shape.GetPosition(dimensionalDepth);
            bool  smaller    = currentPos <= median;

            closeShape = (smaller ? smallerBranch : biggerBranch).GetClosestNeighbor(shape, ref closeDistance);

            if (closeShape != null)
            {
                if (shape.DistanceToWall(median, dimensionalDepth) >= closeDistance)
                {
                    return(closeShape);
                }
            }

            KDShape otherClose = (smaller ? biggerBranch : smallerBranch).GetClosestNeighbor(shape, ref closeDistance);

            if (otherClose != null)
            {
                closeShape = otherClose;
            }
        }
        else
        {
            for (int i = 0; i < shapes.Count; i++)
            {
                if (shapes[i] != shape)
                {
                    if (float.IsNaN(closeDistance))
                    {
                        closeDistance = shape.GetDistance(shapes[i]);
                        closeShape    = shapes[i];
                    }
                    else
                    {
                        float dis = shape.GetDistance(shapes[i]);
                        if (dis < closeDistance)
                        {
                            closeDistance = dis;
                            closeShape    = shapes[i];
                        }
                    }
                }
            }
        }

        return(closeShape);
    }
Beispiel #5
0
 /// <summary>
 /// Adds a shape to the list of shapes.
 /// </summary>
 /// <param name="shape">the shape. NOTE its position must be uniqe were there arent 2 points or more ar in the same position exactly.</param>
 public void AddShape(KDShape shape)
 {
     if (!IsLeaf())
     {
         (shape.GetPosition(dimensionalDepth) <= median ? smallerBranch : biggerBranch).AddShape(shape);
     }
     else
     {
         if (!shapes.Any(x => x.IsEqualPosition(shape)))
         {
             shapes.Add(shape);
             if (shapes.Count > limit)
             {
                 Split();
             }
         }
     }
 }
Beispiel #6
0
 public override bool IsEqualPosition(KDShape another)
 {
     return(x == another.GetPosition(0) && y == another.GetPosition(1));
 }
Beispiel #7
0
 public override int CompareTo(KDShape another, int dimension)
 {
     return((int)Mathf.Sign(GetPosition(dimension) - another.GetPosition(dimension)));
 }