Esempio n. 1
0
        public Point FindNearestPoint(Point point)
        {
            if (NW != null && NE != null && SW != null && SE != null)
            {
                if (
                    point.x >= NW.x1 &&
                    point.x <= NW.x2 &&
                    point.y >= NW.y1 &&
                    point.y <= NW.y2
                    )
                {
                    return(NW.FindNearestPoint(point));
                }
                else if (
                    point.x >= NE.x1 &&
                    point.x <= NE.x2 &&
                    point.y >= NE.y1 &&
                    point.y <= NE.y2
                    )
                {
                    return(NE.FindNearestPoint(point));
                }
                else if (
                    point.x >= SW.x1 &&
                    point.x <= SW.x2 &&
                    point.y >= SW.y1 &&
                    point.y <= SW.y2
                    )
                {
                    return(SW.FindNearestPoint(point));
                }
                else if (
                    point.x >= SE.x1 &&
                    point.x <= SE.x2 &&
                    point.y >= SE.y1 &&
                    point.y <= SE.y2
                    )
                {
                    return(SE.FindNearestPoint(point));
                }
                else
                {
                    Console.WriteLine("this shouldn't happen (returning the point you gave me");
                    return(point);
                }
            }
            else//We have now reached a leaf
            {
                Console.WriteLine("We are now in a leaf!!");
                Point[] toCompare   = new Point[10];
                int     index       = 0;
                int     lowestIndex = -1;
                double  lowestDist  = 1000;
                Quad    above;
                Quad    left;
                Quad    below;
                Quad    right;
                if (this.type == "nw")
                {
                    Console.WriteLine("The point was in a NW quad");
                    above = this.GetUp(this);
                    left  = this.GetLeft(this);
                    if (above != null)
                    {
                        for (int i = 0; i < above.point.Length; i++)
                        {
                            toCompare[index] = above.point[i];
                            index++;
                        }
                    }

                    for (int i = 0; i < parent.point.Length; i++)
                    {
                        toCompare[index] = parent.point[i];
                        index++;
                    }
                    if (left != null)
                    {
                        for (int i = 0; i < left.point.Length; i++)
                        {
                            toCompare[index] = left.point[i];
                            index++;
                        }
                    }
                }
                else if (this.type == "ne")
                {
                    Console.WriteLine("The point was in a NE quad");
                    above = this.GetUp(this);
                    right = this.GetRight(this);

                    if (above != null)
                    {
                        for (int i = 0; i < above.point.Length; i++)
                        {
                            toCompare[index] = above.point[i];
                            index++;
                        }
                    }

                    for (int i = 0; i < parent.point.Length; i++)
                    {
                        toCompare[index] = parent.point[i];
                        index++;
                    }
                    if (right != null)
                    {
                        for (int i = 0; i < right.point.Length; i++)
                        {
                            toCompare[index] = right.point[i];
                            index++;
                        }
                    }
                }
                else if (this.type == "sw")
                {
                    Console.WriteLine("The point was in a SW quad");
                    below = this.GetDown(this);
                    left  = this.GetLeft(this);


                    if (below != null)
                    {
                        for (int i = 0; i < below.point.Length; i++)
                        {
                            toCompare[index] = below.point[i];
                            index++;
                        }
                    }

                    for (int i = 0; i < parent.point.Length; i++)
                    {
                        toCompare[index] = parent.point[i];
                        index++;
                    }

                    if (left != null)
                    {
                        for (int i = 0; i < left.point.Length; i++)
                        {
                            toCompare[index] = left.point[i];
                            index++;
                        }
                    }
                }
                else if (this.type == "se")
                {
                    Console.WriteLine("The point was in a SE quad");
                    below = this.GetDown(this);
                    right = this.GetRight(this);

                    if (below != null)
                    {
                        for (int i = 0; i < below.point.Length; i++)
                        {
                            toCompare[index] = below.point[i];
                            index++;
                        }
                    }

                    for (int i = 0; i < parent.point.Length; i++)
                    {
                        toCompare[index] = parent.point[i];
                        index++;
                    }

                    if (right != null)
                    {
                        for (int i = 0; i < right.point.Length; i++)
                        {
                            toCompare[index] = right.point[i];
                            index++;
                        }
                    }
                }
                else
                {
                    Console.WriteLine("You shouldn't be here");
                    return(point);
                }
                for (int i = 0; i < index; i++)
                {
                    int    tomultiplyx = point.x - toCompare[i].x;
                    int    tomultiplyy = point.y - toCompare[i].y;
                    double newDist     = Math.Sqrt((tomultiplyx * tomultiplyx) + (tomultiplyy * tomultiplyy));
                    if (newDist < lowestDist)
                    {
                        lowestDist  = newDist;
                        lowestIndex = i;
                    }
                }
                return(toCompare[lowestIndex]);;
            }
        }