Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public RiverPoint GetClosestRiverPointTo(Vector2 aPosition)
- {
- RiverPointQuadTree smallestQuadtree = GetSmallestQuadtreeAt(aPosition);
- RiverPointQuadTree currentlyExplored = smallestQuadtree;
- RiverPoint closestPoint = null;
- smallestQuadtree.ExploreNode(aPosition, ref closestPoint); //initial condition : search for closest point in smallest quadtree
- while (currentlyExplored.myParent != null) //while current != root
- {
- currentlyExplored = currentlyExplored.myParent;
- currentlyExplored.ExploreNode(aPosition, ref closestPoint);
- }
- return closestPoint;
- }
- protected void ExploreNode(Vector2 aPoint, ref RiverPoint aClosestPoint)
- {
- if (myHasChildren)
- {
- for (int i = 0; i < 2; ++i)
- for (int j = 0; j < 2; ++j)
- {
- float distanceToChild = myChildren[i, j].GetSquaredDistanceOfPointToNode(aPoint);
- if (distanceToChild >= 0f) //Point not inside of that node (should have been explored already)
- {
- if (aClosestPoint == null)
- {
- myChildren[i, j].ExploreNode(aPoint, ref aClosestPoint);
- }
- else
- {
- if (distanceToChild < (aPoint - aClosestPoint.myPosition).sqrMagnitude) //Only explore nodes that could have closer points
- {
- myChildren[i, j].ExploreNode(aPoint, ref aClosestPoint);
- }
- }
- }
- }
- }
- else
- {
- for (int i = 0; i < myPoints.Count; ++i)
- {
- if (aClosestPoint == null)
- aClosestPoint = myPoints[i];
- else
- {
- if ((aPoint - myPoints[i].myPosition).sqrMagnitude < (aPoint - aClosestPoint.myPosition).sqrMagnitude)
- aClosestPoint = myPoints[i];
- }
- }
- }
- }
- protected float GetSquaredDistanceOfPointToNode(Vector2 aPoint)
- {
- if (IsPointInQuadtree(aPoint))
- return -1f;
- else
- {
- float cx = Mathf.Clamp(aPoint.x, myTopLeft.x, myBottomRight.x);// Mathf.Max(Mathf.Min(aPoint.x, myBottomRight.x), myTopLeft.x);
- float cy = Mathf.Clamp(aPoint.y, myTopLeft.y, myBottomRight.y); //Mathf.Max(Mathf.Min(aPoint.y, aBottomRight.y), ry);
- return (aPoint.x - cx) * (aPoint.x - cx) + (aPoint.y - cy) * (aPoint.y - cy);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement