3

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 (e.X & e.Y)

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;
    }
Fred
  • 5,663
  • 4
  • 45
  • 74
  • Would be helpfull to include how you populate 'targetX' and 'sourceX' (I would guess it's pixels btw) – Eddy Dec 18 '12 at 16:18
  • what is the distance intended to be used for? a distance between 2 points on a computer screen doesn't ever come in inches/cm, and requires quite a lot of information to figure out (screen DPI etc). – Rawrgramming Dec 18 '12 at 16:20
  • @Rawrgramming to be honest i'm bored at work on the run up to Xmas. Project is on hold. My wife asked me last night if I had any idea how many inches across the screen I moved my mouse in one day. You can guess the rest! :) – Fred Dec 18 '12 at 16:24
  • @Fred: http://download.cnet.com/My-Mouse-Meter/3000-2072_4-10445290.html?tag=mncol;6 – Eric J. Dec 18 '12 at 16:27
  • @EricJ. Appreciate the link but I am killing time so looking to do it myself :) – Fred Dec 18 '12 at 16:32

3 Answers3

5

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.

Community
  • 1
  • 1
Eric J.
  • 147,927
  • 63
  • 340
  • 553
3

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.

Rawling
  • 49,248
  • 7
  • 89
  • 127
1
        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;
LaGrandMere
  • 10,265
  • 1
  • 33
  • 41