Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8524303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:39:02+00:00 2026-06-11T07:39:02+00:00

I have to get a Circle from a List<Circle> depending on the current MousePosition

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T07:39:03+00:00Added an answer on June 11, 2026 at 7:39 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

maybe you can help me get this right. I have a class that is
Can I Have circle-shape buttonset (use in radio button) from jqueryUI? as I get
I have an abstract base class called Shape from which both Circle and Rectangle
So I have a class that has the following member variables. I have get
I have to get a date from a string. So I am using the
I have to get records from my MySQL DB where: sentto = $username OR
I have to get data from Oracle Table in which I have one datefield
i have to get static information from one 'module' to another. I'm trying to
So I have a circle of planes which get constructed as: plane.x = Math.cos(angle)
I have managed to make a surface from a set point points in vtk.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.