//this method calculates all a target's neighbors and stores them in //the neighbor vector. After you have called this method use the begin, //next, and end methods to iterate through the vector. public IEnumerable <T> CalculateNeighbors(Vector2D searcherPos, double QueryRadius) { //create the query box that is the bounding box of the target's query //area var topLeft = searcherPos - new Vector2D(QueryRadius, QueryRadius); var bottomRight = searcherPos + new Vector2D(QueryRadius, QueryRadius); var queryBox = new InvertedBox(topLeft, bottomRight); //iterate through each cell and test to see if its bounding box overlaps //with the query box. If it does and it also contains entities then //make further proximity tests. var i = 0; while (i < _cells.Count) { var members = _cells[i].Members.Values.ToList(); //test to see if this cell contains members and if it overlaps the //query box if (_cells[i].BoundingBox.Overlap(queryBox) && members.Any()) { int j = 0; //add any entities found within query radius to the neighbor list while (j < members.Count) { var target = members[j]; if (target != null && (target.Pos - searcherPos).LengthSquared() < QueryRadius * QueryRadius) { yield return(target); } j++; } } i++; } }
public bool Overlap(InvertedBox box) { return(this.Left < box.Right && this.Right > box.Left && this.Top < box.Bottom && this.Bottom > box.Top); }
public Cell(Vector2D topleft, Vector2D botright) { Members = new ConcurrentDictionary <int, T>(); BoundingBox = new InvertedBox(topleft, botright); }