1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| #include <string> #include <iostream>
struct Vector2 { float x, y;
Vector2(float x, float y) : x(x), y(y) {}
Vector2 Add(const Vector2& other) const { return Vector2(x + other.x, y + other.y); }
Vector2 Multiply(const Vector2& other) const { return Vector2(x * other.x, y * other.y); }
Vector2 operator+(const Vector2& other) const { return Vector2(x + other.x, y + other.y); }
Vector2 operator*(const Vector2& other) const { return Vector2(x * other.x, y * other.y); }
bool operator==(const Vector2& other) const { return x == other.x && y == other.y; }
bool operator!=(const Vector2& other) const { return !(*this == other); } };
std::ostream& operator<<(std::ostream& stream, const Vector2& other) { stream << other.x << " , " << other.y; return stream; }
int main() { Vector2 pos(4.0f, 4.0f); Vector2 speed(0.5f, 1.5f); Vector2 powerUp(0.5f, 1.5f);
Vector2 result = pos.Add(speed.Multiply(powerUp)); Vector2 result1 = pos + speed * powerUp;
if (result == result1) {
}
std::cout << result << std::endl;
return 0; }
|