Get distance between two declared points in a struct

2

I have a struct with different parameters, including one consisting of a point on a plane, with a coordinate x and another and .

I want to create a function that, from two points, calculate its distance with the corresponding formula. My problem is that I can not connect values for the points, and it gives inconsistent results.

Code:

#include <stdio.h>
#include <math.h>
#define N 20

struct Tpunto                   // punto en un plano, x abscisas, y ordenadas
{
    float x;
    float y;
};

struct TlistaPuntos            //Lista de puntos del plano
{
    int tam;                      //cantidad de valores en el vector puntos
    struct Tpunto puntos[N];
};

int distancia_puntos(struct Tpunto x, struct Tpunto y);


void main()
{
    struct Tpunto p1, p2;

    printf("\nLa distancia entre los puntos %d y %d es %d", p1, p2, distancia_puntos(p1, p2));

    printf("\n\n");

}

int distancia_puntos(struct Tpunto punto1, struct Tpunto punto2)
{
    int resul=sqrt((pow(punto2.x-punto1.x, 2))+(pow(punto2.y-punto1.y, 2)));

    return resul;
}
    
asked by DDN 16.05.2016 в 13:30
source

1 answer

3

I am not sure if I fully understand your query, but as far as I can conclude your problem is how to assign values to the struct variables. The assignment would be more or less like this:

void main()
{
  struct Tpunto p1, p2;
  p1.x = val_1_x // asignar valor para el punto1 en x
  p1.y = val_1_y // asignar valor para el punto1 en y

  p2.x = val_2_x // asignar valor para el punto2 en x
  p2.y = val_2_y // asignar valor para el punto2 en y

  //.. resto del código luego de la asignación
}

Also, as mentioned in one of the comments, the function that calculates distance should change it so that it returns a value float . There should be something like the following:

float distancia_puntos(struct Tpunto punto1, struct Tpunto punto2)
{
  return sqrt((pow(punto2.x-punto1.x, 2))+(pow(punto2.y-punto1.y, 2)));
}

Then this and stored the desired values in the struct, your algorithms should give more consistent values.

Greetings

    
answered by 16.05.2016 / 14:32
source