diff options
| author | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:08:27 +0200 |
|---|---|---|
| committer | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:08:27 +0200 |
| commit | c9b6b9a5ca082fe7c1b6f58d7713f785a9eb6a5c (patch) | |
| tree | 3e4f42f93c7ae89a364e4d51fff6e5cec4e55fa9 /graphs/cpp/cartesian_vector/vector.cc | |
add: graphs et rushs
Diffstat (limited to 'graphs/cpp/cartesian_vector/vector.cc')
| -rw-r--r-- | graphs/cpp/cartesian_vector/vector.cc | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/graphs/cpp/cartesian_vector/vector.cc b/graphs/cpp/cartesian_vector/vector.cc new file mode 100644 index 0000000..1291f69 --- /dev/null +++ b/graphs/cpp/cartesian_vector/vector.cc @@ -0,0 +1,69 @@ +#include "vector.hh" + +#include "state_saver.hh" + +Vector::Vector(double x, double y) + : x_{ x } + , y_{ y } +{} +double Vector::get_x() const +{ + return x_; +} +double Vector::get_y() const +{ + return y_; +} +Vector& Vector::operator+=(const Vector& rhs) +{ + x_ += rhs.get_x(); + y_ += rhs.get_y(); + return *this; +} +Vector& Vector::operator-=(const Vector& rhs) +{ + x_ -= rhs.get_x(); + y_ -= rhs.get_y(); + return *this; +} +Vector& Vector::operator*=(double scalar) +{ + x_ *= scalar; + y_ *= scalar; + return *this; +} +Vector operator+(const Vector& lhs, const Vector& rhs) +{ + Vector lhs1{ lhs }; + return lhs1 += rhs; +} +Vector operator-(const Vector& lhs, const Vector& rhs) +{ + Vector lhs1{ lhs }; + return lhs1 -= rhs; +} +Vector operator*(const Vector& lhs, double scalar) +{ + Vector lhs1{ lhs }; + return lhs1 *= scalar; +} +Vector operator*(double scalar, const Vector& rhs) +{ + return rhs * scalar; +} +double operator*(const Vector& lhs, const Vector& rhs) +{ + return lhs.get_x() * rhs.get_x() + lhs.get_y() * rhs.get_y(); +} + +FormatNumericalData Vector::format_numerical_data_{ "[ ", " ]", 12, true, + true }; + +std::ostream& operator<<(std::ostream& os, const Vector& vec) +{ + StateSaver savestate{ os }; + Vector::format_numerical_data_.formatOs(os); + os << Vector::format_numerical_data_.prefix << vec.get_x() << " , " + << vec.get_y() << Vector::format_numerical_data_.suffix; + return os; +}
\ No newline at end of file |
