public void AddAsChild(Rectangle rect) { bool rightSide = false; switch (Dimension) { case IntersectRectDimension.Top: rightSide = rect.Top > Rect.Top; break; case IntersectRectDimension.Left: rightSide = rect.Left > Rect.Left; break; case IntersectRectDimension.Bottom: rightSide = rect.Bottom > Rect.Bottom; break; case IntersectRectDimension.Right: rightSide = rect.Right > Rect.Right; break; } if (rightSide) { if (RightChild != null) { RightChild.AddAsChild(rect); } else { RightChild = new IntersectRectNode { Dimension = (IntersectRectDimension)(((int)Dimension + 1) % 4), Rect = rect }; } } else { if (LeftChild != null) { LeftChild.AddAsChild(rect); } else { LeftChild = new IntersectRectNode { Dimension = (IntersectRectDimension)(((int)Dimension + 1) % 4), Rect = rect }; } } }
public void Add(Rectangle rect) { if (Root == null) { Root = new IntersectRectNode { Dimension = IntersectRectDimension.Left, Rect = rect }; } else { Root.AddAsChild(rect); } }