How to get the position of a Coordinate with respect to Mouse position?

孤人 提交于 2019-12-11 14:28:09

问题


I am on Wpf and I have a list of coordinates where I draw them on a Bitmap Image. My Bitmap file is 1000 * 1000 and it gets filled in a 680 * 440 Image control. Now what I am trying to accomplish is to highlight the coordinates that are near the mouse cursor, when mouse is hoovering my Image.

on MouseMove() event handler, I call this function and pass to it my mouse position with respect to the Image control:

public void HighLightNearbyDots(Point MousePosition)
{
    int Distance;
    CoordPoint temp = new CoordPoint();
    temp.X = MousePosition.X;
    temp.Y = MousePosition.Y;

    foreach (var point in myDisplayedCoords)
    {
        Distance = (int)(temp - point); // using subtraction operator that I wrote

        if (Distance < 10)
        {
            point.Color = Colors.Blue;
        }
        else
        {
            point.Color = InitialCoordColor; // Aqua
        }
    }

    DrawImage();
}

Yes I redraw my image on every call to reflect the changes. Maybe the issue is that I need to scale or calculate some ratio between the 1000 * 1000 file size and the 680 * 440 control size to hit the exact pixel.. But I am not sure what is the issue. Below is the current result which is killing me since the morning. Could any one help me approach that?


回答1:


Based on this How to scale a coordinate system? Now we know the equation. Then I use it this way:

int Distance;
CoordPoint temp = new CoordPoint();
temp.X = MousePosition.X / 660 * Bitmap.Width;
temp.Y = Bitmap.Height - (MousePosition.Y / 440 * Bitmap.Height); // y is flipped



来源:https://stackoverflow.com/questions/38400410/how-to-get-the-position-of-a-coordinate-with-respect-to-mouse-position

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!