Adjusted Vector2
This commit is contained in:
+5
-4
@@ -27,10 +27,8 @@
|
|||||||
|
|
||||||
class Vector2 {
|
class Vector2 {
|
||||||
public:
|
public:
|
||||||
double x, y;
|
double x = 0, y = 0;
|
||||||
Vector2() {
|
Vector2() = default;
|
||||||
x = y = 0;
|
|
||||||
}
|
|
||||||
Vector2(double i, double j) {
|
Vector2(double i, double j) {
|
||||||
x = i; y = j;
|
x = i; y = j;
|
||||||
}
|
}
|
||||||
@@ -40,11 +38,13 @@ public:
|
|||||||
double SquaredLength() const {
|
double SquaredLength() const {
|
||||||
return x*x+y*y;
|
return x*x+y*y;
|
||||||
}
|
}
|
||||||
|
|
||||||
double operator[](size_t i) {
|
double operator[](size_t i) {
|
||||||
if (i >= 2)
|
if (i >= 2)
|
||||||
throw(std::runtime_error("Out of range"));
|
throw(std::runtime_error("Out of range"));
|
||||||
return *(&x+i);
|
return *(&x+i);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Arithmetic operators
|
//Arithmetic operators
|
||||||
Vector2 operator+(Vector2 v) const { return Vector2(x + v.x, y + v.y); }
|
Vector2 operator+(Vector2 v) const { return Vector2(x + v.x, y + v.y); }
|
||||||
Vector2 operator-(Vector2 v) const { return Vector2(x - v.x, y - v.y); }
|
Vector2 operator-(Vector2 v) const { return Vector2(x - v.x, y - v.y); }
|
||||||
@@ -65,6 +65,7 @@ public:
|
|||||||
bool operator==(Vector2 v) { return (x == v.x && y == v.y); }
|
bool operator==(Vector2 v) { return (x == v.x && y == v.y); }
|
||||||
bool operator!=(Vector2 v) { return (x != v.x || y != v.y); }
|
bool operator!=(Vector2 v) { return (x != v.x || y != v.y); }
|
||||||
|
|
||||||
|
//templates
|
||||||
template<typename T> Vector2 operator+=(T t) { return *this = *this + t; }
|
template<typename T> Vector2 operator+=(T t) { return *this = *this + t; }
|
||||||
template<typename T> Vector2 operator-=(T t) { return *this = *this - t; }
|
template<typename T> Vector2 operator-=(T t) { return *this = *this - t; }
|
||||||
template<typename T> Vector2 operator*=(T t) { return *this = *this * t; }
|
template<typename T> Vector2 operator*=(T t) { return *this = *this * t; }
|
||||||
|
|||||||
Reference in New Issue
Block a user