Created
March 5, 2019 20:15
-
-
Save Ben1980/e6c0094f261731cd341a48224e36075f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bool Vector2D::operator==(const Vector2D &rhs) const { | |
auto equalZero = std::numeric_limits<double>::min(); | |
return fabs(x - rhs.x) <= equalZero && | |
fabs(y - rhs.y) <= equalZero; | |
} | |
bool Vector2D::operator!=(const Vector2D &rhs) const { | |
return !(rhs == *this); | |
} | |
double Vector2D::length() const { | |
return sqrt(x*x + y*y); | |
} | |
Vector2D& Vector2D::operator-=(const Vector2D& rhs) { | |
*this = *this - rhs; | |
return *this; | |
} | |
Vector2D& Vector2D::operator*=(const double& rhs) { | |
*this = *this * rhs; | |
return *this; | |
} | |
Vector2D& Vector2D::operator/=(const double& rhs) { | |
*this = *this / rhs; | |
return *this; | |
} | |
Vector2D &Vector2D::operator+=(const Vector2D &rhs) { | |
*this = *this + rhs; | |
return *this; | |
} | |
Vector2D operator-(const Vector2D &lhs, const Vector2D &rhs) { | |
return { lhs.x - rhs.x, | |
lhs.y - rhs.y }; | |
} | |
Vector2D operator+(const Vector2D &lhs, const Vector2D &rhs) { | |
return { lhs.x + rhs.x, | |
lhs.y + rhs.y }; | |
} | |
Vector2D operator*(const Vector2D &lhs, const double &rhs) { | |
return { lhs.x * rhs, | |
lhs.y * rhs }; | |
} | |
Vector2D operator*(const double &lhs, const Vector2D &rhs) { | |
return rhs * lhs; | |
} | |
Vector2D operator/(const Vector2D &lhs, const double &rhs) { | |
assert(fabs(rhs) > 0); | |
return { lhs.x / rhs, | |
lhs.y / rhs }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment