Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.72 KB | None | 0 0
  1.     public RiverPoint GetClosestRiverPointTo(Vector2 aPosition)
  2.     {
  3.         RiverPointQuadTree smallestQuadtree = GetSmallestQuadtreeAt(aPosition);
  4.         RiverPointQuadTree currentlyExplored = smallestQuadtree;
  5.  
  6.         RiverPoint closestPoint = null;
  7.  
  8.         smallestQuadtree.ExploreNode(aPosition, ref closestPoint); //initial condition : search for closest point in smallest quadtree
  9.  
  10.         while (currentlyExplored.myParent != null)  //while current != root
  11.         {
  12.             currentlyExplored = currentlyExplored.myParent;
  13.             currentlyExplored.ExploreNode(aPosition, ref closestPoint);
  14.         }
  15.  
  16.         return closestPoint;
  17.     }
  18.  
  19.     protected void ExploreNode(Vector2 aPoint, ref RiverPoint aClosestPoint)
  20.     {
  21.         if (myHasChildren)
  22.         {
  23.             for (int i = 0; i < 2; ++i)
  24.                 for (int j = 0; j < 2; ++j)
  25.                 {
  26.                     float distanceToChild = myChildren[i, j].GetSquaredDistanceOfPointToNode(aPoint);
  27.                     if (distanceToChild >= 0f)   //Point not inside of that node (should have been explored already)
  28.                     {
  29.                         if (aClosestPoint == null)
  30.                         {
  31.                             myChildren[i, j].ExploreNode(aPoint, ref aClosestPoint);
  32.                         }
  33.                         else
  34.                         {
  35.                             if (distanceToChild < (aPoint - aClosestPoint.myPosition).sqrMagnitude) //Only explore nodes that could have closer points
  36.                             {
  37.                                 myChildren[i, j].ExploreNode(aPoint, ref aClosestPoint);
  38.                             }
  39.                         }
  40.                     }
  41.                 }
  42.         }
  43.         else
  44.         {
  45.             for (int i = 0; i < myPoints.Count; ++i)
  46.             {
  47.                 if (aClosestPoint == null)
  48.                     aClosestPoint = myPoints[i];
  49.                 else
  50.                 {
  51.                     if ((aPoint - myPoints[i].myPosition).sqrMagnitude < (aPoint - aClosestPoint.myPosition).sqrMagnitude)
  52.                         aClosestPoint = myPoints[i];
  53.                 }
  54.             }
  55.         }
  56.     }
  57.  
  58.     protected float GetSquaredDistanceOfPointToNode(Vector2 aPoint)
  59.     {
  60.         if (IsPointInQuadtree(aPoint))
  61.             return -1f;
  62.         else
  63.         {
  64.             float cx = Mathf.Clamp(aPoint.x, myTopLeft.x, myBottomRight.x);// Mathf.Max(Mathf.Min(aPoint.x, myBottomRight.x), myTopLeft.x);
  65.             float cy = Mathf.Clamp(aPoint.y, myTopLeft.y, myBottomRight.y); //Mathf.Max(Mathf.Min(aPoint.y, aBottomRight.y), ry);
  66.             return (aPoint.x - cx) * (aPoint.x - cx) + (aPoint.y - cy) * (aPoint.y - cy);
  67.         }
  68.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement