public bool Add(IRectangle rectangle) { Node before = null, after = this.Root; int top = rectangle.getTop(); while (after != null) { before = after; if (top < after.Data) //Is new node in left tree? { after = after.LeftNode; } else if (top > after.Data) //Is new node in right tree? { after = after.RightNode; } else { //Exist same value return(false); } } Node newNode = new Node(); newNode.Data = top; newNode.Tag = rectangle; if (this.Root == null) // Is tree is empty { this.Root = newNode; } else { if (top < before.Data) { before.LeftNode = newNode; } else { before.RightNode = newNode; } } return(true); }
private void Form1_Click(object sender, EventArgs e) { MouseEventArgs e2 = (MouseEventArgs)e; IRectangle closest = store.findRectangleAt(e2.X, e2.Y); MessageBox.Show(string.Format("The closest IRectangle locate in X: {0} Y: {1}", closest.getLeft(), closest.getTop())); }