How to calculate the distance between two points and calculate distance ABC, CAB, BAC? in C

Asked

Viewed 931 times

0

created in class this program that calculates the distance between two points and then gives the option of 3 different paths: ABC, BAC or CAB.

I would like tips to optimize my code, ideas on how to improve it and also tips on new features...

follows the code:

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>

    int main(int argc, char *argv[]) 
    {
        float ax, bx,cx, ay, by, cy;
        float AB, BC, AC;
        float distancia;
        int rota;

        printf("Digite o valor de Ax\n");
        scanf("%f", &ax);
        printf("Digite o valor de Ay\n");
        scanf("%f", &ay);
        printf("Digite o valor de Bx\n");
        scanf("%f", &bx);
        printf("Digite o valor de By\n");
        scanf("%f", &by);
        printf("Digite o valor de Cx\n");
        scanf("%f", &cx);
        printf("Digite o valor de Cy\n");
        scanf("%f", &cy);


        AB=sqrt(pow((bx-ax),2)+pow((by-ay),2));
        printf("A distancia entre os pontos A e B eh %f\n", AB);

        AC=sqrt(pow((cx-ax),2)+pow((cy-ay),2));
        printf("A distancia entre os pontos A e C eh %f\n", AC);
        BC=sqrt(pow((cx-bx),2)+pow((cy-by),2));
        printf("A distancia entre os pontos B e C eh %f\n\n", BC);
        printf("Escolha uma rota: ABC = 1, BAC = 2 ou CAB = 3\n");
        scanf("%d", &rota);
        switch (rota)
        {
          case 1:
            distancia = AB + BC;
            printf("A distancia entre os pontos ABC eh %f\n", distancia);
          break;

          case 2:
            distancia = AB + BC;
            printf("A distancia entre os pontos BAC eh %f\n", distancia);
          break;

          case 3:
            distancia = AB + BC;
            printf("A distancia entre os pontos CAB eh %f\n", distancia);
          break;

          default :
        printf("ROTA INVALIDA!!!");

        }

        system("pause"); 
        return 0;
    }

1 answer

1


The tip I give, then, is about style. Do you already have any notion of data structure? It would be interesting to make this code cleaner by using structs and encapsulating some repeated actions in subroutines.

For example, points can be abstracted this way:

typedef struct point{
  float xcoord;
  float ycoord;
} point;

/* Uso: point A = set_point(4.0,3.0) */
point set_point(float xcoord, float ycoord){
  point ret;

  ret.xcoord = xcoord;
  ret.ycoord = ycoord;

  return ret;
}

With this, the distance between two points can be calculated by this function:

float distance(point A, point B){
  float dx = A.xcoord - B.xcoord;
  float dy = A.ycoord - B.ycoord;

  return sqrt(dx*dx + dy*dy);
}

I wouldn’t recommend using the function pow, since native multiplication is(?) faster.

Another thing would be that phrase "A distância entre os pontos ABC é".... That phrasing is confusing. How about "O comprimento da rota A-B-C é"...?

I would also suggest something more verbose in the collection of point coordinate values, something like "type the x coordinate of point A", "type the y coordinate of point C".

  • +1 for suggesting breaking code into subroutines and using data structures. I would only recommend using .x and .yinstead of .xcoord and .ycoord, which is redundant, and pass the parameters to distance() by reference, rather than by value.

  • Thanks for the tips, Anderson!

Browser other questions tagged

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