简体   繁体   中英

Calculate distance between two mouse points

I need to calculate the distance between two places the mouse was clicked on the screen.

target(x & Y) & source(X & Y) are populated on the mouse move event (eX & eY)

I have distance = Math.Sqrt(Math.Pow(targetX - sourceX, 2) + Math.Pow(targetY - sourceY, 2));

This gives me a result but if I'm honest I am not sure what unit the measurement is in or how to convert it. How can I convert that result in a meaningful result such as cm's or inches? I'm guessing I will need to take the screen res into account?

Update Im just killing time really. Not looking for an excellent solution just something that works. It will only last a day or two.

Here is the MoveMove event and the call made. Should have posted it all before to be clearer.

    private void HookManager_MouseMove(object sender, MouseEventArgs e)
    {

        labelMousePosition.Text = string.Format("x={0:0000}; y={1:0000}", e.X, e.Y);
        AddDistance(Convert.ToDouble(e.X), Convert.ToDouble(e.Y));
    }

    private void AddDistance(double targetX, double targetY)
    {
        if (sourceX != 0 && sourceY != 0)
        {
            double distance = Convert.ToDouble(lblDistanceTravelled.Text);
            distance =+ Math.Sqrt(Math.Pow(targetX - sourceX, 2) + Math.Pow(targetY - sourceY, 2));
            lblDistanceTravelled.Text = distance.ToString();
        }
        sourceX = targetX;
        sourceY = targetY;
    }

The variables targetX and sourceX are most likely in pixels, so the resulting distance would be in pixels.

In order to convert that to "inches on the screen", you would have to know the size of the screen. You can determine the number of pixels per inch and convert from there (though that only provides an estimate of what you would get if you actually hold a ruler to the screen). To get the pixels per inch, see

How do I determine the true pixel size of my Monitor in .NET?

From that question, you can get DPI as follows (but read the accepted answer for many caveats)

PointF dpi = PointF.Empty;
using(Graphics g = this.CreateGraphics()){
    dpi.X = g.DpiX;
    dpi.Y = g.DpiY;
}

Converting between units works like this:

lengthInInches = numberOfPixes / dotsPerInch

Here "dots" and "pixels" mean the same thing. I'm using common terminology.

You can get the "current DPI" via

int currentDPI = 0;  
using (Graphics g = this.CreateGraphics())  
{  
    currentDPI = (int)g.DpiX;      
}

and then you can get

double distanceInInches = distance/*InPixels*/ / currentDPI;

However, the system's DPI setting can't really be relied upon to give a true conversion from pixel distances to on-screen-inch distances.

        double dpc = this.CreateGraphics().DpiX / 2.54; //Dots Per Centimeter

        //calculate the number of pixels in the line
        double lineLengthInPixels = Math.Sqrt(Math.Pow(x2 - x1, 2) + Math.Pow(y2 - y1, 2));

        //line length in centimenters
        double lineLengthInCentimeters = dpc / lineLengthInPixels;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM