Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <math.h>
- using namespace std;
- struct Point2f
- {
- Point2f(const float _x,const float _y) : x(_x), y(_y) {}
- Point2f(const Point2f& other) : x(other.x), y(other.y) {}
- Point2f() : x(.0f), y(.0f) {}
- float x, y;
- Point2f operator +(const Point2f& other)
- { return Point2f((x + other.x), (y + other.y)); }
- Point2f operator -(const Point2f& other)
- { return Point2f(x - other.x, y - other.y); }
- Point2f operator =(const Point2f& other)
- { x = other.x; y = other.y; return *this;}
- float distance(Point2f& other)
- {return sqrt(pow(other.x - x, 2) + pow(other.y - y, 2));}
- };
- struct Vec2f
- {
- Vec2f(const Point2f& _start,const Point2f& _end) : start(_start), end(_end) {}
- Vec2f(const Vec2f& other) : start(other.start), end(other.end) {}
- Vec2f() : start(), end() {}
- Point2f start, end;
- Vec2f operator +(const Vec2f& other)
- { return Vec2f(start + other.start, end + other.end); }
- Vec2f operator -(const Vec2f& other)
- { return Vec2f(start - other.start, end - other.end); }
- Vec2f operator =(const Vec2f& other)
- { start = other.start; end = other.end; return *this; }
- float getLength() {return start.distance(end);}
- };
- int main()
- {
- const int count = 5;
- Point2f start, end;
- Vec2f vect[count];
- for (int i = 0; i < count; ++i)
- {
- printf("Enter x and y for begin of %d vector: ", i+1);
- scanf("%f%*c%f", &start.x, &start.y);
- printf("Enter x and y for end of %d vector: ", i+1);
- scanf("%f%*c%f", &end.x, &end.y);
- vect[i] = Vec2f(start, end);
- }
- printf("\nResult:\n");
- for (int i = 0; i < count; ++i)
- {
- printf("Length of %d vector: %.4f\n", i+1, vect[i].getLength());
- for (int s = i + 1; s < count; ++s)
- {
- printf("\tLength of vector(%d) - vector(%d): %.4f\n", i+1, s+1, (vect[i] - vect[s]).getLength());
- printf("\tLength of vector(%d) + vector(%d): %.4f\n", i+1, s+1, (vect[i] + vect[s]).getLength());
- }
- }
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment