Advertisement
cotolonco

Structs: Distancia entre 2 puntos

Nov 22nd, 2012
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3.  
  4. using namespace std;
  5.  
  6. //Distancia entre 2 puntos con Estructura
  7.  
  8. struct Punto{
  9.     float x;
  10.     float y;
  11.     Punto(){x = 0; y = 0;} //Constructor
  12.     void Set(float xo, float yo){x = xo; y = yo;} //Estableciendo valor
  13. };
  14.  
  15. typedef struct Punto Punto;
  16.  
  17. //Función Prototipo:
  18. float distancia(Punto, Punto);
  19.  
  20. int main()
  21. {
  22.     Punto punto1, punto2; // se inician como 0, 0 por el constructor
  23.     //Establecemos 2 puntos:
  24.     punto1.Set(4.6, 8.4);
  25.     punto2.Set(-6.3, 2.5);
  26.     //Escribimos distancia
  27.     cout << endl << "La distancia es: " << distancia(punto1, punto2) << endl;
  28.     return 0;
  29. }
  30.  
  31. float distancia(Punto p1, Punto p2){
  32.     return sqrt(pow(p1.x - p2.x, 2.0) + pow(p1.y - p2.y, 2.0));
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement