Skip to content

Instantly share code, notes, and snippets.

@Ben1980
Created March 5, 2019 20:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ben1980/e6c0094f261731cd341a48224e36075f to your computer and use it in GitHub Desktop.
Save Ben1980/e6c0094f261731cd341a48224e36075f to your computer and use it in GitHub Desktop.
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