yukisaw

Length of vectors

Nov 26th, 2015
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.90 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3.  
  4. using namespace std;
  5.  
  6. struct Point2f
  7. {
  8.     Point2f(const float _x,const float _y) : x(_x), y(_y) {}
  9.     Point2f(const Point2f& other) : x(other.x), y(other.y) {}
  10.     Point2f() : x(.0f), y(.0f) {}
  11.     float x, y;
  12.     Point2f operator +(const Point2f& other)
  13.     { return Point2f((x + other.x), (y + other.y)); }
  14.     Point2f operator -(const Point2f& other)
  15.     { return Point2f(x - other.x, y - other.y); }
  16.     Point2f operator =(const Point2f& other)
  17.     { x = other.x; y = other.y; return *this;}
  18.     float distance(Point2f& other)
  19.     {return sqrt(pow(other.x - x, 2) + pow(other.y - y, 2));}
  20. };
  21.  
  22. struct Vec2f
  23. {
  24.     Vec2f(const Point2f& _start,const Point2f& _end) : start(_start), end(_end) {}
  25.     Vec2f(const Vec2f& other) : start(other.start), end(other.end) {}
  26.     Vec2f() : start(), end() {}
  27.     Point2f start, end;
  28.     Vec2f operator +(const Vec2f& other)
  29.     { return Vec2f(start + other.start, end + other.end); }
  30.     Vec2f operator -(const Vec2f& other)
  31.     { return Vec2f(start - other.start, end - other.end); }
  32.     Vec2f operator =(const Vec2f& other)
  33.     { start = other.start; end = other.end; return *this; }
  34.     float getLength() {return start.distance(end);}
  35. };
  36.  
  37. int main()
  38. {
  39.     const int count = 5;
  40.     Point2f start, end;
  41.     Vec2f vect[count];
  42.     for (int i = 0; i < count; ++i)
  43.     {
  44.         printf("Enter x and y for begin of %d vector: ", i+1);
  45.         scanf("%f%*c%f", &start.x, &start.y);
  46.         printf("Enter x and y for end of %d vector: ", i+1);
  47.         scanf("%f%*c%f", &end.x, &end.y);
  48.         vect[i] = Vec2f(start, end);
  49.     }
  50.     printf("\nResult:\n");
  51.     for (int i = 0; i < count; ++i)
  52.     {
  53.         printf("Length of %d vector: %.4f\n", i+1, vect[i].getLength());
  54.         for (int s = i + 1; s < count; ++s)
  55.         {
  56.            printf("\tLength of vector(%d) - vector(%d): %.4f\n", i+1, s+1, (vect[i] - vect[s]).getLength());
  57.            printf("\tLength of vector(%d) + vector(%d): %.4f\n", i+1, s+1, (vect[i] + vect[s]).getLength());
  58.         }
  59.     }
  60.     system("pause");
  61. }
Advertisement
Add Comment
Please, Sign In to add comment