Skip to content

Commit 1b859ae

Browse files
committed
add formatter support to Vector2D
1 parent c037d04 commit 1b859ae

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

include/units/Vector2D.hpp

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
#include "units/Angle.hpp"
44

5-
namespace units {
65
/**
76
* @class Vector2D
87
*
@@ -43,8 +42,8 @@ template <isQuantity T> class Vector2D {
4342
*/
4443
constexpr static Vector2D fromPolar(Angle t, T m) {
4544
m = abs(m);
46-
t = constrainAngle360(t);
47-
return Vector2D<T>(m * cos(t), m * sin(t));
45+
t = units::constrainAngle360(t);
46+
return Vector2D<T>(m * units::cos(t), m * units::sin(t));
4847
}
4948

5049
/**
@@ -277,9 +276,30 @@ constexpr Vector2D<Q3> operator*(Q1 lhs, const Vector2D<Q2>& rhs) {
277276
*/
278277
template <isQuantity Q> constexpr Vector2D<Q> operator*(double lhs, const Vector2D<Q>& rhs) { return rhs * lhs; }
279278

279+
namespace std {
280+
template <typename T> struct formatter<Vector2D<T>> : formatter<T> {
281+
// Optionally parse format specifiers for T
282+
constexpr auto parse(auto& ctx) { return formatter<T>::parse(ctx); }
283+
284+
auto format(const Vector2D<T>& vector, format_context& ctx) const {
285+
auto it = ctx.out();
286+
it = format_to(it, "(");
287+
288+
// Format vector.x using the base formatter<T>
289+
it = static_cast<const formatter<T>*>(this)->format(vector.x, ctx);
290+
it = format_to(it, ", ");
291+
292+
// Format vector.y using the base formatter<T>
293+
it = static_cast<const formatter<T>*>(this)->format(vector.y, ctx);
294+
it = format_to(it, ")");
295+
296+
return it;
297+
}
298+
};
299+
} // namespace std
300+
280301
// define some common vector types
281302
typedef Vector2D<Length> V2Position;
282303
typedef Vector2D<LinearVelocity> V2Velocity;
283304
typedef Vector2D<LinearAcceleration> V2Acceleration;
284305
typedef Vector2D<Force> V2Force;
285-
} // namespace units

src/main.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ void initialize() {
1515
std::cout << std::format("{:.2f}", units::pow<5>(505_cm) * 15_celsius) << std::endl;
1616
Number a(2.123);
1717
std::cout << std::format("{:.2f}", a) << std::endl;
18+
Vector2D<Length> v2a = V2Position(2_in, 2_in) / 2;
19+
std::cout << std::format("{}", v2a) << std::endl;
1820
}
1921

2022
constexpr void miscTests() {
@@ -48,11 +50,11 @@ constexpr void v3dTests() {
4850

4951
constexpr void v2dTests() {
5052
// check Vector2D overloads
51-
units::Vector2D<Length> v2a = units::V2Position(2_in, 2_in) / 2;
52-
units::Vector2D<Length> v2b = 2 * units::V2Position(2_in, 2_in) * 2;
53-
units::Vector2D<Area> v2c = 2_in * units::V2Position(2_in, 2_in);
54-
units::Vector2D<Area> v2d = units::V2Position(2_in, 2_in) * 2_in;
55-
units::Vector2D<Number> v2e = units::V2Position(2_in, 2_in) / 2_in;
53+
Vector2D<Length> v2a = V2Position(2_in, 2_in) / 2;
54+
Vector2D<Length> v2b = 2 * V2Position(2_in, 2_in) * 2;
55+
Vector2D<Area> v2c = 2_in * V2Position(2_in, 2_in);
56+
Vector2D<Area> v2d = V2Position(2_in, 2_in) * 2_in;
57+
Vector2D<Number> v2e = V2Position(2_in, 2_in) / 2_in;
5658
}
5759

5860
constexpr void angleTests() {

0 commit comments

Comments
 (0)