views:

74

answers:

3

I can compute horizontal and vertical points, but I cant figure out how to compute distance using diagonal points. Can someone help me with this.

here's the code for my horizontal and vertical measuring:

private float ComputeDistance(float point1, float point2) 
{
        float sol1 = point1 - point2;
        float sol2 = (float)Math.Abs(Math.Sqrt(sol1 * sol1));

        return sol2;
}

protected override void OnMouseMove(MouseEventArgs e)
    {

        _endPoint.X = e.X;
        _endPoint.Y = e.Y;

        if (ComputeDistance(_startPoint.X, _endPoint.X) <= 10)
        {
            str = ComputeDistance(_startPoint.Y, _endPoint.Y).ToString();
        }
        else
        {
            if (ComputeDistance(_startPoint.Y, _endPoint.Y) <= 10)
            {
                str = ComputeDistance(_startPoint.X, _endPoint.X).ToString();
            }
        }
    }

Assuming that the _startPoint has been already set.

alt text

In this image the diagonal point is obviously wrong.

+1  A: 

Well you might take a look at: http://en.wikipedia.org/wiki/Pythagorean_theorem

Yves M.
+1 for suggesting Pythagoras' theorem
Xander
+2  A: 

I think you're looking for the Euclidean distance formula.

In mathematics, the Euclidean distance or Euclidean metric is the "ordinary" distance between two points that one would measure with a ruler, and is given by the Pythagorean formula.

Greg Hewgill
+1 for pointing out that there are actually many ways of calculating distance and that the Euclidean distance is probably the one wanted here.
Chris
+1  A: 

You need to use Pythagoras' theorem.

d = Math.Sqrt(Math.Pow(end.x - start.x, 2) + Math.Pow(end.y - start.y, 2))
Andrew Cooper
This one's a Pythagorean, Wait I will try this one.
Rye
Good stuff. It worked. Thanks Andrew.
Rye
+1 for suggesting Pythagoras' theorem
Xander