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

0

I created in the classroom that 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 ...

follow 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;
    }
    
asked by anonymous 30.04.2017 / 17:58

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 leave this code cleaner by using struct s and encapsulating some repeated actions in subroutines.

For example, points can be abstracted like this:

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 would not recommend using the pow function, since native multiplication is faster (?).

Another thing would be this 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 part of collecting the coordinate values of the points, something like "type the x coordinate of point A", "type the coordinate y of point C".

    
02.05.2017 / 19:32