I have a bunch of coordinates and I draw them as a coordinate system on a bitmap image. While hovering my mouse over my image, I want to highlight the coordinates that are within certain Distance
from my MousePosition
using the below routine:
public void HighLightNearbyDots(Point _mousePosition)
{
// ..
foreach (var point in myDisplayedCoords)
{
Distance = (int)(MousePosition - point); // this gets the distance between two points and converts it to an int
if (Distance < 10)
point.Color = Colors.Red;
else
point.Color = InitialCoordColor;
}
DrawImage();
}
Now, if I find a Nearby coordinates, I want to Change my CursorShape
to be a HAND. I can not do that inside if (Distance < NearbyCursorDistance)
; Why? Because I will either need to change back to Arrow (which takes a micro second) on the else
and user won't see it, or it will stay as HAND for the rest of execution. So I implement this:
private bool IsThereANearbyDot(CoordPoint _mousePosition)
{
foreach(var point in MyDisplayedCoords)
{
if (10 > (int) (_mousePosition - point))
return true;
}
return false;
}
And use it this way:
public void HighLightNearbyDots(Point _mousePosition)
{
//..
if (IsThereANearbyDot(MousePosition))
CursorShape = Cursors.Hand;
else
CursorShape = Cursors.Arrow;
foreach (var point in myDisplayedCoords)
{
Distance = (int)(MousePosition - point);
if (Distance < 10)
point.Color = Colors.Red;
else
point.Color = InitialCoordColor;
}
DrawImage();
}
It works but if MyDisplayedCoords
is huge, which I have to iterate twice now, it takes a lot of time and users will notice lagging on the screen. How could I solve this problem?