Lodestone.Level
Loading...
Searching...
No Matches
Vec2.h
Go to the documentation of this file.
1//
2// Created by DexrnZacAttack on 10/14/25 using zPc-i2.
3//
4#ifndef LODESTONE_VEC2I_H
5#define LODESTONE_VEC2I_H
6
8#include <format>
9
11 template <typename T>
12 struct Vec2 {
13 static_assert(std::is_arithmetic_v<T>, "typeof T must be numeric"); // Waiter! I want one Vec2<std::string, std::string> please.
14
15 T x{}, y{};
16
17 constexpr Vec2(const T x, const T z) : x(x), y(z) {}
18
19 template <typename O>
20 explicit constexpr Vec2(const Vec2<O>& rhs) : x(static_cast<T>(rhs.x)), y(static_cast<T>(rhs.y)) {
21 }
22
23 constexpr ~Vec2() = default;
24
25 constexpr operator T *() { return &x; }
26 constexpr operator const T *() const { return &x; }
27
28 constexpr bool operator==(const Vec2 &rhs) const {
29 return x == rhs.x && y == rhs.y;
30 }
31
32 constexpr Vec2 operator+(const T v) const { return {x + v, y + v}; }
33
34 constexpr Vec2 operator-(const T v) const { return {x - v, y - v}; }
35
36 constexpr Vec2 operator*(const T v) const { return {x * v, y * v}; }
37
38 constexpr Vec2 operator/(const T v) const { return {x / v, y / v}; }
39
40 constexpr Vec2 operator+(const Vec2 &rhs) const {
41 return {x + rhs.x, y + rhs.y};
42 }
43
44 constexpr Vec2 operator-(const Vec2 &rhs) const {
45 return {x - rhs.x, y - rhs.y};
46 }
47
48 constexpr Vec2 operator*(const Vec2 &rhs) const {
49 return {x * rhs.x, y * rhs.y};
50 }
51
52 constexpr Vec2 operator/(const Vec2 &rhs) const {
53 return {x / rhs.x, y / rhs.y};
54 }
55
56 std::string toString() const {
57 return std::format("Vec2[x={}, y={}]", x, y);
58 };
59 };
60
61 template <typename T>
62 constexpr Vec2<T> VEC2_ZERO{0,0};
63
64 template <typename T>
65 constexpr Vec2<T> VEC2_ONE{1,1};
66
70} // namespace lodestone::level::types
71
72template <typename T> struct std::hash<lodestone::level::types::Vec2<T>> {
73 size_t
75 return std::hash<T>()(v.x) ^ (std::hash<T>()(v.y) << 1);
76 }
77};
78
79template <typename T>
80struct std::formatter<lodestone::level::types::Vec2<T>> {
81 template <typename FormatParseContext> constexpr auto parse(FormatParseContext &pc) { return pc.begin(); }
82 template <typename FormatContext> auto format(lodestone::level::types::Vec2<T> vec, FormatContext &fc) const { return std::format_to(fc.out(), "x={}, y={}", vec.x, vec.y); }
83};
84
85#endif
Definition Bounds2.h:12
constexpr Vec2< T > VEC2_ZERO
Definition Vec2.h:62
Vec2< float > Vec2f
Definition Vec2.h:68
Vec2< int > Vec2i
Definition Vec2.h:67
constexpr Vec2< T > VEC2_ONE
Definition Vec2.h:65
Vec2< double > Vec2d
Definition Vec2.h:69
Definition Vec2.h:12
constexpr Vec2 operator/(const Vec2 &rhs) const
Definition Vec2.h:52
constexpr Vec2 operator+(const T v) const
Definition Vec2.h:32
constexpr Vec2 operator-(const T v) const
Definition Vec2.h:34
constexpr Vec2 operator/(const T v) const
Definition Vec2.h:38
std::string toString() const
Definition Vec2.h:56
constexpr Vec2 operator*(const T v) const
Definition Vec2.h:36
T y
Definition Vec2.h:15
constexpr ~Vec2()=default
constexpr Vec2 operator-(const Vec2 &rhs) const
Definition Vec2.h:44
constexpr Vec2(const T x, const T z)
Definition Vec2.h:17
constexpr Vec2(const Vec2< O > &rhs)
Definition Vec2.h:20
constexpr bool operator==(const Vec2 &rhs) const
Definition Vec2.h:28
constexpr Vec2 operator*(const Vec2 &rhs) const
Definition Vec2.h:48
constexpr Vec2 operator+(const Vec2 &rhs) const
Definition Vec2.h:40
auto format(lodestone::level::types::Vec2< T > vec, FormatContext &fc) const
Definition Vec2.h:82
constexpr auto parse(FormatParseContext &pc)
Definition Vec2.h:81
size_t operator()(const lodestone::level::types::Vec2< T > &v) const noexcept
Definition Vec2.h:74