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.