// Select the clicked point. private void pointsPictureBox_MouseClick(object sender, MouseEventArgs e) { // Find the point closest to the selected point. PointIsSelected = Root.FindPoint(e.Location, Radius, out SelectedPoint); // Redraw. pointsPictureBox.Refresh(); }
// Search the children to find the point closest to the target. private bool FindPointInChildren(PointF target, float radius, out PointF resultPoint) { // The best point we have found so far. resultPoint = new PointF(float.MinValue, float.MinValue); float bestDist = float.MaxValue; // See if the northern subtrees intersect the area of interest. if (target.Y - radius < Ymid) { // See if the northwest subtree intersects the area of interest. if (target.X - radius < Xmid) { // The northwest subtree does intersect the area of interest. PointF testPoint; if (NWchild.FindPoint(target, radius, out testPoint)) { float dx = testPoint.X - target.X; float dy = testPoint.Y - target.Y; float testDist = (float)Math.Sqrt(dx * dx + dy * dy); if ((testDist < radius) && (testDist < bestDist)) { bestDist = testDist; resultPoint = testPoint; } } } // See if the northeast subtree intersects the area of interest. if (target.X + radius > Xmid) { // The northeast subtree does intersect the area of interest. PointF testPoint; if (NEchild.FindPoint(target, radius, out testPoint)) { float dx = testPoint.X - target.X; float dy = testPoint.Y - target.Y; float testDist = (float)Math.Sqrt(dx * dx + dy * dy); if ((testDist < radius) && (testDist < bestDist)) { bestDist = testDist; resultPoint = testPoint; } } } } // End if the northern subtrees intersect the area of interest. // See if the southern subtrees intersect the area of interest. if (target.Y + radius > Ymid) { // See if the southwest subtree intersects the area of interest. if (target.X - radius < Xmid) { // The southwest subtree does intersect the area of interest. PointF testPoint; if (SWchild.FindPoint(target, radius, out testPoint)) { float dx = testPoint.X - target.X; float dy = testPoint.Y - target.Y; float testDist = (float)Math.Sqrt(dx * dx + dy * dy); if ((testDist < radius) && (testDist < bestDist)) { bestDist = testDist; resultPoint = testPoint; } } } // See if the southeast subtree intersects the area of interest. if (target.X + radius > Xmid) { // The southeast subtree does intersect the area of interest. PointF testPoint; if (SEchild.FindPoint(target, radius, out testPoint)) { float dx = testPoint.X - target.X; float dy = testPoint.Y - target.Y; float testDist = (float)Math.Sqrt(dx * dx + dy * dy); if ((testDist < radius) && (testDist < bestDist)) { bestDist = testDist; resultPoint = testPoint; } } } } // End if the southern subtrees intersect the area of interest. // Return true if we found a point. return(bestDist < float.MaxValue); }