From c9b6b9a5ca082fe7c1b6f58d7713f785a9eb6a5c Mon Sep 17 00:00:00 2001 From: Martial Simon Date: Mon, 15 Sep 2025 01:08:27 +0200 Subject: add: graphs et rushs --- graphs/cpp/cartesian_vector/vector.cc | 69 +++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 graphs/cpp/cartesian_vector/vector.cc (limited to 'graphs/cpp/cartesian_vector/vector.cc') 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 -- cgit v1.2.3