Is the math going wrong? C##

Asked

Viewed 82 times

-1

Hello I am very confused now I took the formula to take the distance between 2 points and applied in my application but this giving result different from the calculator Imagem Explicando o Problema

text code:

        public static double GetDist(Point A, Point B)
        {
            double X, Y;

            X = (B.X - A.X) ^ 2;
            Y = (B.Y - A.Y) ^ 2;

            Debug.WriteLine($"X:{A.X},Y:{A.Y}");
            Debug.WriteLine($"X:{B.X},Y:{B.Y}");
            Debug.WriteLine($"Result:{X+Y}");
            return Math.Sqrt(X+Y);
        }

Input: A = {126, 101}, B = {150, 60}.

Code Debug Result from: -17

and in the field of 2257 in Account: (150-126) 2 + (60-101) 2

.

.

Please Ignore the Math. Sqrt please.

  • I’m sorry if it’s a silly mistake but I make it like seven hours in this mistake.

  • 1

    Try using Math.Pow((B.X - A.X), 2) for x and y values.

  • 2

    In C# the operator ^ is not potentiation, is logical connective to the or exclusive bit by bit

  • Bernardo Lopes and Augusto Vasquel Thanks. One Completed the other in the explanation.

  • Use Math.Pow(base, exponent) or simply: deltax*deltax

1 answer

0


Hello, If you intend to make a Cartesian turn, you should make a for with a range encompassing this code to create a curve.

public static List<double> GetDist(Point A, Point B){
    List<double> result = new List<double>();
    for (int i = -5; i < 6; i++){
        double X = i, Y = i;

        X = (B.X - A.X) ^ 2;
        Y = (B.Y - A.Y) ^ 2;

        Debug.WriteLine($ "X:{A.X},Y:{A.Y}");
        Debug.WriteLine($ "X:{B.X},Y:{B.Y}");
        Debug.WriteLine($ "Result:{X+Y}");
        result.add(Math.Sqrt(X + Y));
    }
    return result;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.