40 lines
698 B
C++
40 lines
698 B
C++
#include "math_lib.hpp"
|
|
#include <cmath>
|
|
|
|
namespace Math {
|
|
|
|
// --- Free functions ---
|
|
|
|
double sqrt(double x) {
|
|
return std::sqrt(x);
|
|
}
|
|
|
|
double square(double x) {
|
|
return x * x;
|
|
}
|
|
|
|
double cube(double x) {
|
|
return x * x * x;
|
|
}
|
|
|
|
// --- Vector2D ---
|
|
|
|
Vector2D::Vector2D(double x, double y) : x(x), y(y) {}
|
|
|
|
double Vector2D::length() const {
|
|
return std::sqrt(x * x + y * y);
|
|
}
|
|
|
|
Vector2D Vector2D::add(const Vector2D& other) const {
|
|
return Vector2D(x + other.x, y + other.y);
|
|
}
|
|
|
|
Vector2D Vector2D::scale(double factor) const {
|
|
return Vector2D(x * factor, y * factor);
|
|
}
|
|
|
|
double Vector2D::dot(const Vector2D& other) const {
|
|
return x * other.x + y * other.y;
|
|
}
|
|
|
|
} // namespace Math
|