Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

Binary Search works only on a sorted list. Therefore your Circle objects need to be comparable in order to get a sorted list. I am afraid, though, that you cannot put them in a reasonable linear order to find "collisions" with the mouse cursor position faster.

You could use AsParallel in your LINQ query to use all your CPU cores for the comparision. This is still O(n) and therefore does not scale too well, but it will perhaps speed things a bit up (given a barely ever growing number of Circle objects).

I would move the comparison to the circle so you can do some short cuts. And use some faster math. I assume the circles do not overlap. Worst case (at most) 4 boxes would include the point so the complex equation is evaluated at most 4 times.

public bool Distance(double x, double y, double radius)
{
    double deltaX = this.Center.X - x;
    if (deltaX > radius) return false;   // test to see if this makes it faster
    double deltaY = this.Center.Y - y;
    if (deltaY > radius) return false;
    if ( (deltaX + deltaY) > radius) return false;   
    return ( (deltaX*deltaX + deltaY*deltaY) <= radius*radius) );
}

Combine with FirstOrDefault and Parallel as stated in other comments and answers.

How about just registering an event handler for mouse enter? Tested this and no lag at 112 X 116.

    public MainWindow()
    {

        InitializeComponent();

        for (int j = 0; j < 112; j++)
        {
            for (int i = 0; i < 116; i++)
            {
                ellipse = new Ellipse();
                ellipse.Name = "EllipseX" + i.ToString() + "Y" + j.ToString();
                ellipse.StrokeThickness = 1;
                ellipse.Stroke = System.Windows.Media.Brushes.Blue;
                ellipse.Width = 5;
                ellipse.Height = 5;
                ellipse.MouseEnter += ellipse_MouseEnter;
                ellipse.MouseLeave += ellipse_MouseLeave;

                Canvas.SetTop(ellipse, 5 * j);
                Canvas.SetLeft(ellipse, 5 * i);
                Canvas1.Children.Add(ellipse);
            }
        }
    }

    private void ellipse_MouseEnter(object sender, MouseEventArgs e)
    {
        Ellipse ellipse = (Ellipse)sender;
        ellipse.Fill = System.Windows.Media.Brushes.Red;
        tbEname.Text = ellipse.Name;
    }

    private void ellipse_MouseLeave(object sender, MouseEventArgs e)
    {
        ((Ellipse)sender).Fill = System.Windows.Media.Brushes.Blue;
        tbEname.Text = string.Empty;
    }

Try using HashSet<Circle> instead of List<Circle>. It seams to have a better performance with growing data sizes - HashSet vs. List performance

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top