88
99namespace omath
1010{
11+
12+ template <class Type > requires std::is_arithmetic_v<Type>
1113 class Vector2
1214 {
1315 public:
14- float x = 0 .f ;
15- float y = 0 .f ;
16+ Type x = static_cast <Type>( 0 ) ;
17+ Type y = static_cast <Type>( 0 ) ;
1618
1719 // Constructors
1820 constexpr Vector2 () = default;
1921
20- constexpr Vector2 (const float x, const float y) : x(x), y(y) {}
22+ constexpr Vector2 (const Type& x, const Type& y) : x(x), y(y) {}
2123
2224 // Equality operators
2325 [[nodiscard]]
@@ -65,31 +67,31 @@ namespace omath
6567 return *this ;
6668 }
6769
68- constexpr Vector2& operator *=(const float fl)
70+ constexpr Vector2& operator *=(const Type& fl)
6971 {
7072 x *= fl;
7173 y *= fl;
7274
7375 return *this ;
7476 }
7577
76- constexpr Vector2& operator /=(const float fl)
78+ constexpr Vector2& operator /=(const Type& fl)
7779 {
7880 x /= fl;
7981 y /= fl;
8082
8183 return *this ;
8284 }
8385
84- constexpr Vector2& operator +=(const float fl)
86+ constexpr Vector2& operator +=(const Type& fl)
8587 {
8688 x += fl;
8789 y += fl;
8890
8991 return *this ;
9092 }
9193
92- constexpr Vector2& operator -=(const float fl)
94+ constexpr Vector2& operator -=(const Type& fl)
9395 {
9496 x -= fl;
9597 y -= fl;
@@ -98,45 +100,45 @@ namespace omath
98100 }
99101
100102 // Basic vector operations
101- [[nodiscard]] float DistTo (const Vector2& vOther) const
103+ [[nodiscard]] Type DistTo (const Vector2& vOther) const
102104 {
103105 return std::sqrt (DistToSqr (vOther));
104106 }
105107
106- [[nodiscard]] constexpr float DistToSqr (const Vector2& vOther) const
108+ [[nodiscard]] constexpr Type DistToSqr (const Vector2& vOther) const
107109 {
108110 return (x - vOther.x ) * (x - vOther.x ) + (y - vOther.y ) * (y - vOther.y );
109111 }
110112
111- [[nodiscard]] constexpr float Dot (const Vector2& vOther) const
113+ [[nodiscard]] constexpr Type Dot (const Vector2& vOther) const
112114 {
113115 return x * vOther.x + y * vOther.y ;
114116 }
115117
116118#ifndef _MSC_VER
117- [[nodiscard]] constexpr float Length () const
119+ [[nodiscard]] constexpr Type& Length () const
118120 {
119121 return std::hypot (x, y);
120122 }
121123
122124 [[nodiscard]] constexpr Vector2 Normalized () const
123125 {
124- const float len = Length ();
126+ const Type len = Length ();
125127 return len > 0 .f ? *this / len : *this ;
126128 }
127129#else
128- [[nodiscard]] float Length () const
130+ [[nodiscard]] Type Length () const
129131 {
130132 return std::hypot (x, y);
131133 }
132134
133135 [[nodiscard]] Vector2 Normalized () const
134136 {
135- const float len = Length ();
137+ const Type len = Length ();
136138 return len > 0 .f ? *this / len : *this ;
137139 }
138140#endif
139- [[nodiscard]] constexpr float LengthSqr () const
141+ [[nodiscard]] constexpr Type LengthSqr () const
140142 {
141143 return x * x + y * y;
142144 }
@@ -187,13 +189,13 @@ namespace omath
187189 }
188190
189191 // Sum of elements
190- [[nodiscard]] constexpr float Sum () const
192+ [[nodiscard]] constexpr Type Sum () const
191193 {
192194 return x + y;
193195 }
194196
195197 [[nodiscard]]
196- constexpr std::tuple<float , float > AsTuple () const
198+ constexpr std::tuple<Type, Type > AsTuple () const
197199 {
198200 return std::make_tuple (x, y);
199201 }
0 commit comments