Re: Drawing graphics and responding to events

John Brett <[email protected]>
Newsgroups gmane.comp.windows.devel.dotnet.winforms
Message-ID <[email protected]>
This may be an obvious comment, but it is worth caching the last
selected Circle object and optimizing the very common case in which
the mouse has moved but is still within the same circle.

You don't state any of the constraints on the locations of your
circles - are they
static locations or can they move at run-time? Do the circles ever overlap their
bounding rectangles, or overlap each other?

If the locations of the circles is static, then you should be able to
speed up the searching process by designating bounding rectangles
around discrete sets of circles - e.g if the circles are approximately
on a grid, then put 20 bounding rectangles around rows of 20 circles.
You can then search the outer bounding rectangles to find the row to
search, and then search only that. This would cut down the number of
comparisons from ~(20*20)/2 to ~(20+20)/2.

If your circles don't ever overlap their bounding rectangles, then
testing the point for inclusion within the rectangle is going to be
the easiest option.

If the bounding rectangles can overlap, then you can determine whether
a point is within a circle by calculating the distance between the
point and the centre, and comparing it with the radius.

<eMail-code>
PointF circlePos;
PointF mousePos;

double distX= (circlePos.X - mousePos.X);
double distY= (circlePos.Y - mousePos.Y);
double distSquared= distX*distX + distY*distY;

if (distSquared < radiusSquared)
{
   // point within circle.
}
</eMail-code>

If the circles can overlap then you need to manage some sort of
Z-order & arbitration if the mouse is over two or more circles.

Using a GraphicsPath gives aesthetically pleasing code, but I doubt
it's going to be as quick as comparing co-ordinates (given that it has
to deal with arbitrarily complex shapes, and you're working with about
the easiest shape to optimise).
BTW - what are your efficiency constraints? Given that this is appears
to be a UI issue, then I'd suggest that the only criteria that matters
is whether the user perceives any flickering or delay whilst moving
the mouse.

John
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.