private static void FindMinMaxDistance(Point searcherPoint, SpatialTreeNode spatialTreeNode, out int minDistance, out int maxDistance) { // Find the possible minimum and maximum distances for this node as we only want to accrue sections that provide a minium range if (spatialTreeNode.TopLeft.X <= searcherPoint.X && spatialTreeNode.BottomRight.X >= searcherPoint.X && spatialTreeNode.TopLeft.Y <= searcherPoint.Y && spatialTreeNode.BottomRight.Y >= searcherPoint.Y) { minDistance = 0; int xDist = Math.Max((searcherPoint.X - spatialTreeNode.TopLeft.X), (spatialTreeNode.BottomRight.X - searcherPoint.X)); int yDist = Math.Max((searcherPoint.Y - spatialTreeNode.TopLeft.Y), (spatialTreeNode.BottomRight.Y - searcherPoint.Y)); maxDistance = xDist + yDist; } else if (spatialTreeNode.TopLeft.X <= searcherPoint.X && spatialTreeNode.BottomRight.X >= searcherPoint.X) { minDistance = Math.Min(Math.Abs(searcherPoint.Y - spatialTreeNode.TopLeft.Y), Math.Abs(spatialTreeNode.BottomRight.Y - searcherPoint.Y)); maxDistance = Math.Max(Math.Abs(searcherPoint.Y - spatialTreeNode.TopLeft.Y), Math.Abs(spatialTreeNode.BottomRight.Y - searcherPoint.Y)); } else if (spatialTreeNode.TopLeft.Y <= searcherPoint.Y && spatialTreeNode.BottomRight.Y >= searcherPoint.Y) { minDistance = Math.Min(Math.Abs(searcherPoint.X - spatialTreeNode.TopLeft.X), Math.Abs(spatialTreeNode.BottomRight.X - searcherPoint.X)); maxDistance = Math.Max(Math.Abs(searcherPoint.X - spatialTreeNode.TopLeft.X), Math.Abs(spatialTreeNode.BottomRight.X - searcherPoint.X)); } else { int maxXDist = Math.Max(Math.Abs(searcherPoint.X - spatialTreeNode.TopLeft.X), Math.Abs(spatialTreeNode.BottomRight.X - searcherPoint.X)); int maxYDist = Math.Max(Math.Abs(searcherPoint.Y - spatialTreeNode.TopLeft.Y), Math.Abs(spatialTreeNode.BottomRight.Y - searcherPoint.Y)); maxDistance = maxXDist + maxYDist; int minXDist = Math.Min(Math.Abs(searcherPoint.X - spatialTreeNode.TopLeft.X), Math.Abs(spatialTreeNode.BottomRight.X - searcherPoint.X)); int minYDist = Math.Min(Math.Abs(searcherPoint.Y - spatialTreeNode.TopLeft.Y), Math.Abs(spatialTreeNode.BottomRight.Y - searcherPoint.Y)); minDistance = minXDist + minYDist; } }
public void Construct() { int treeWidth = _spatialTreeIndex.TreeWidth[Level]; int treeHeight = _spatialTreeIndex.TreeHeight[Level]; int subWidth = (int)Math.Ceiling(((BottomRight.X - TopLeft.X) + 1) / (double)treeWidth); int newHeight = (int)Math.Ceiling(((BottomRight.Y - TopLeft.Y) + 1) / (double)treeHeight); if (Level < MaxLevel) { Children = new List <SpatialTreeNode>(); for (int i = 0; i < treeWidth; i++) { for (int j = 0; j < treeHeight; j++) { Point newTopLeft = new Point(TopLeft.X + (subWidth * i), TopLeft.Y + (newHeight * j)); Point newBottomRight = new Point(Math.Min(newTopLeft.X + subWidth - 1, _spatialTreeIndex.WorldSize.X) , Math.Min(newTopLeft.Y + newHeight - 1, _spatialTreeIndex.WorldSize.Y)); SpatialTreeNode addNode = new SpatialTreeNode(newTopLeft, newBottomRight, Level + 1, MaxLevel, this, _spatialTreeIndex); Children.Add(addNode); addNode.Construct(); } } } if (IsLeafNode) { for (int i = TopLeft.X; i <= BottomRight.X; i++) { for (int j = TopLeft.Y; j <= BottomRight.Y; j++) { // Link this particular tile into this leaf node. _spatialTreeIndex.GridToLeafNodes[i, j] = this; } } } }
private void Init(Point topLeft, Point bottomRight, int level, int maxLevel, SpatialTreeNode parent, SpatialTreeIndex spatialTreeIndex) { SpatialIndexMembers = new Dictionary <SpatialObjectKey, List <ISpatialIndexMember> >(); TopLeft = topLeft; BottomRight = bottomRight; Level = level; MaxLevel = maxLevel; _parent = parent; _spatialTreeIndex = spatialTreeIndex; }
public void CheckChangeSection(Point newPosition, ISpatialIndexMember objectToMove, SpatialObjectKey spatialObjectType) { SpatialTreeNode leafNodeToRemoveFrom = PositionToLeafNode(objectToMove.SpatialIndexPosition); SpatialTreeNode leafNodeToAdd = PositionToLeafNode(newPosition); if (leafNodeToRemoveFrom != leafNodeToAdd) { RemoveFromSection(objectToMove, spatialObjectType); leafNodeToAdd.AddSpatialMemeber(spatialObjectType, objectToMove); } }
public void Initialise() { _topNode = new SpatialTreeNode(new Point(0, 0), new Point(WorldSize.X - 1, WorldSize.Y - 1), 0, TreeWidth.Length - 1, this); GridToLeafNodes = new SpatialTreeNode[WorldSize.X, WorldSize.Y]; _topNode.Construct(); }
public void RemoveFromSection(ISpatialIndexMember objectToRemove, SpatialObjectKey spatialObjectType) { SpatialTreeNode leafNodeToRemoveFrom = PositionToLeafNode(objectToRemove.SpatialIndexPosition); leafNodeToRemoveFrom.RemoveSpatialMemeber(spatialObjectType, objectToRemove); }
public void AddToSection(ISpatialIndexMember objectToAdd, SpatialObjectKey spatialObjectType) { SpatialTreeNode leafNodeToAdd = PositionToLeafNode(objectToAdd.SpatialIndexPosition); leafNodeToAdd.AddSpatialMemeber(spatialObjectType, objectToAdd); }
private SpatialTreeNode(Point topLeft, Point bottomRight, int level, int maxLevel, SpatialTreeNode parent, SpatialTreeIndex spatialTreeIndex) { Init(topLeft, bottomRight, level, maxLevel, parent, spatialTreeIndex); }