| 1 | // SuperTux |
| 2 | // Copyright (C) 2006 Matthias Braun <matze@braunis.de> |
| 3 | // |
| 4 | // This program is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // |
| 9 | // This program is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU General Public License |
| 15 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | |
| 17 | #ifndef HEADER_SUPERTUX_MATH_VECTOR_HPP |
| 18 | #define |
| 19 | |
| 20 | #include <math.h> |
| 21 | #include <iosfwd> |
| 22 | |
| 23 | /** Simple two dimensional vector. */ |
| 24 | class Vector final |
| 25 | { |
| 26 | public: |
| 27 | Vector(float nx, float ny) |
| 28 | : x(nx), y(ny) |
| 29 | { } |
| 30 | Vector(const Vector& other) |
| 31 | : x(other.x), y(other.y) |
| 32 | { } |
| 33 | Vector() |
| 34 | : x(0), y(0) |
| 35 | { } |
| 36 | |
| 37 | bool operator ==(const Vector& other) const |
| 38 | { |
| 39 | return x == other.x && y == other.y; |
| 40 | } |
| 41 | |
| 42 | bool operator !=(const Vector& other) const |
| 43 | { |
| 44 | return !(x == other.x && y == other.y); |
| 45 | } |
| 46 | |
| 47 | Vector& operator=(const Vector& other) |
| 48 | { |
| 49 | x = other.x; |
| 50 | y = other.y; |
| 51 | return *this; |
| 52 | } |
| 53 | |
| 54 | Vector operator+(const Vector& other) const |
| 55 | { |
| 56 | return Vector(x + other.x, y + other.y); |
| 57 | } |
| 58 | |
| 59 | Vector operator-(const Vector& other) const |
| 60 | { |
| 61 | return Vector(x - other.x, y - other.y); |
| 62 | } |
| 63 | |
| 64 | Vector operator*(float s) const |
| 65 | { |
| 66 | return Vector(x * s, y * s); |
| 67 | } |
| 68 | |
| 69 | Vector operator/(float s) const |
| 70 | { |
| 71 | return Vector(x / s, y / s); |
| 72 | } |
| 73 | |
| 74 | Vector operator-() const |
| 75 | { |
| 76 | return Vector(-x, -y); |
| 77 | } |
| 78 | |
| 79 | const Vector& operator +=(const Vector& other) |
| 80 | { |
| 81 | x += other.x; |
| 82 | y += other.y; |
| 83 | return *this; |
| 84 | } |
| 85 | |
| 86 | const Vector& operator -=(const Vector& other) |
| 87 | { |
| 88 | x -= other.x; |
| 89 | y -= other.y; |
| 90 | return *this; |
| 91 | } |
| 92 | |
| 93 | const Vector& operator *=(float val) |
| 94 | { |
| 95 | x *= val; |
| 96 | y *= val; |
| 97 | return *this; |
| 98 | } |
| 99 | |
| 100 | const Vector& operator /=(float val) |
| 101 | { |
| 102 | x /= val; |
| 103 | y /= val; |
| 104 | return *this; |
| 105 | } |
| 106 | |
| 107 | /// Scalar product of 2 vectors |
| 108 | float operator*(const Vector& other) const |
| 109 | { |
| 110 | return x*other.x + y*other.y; |
| 111 | } |
| 112 | |
| 113 | float norm() const; |
| 114 | Vector unit() const; |
| 115 | |
| 116 | Vector floor() const |
| 117 | { |
| 118 | return Vector(floorf(x), floorf(y)); |
| 119 | } |
| 120 | |
| 121 | // ... add the other operators as needed, I'm too lazy now ... |
| 122 | |
| 123 | float x, y; // leave this public, get/set methods just give me headaches |
| 124 | // for such simple stuff :) |
| 125 | }; |
| 126 | |
| 127 | std::ostream& operator<<(std::ostream& out, const Vector& vector); |
| 128 | |
| 129 | #endif |
| 130 | |
| 131 | /* EOF */ |
| 132 | |