I have to get a Circle
from a List<Circle>
depending on the current MousePosition
This is Circle class
public class Circle
{
public Point Center;
public Circle(int x, int y)
{
this.Center = new Point(x, y);
}
public int Distance(int x, int y)
{
int result = 0;
double part1 = Math.Pow((this.Center.X - x), 2);
double part2 = Math.Pow((this.Center.Y - y), 2);
double underRadical = part1 + part2;
result = (int)Math.Sqrt(underRadical);
return result;
}
public void Draw(Graphics g, Pen pen, int d)
{
g.DrawEllipse(pen, this.Center.X - d / 2, this.Center.Y - d/ 2, d, d );
}
}
Here is how i am retrieving the circle from the list
public class UserControl1 : UserControl
{
private Circle currentCircle;
private List<Circle> circles = new List<Circle>();
private const int RADIUS = 16;
// ...snip
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
currentCircle= this.circles
.Where(c => c.Distance(e.X, e.Y) < RADIUS )
.DefaultIfEmpty(null)
.First();
}
//...snip
}
this works fine for small list, but as the list grows this is getting slower. I think i can use List.BinarySearch to get better performance but i could'nt figure how to implement IComparable
in this scenario.
One observation - you can cancel out the Math.Pow
and Math.Sqrt
to save time. The calculated distance becomes 'distance squared' but since all circles undergo the same scaling it doesn't matter.
However, you need a solution where the number of items being searched doesn't have as direct an impact on the amount of time taken to find a match.
So I think you might want to look at a KD Tree, which offers fast performance for large amounts of dimensional data. I have adapted a complete generic KD Tree from source written by Marco A. Alvarez linked to on this page (KDTreeDLL.zip); however there's this one which appears to be better and more generic. Unfortunately I can't supply my code - it's owned by my employer ;)
Once you've got your data in the KD Tree, you simply look for the nearest circle to the current X,Y (which is a single-call into the KD Tree) and then check to see if the pointer is inside that, if that's what you're looking for.
The structure is effectively a binary tree with the left/right values at each level being above/below a 'splitting plane' on successive dimensions, wrapping around until there is no child node. Because of the way the data is stored, a proximity search is fast because when a near neighbour is found, only other neighbours around the same splitting planes can be closer (it's a bit more subtle than that and actually a higher-class of Maths than that of which I am capable). As a result, the algorithm rarely needs to examine all nodes to find the closest match. That said, however, there are scenarios in which all nodes might need to be searched; and this is as much down to the order in which you insert the data as much as anything else.
Thus; very high numbers of circles might require a 'balanced insertion' (there's a good description of this in the 'Construction' sub topic on the Wikipedia entry for KD Trees). The code I've linked to appears to do some form of balanced insertion anyway, assuming you have a list of points to build it with - so it looks like you'll be alright.
It also appears that it's default behaviour in measuring distance is the Euclidean distance, which is exactly what you want.
As a rough idea of performance (which is always a subjective measure) my adapted KD Tree, into which I currently plug the Haversine distance calculation for points on a map, takes about 250-350ms to locate the nearest lat/long out of 250,000. A Euclidean distance calculation is likely to be quite a bit faster.