Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I measure diagonal distance points?

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.

like image 313
Rye Avatar asked Sep 29 '10 06:09

Rye


People also ask

How will you measure diagonal distance?

You can use the hypotenuse formula, e.g., from the Pythagorean theorem calculator, to estimate the diagonal of a rectangle, which can be expressed with the following formula: d² = l² + w² , and now you should know how to find the diagonal of a rectangle explicit formula - just take a square root: d = √(l² + w²) .

What is the diagonal distance between the two points?

This means that you will square the x-axis distance (x2 - x1), and that you will separately square the y-axis distance (y2 - y1). Add the squared values together. This will give you the square of the diagonal, linear distance between your two points.

What is the formula of diagonal of?

The diagonal formula is a formula for calculating the number of diagonals in various polygons and their lengths. An n-sided polygon's number of diagonal lines = n(n-3)/2, where n is the number of sides.


1 Answers

You need to use Pythagoras' theorem.

d = Math.Sqrt(Math.Pow(end.x - start.x, 2) + Math.Pow(end.y - start.y, 2))
like image 79
Andrew Cooper Avatar answered Oct 17 '22 01:10

Andrew Cooper