public void Divide(Quadtree newTree) { Quadtree support = null; Quadtree current = this; do { current.Type = Type.NODE; current.Children = GenerateChildrenQuadrants(current); var quad1 = MapPointToQuadrant(current, Point); var quad2 = MapPointToQuadrant(current, newTree.Point); if (quad1.Equals(quad2)) { current = current.Children[quad1.y, quad1.x]; } else { support = current.Children[quad2.y, quad2.x]; current = current.Children[quad1.y, quad1.x]; break; } } while (true); #if DEBUG if (Point.X < current.Region.X || Point.X > current.Region.X + current.Region.Width || Point.Y < current.Region.Y || Point.Y > current.Region.Y + current.Region.Width) { throw new ArgumentException("this throws"); } #endif current.Type = Type.PARTICLE; current.Point = Point; #if DEBUG if (newTree.Point.X < support.Region.X || newTree.Point.X > support.Region.X + support.Region.Width || newTree.Point.Y < support.Region.Y || newTree.Point.Y > support.Region.Y + support.Region.Width) { throw new ArgumentException("newTree throws"); } #endif support.Type = Type.PARTICLE; support.Point = newTree.Point; }
private static (int x, int y) MapPointToQuadrant(Quadtree tree, Point p) { return((int)((p.X % tree.Region.Width) / (tree.Region.Width / 2)), (int)((p.Y % tree.Region.Width) / (tree.Region.Width / 2))); }