1

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.

Naveed Quadri
  • 498
  • 5
  • 18
  • Your circles don't have a radius property? – Tudor Sep 10 '12 at 11:53
  • Oops Sorry, thats just a constant in my usercontrol – Naveed Quadri Sep 10 '12 at 11:54
  • 1
    This is no solution, but you should compare square distance against square radius, you would avoid sqrt call for each circle (which may not be negligable). You should also use FirstOrDefault(c => c.Distance...) instead of where(..).First(). In this case, you stop when you find one instead of searching them all – Kek Sep 10 '12 at 11:59
  • I think you could change the label of your question into something like "Implementing IComparable for dynamic comparison" because custom class is a minor aspect of your problem. What is important is the comparison depends on a dynamic parameter – Kek Sep 10 '12 at 12:04
  • @Kek Using just `First(predicate)` instead of `Where(predicate).First()` might be slightly faster, but not significantly. The result of `Where()` is lazy and so `Where().First()` won't search the whole collection if it finds match early. – svick Sep 10 '12 at 13:43
  • All right... my apologies, and thx for the correction – Kek Sep 10 '12 at 14:48

5 Answers5

3

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.

Community
  • 1
  • 1
Andras Zoltan
  • 41,961
  • 13
  • 104
  • 160
  • looks promising. and is this the codeproject article you are refereing to http://www.codeproject.com/Articles/18113/KD-Tree-Searching-in-N-dimensions-Part-I – Naveed Quadri Sep 10 '12 at 13:03
  • Hi there - actually no - that's C++. In fact, I *thought* it was from Code Project but it turns out I got it from [here](http://home.wlu.edu/~levys/software/kd/) where it appears as a link (find 'KDTreeDLL.zip' on the page). – Andras Zoltan Sep 10 '12 at 13:13
  • i knew it was c++, i thought for the explanation. – Naveed Quadri Sep 10 '12 at 13:18
  • Yep - the explanation is very good on that article; it's entirely possible I did read it! I spent a very long time working on KD Trees, trying to understand how they worked so I could adapt the code and then support it afterwards! – Andras Zoltan Sep 10 '12 at 13:24
  • There are also other tree structures for 2D data, like [R-tree](http://en.wikipedia.org/wiki/R-tree) or [Quadtree](http://en.wikipedia.org/wiki/Quadtree) that might be worth looking into. – svick Sep 10 '12 at 13:46
  • arggh andras, too bad i can only give one upvote... working like charm. The library u linked was spot on. i spent more time in reading kd tree. implementing was very easy. – Naveed Quadri Sep 10 '12 at 15:37
  • Benchmarks: Number of circles: 80253 Samples: 2203 Avg Time (ms) Existing method: 7.4350 Max(17) Avg Time (ms) KD Tree: 0.00953 Max(7) – Naveed Quadri Sep 10 '12 at 15:39
1

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).

Matthias Meid
  • 12,455
  • 7
  • 45
  • 79
  • atually the number of circles is decided on the application startup. for about upto 10000 circles the performance is accepted but after that "its not following the mouse pointer". Its highlighting the circle only if i rest my pointer on it. – Naveed Quadri Sep 10 '12 at 12:41
  • 1
    I suggest you to search for a different data structure. I don't think a one-dimensional collection will reduce the effort. But `O(n)` appears to be too slow in your scenario - even if you manage to reduce the run-time of `DistanceTo`. – Matthias Meid Sep 10 '12 at 12:57
0

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.

paparazzo
  • 44,497
  • 23
  • 105
  • 176
0

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;
    }
paparazzo
  • 44,497
  • 23
  • 105
  • 176
  • Hi, thanks for the reply. In my case the number of circles can be upto 500 X 500. As suggested by @andras the KD Tree is working perfectly. Also this code looks like WPF, i am on winforms – Naveed Quadri Sep 10 '12 at 21:45
  • You should tag as winforms. And this scales. – paparazzo Sep 11 '12 at 12:29
-1

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

Community
  • 1
  • 1
Marty
  • 3,485
  • 8
  • 38
  • 69
  • yeah already tried that, the performance, atleast in my case seems to be almost the same – Naveed Quadri Sep 10 '12 at 11:53
  • `HashSet` is not a magical data structure that better performance than `List`. It is useful for representing sets, where you do set operations (union, set difference, contains, etc.). But it won't help any in this case. – svick Sep 10 '12 at 13:41