cangine
Loading...
Searching...
No Matches
vector2.hpp
Go to the documentation of this file.
1#pragma once
2//#include <type_traits>
3
4namespace cangine {
5
13struct Vector2 {
14 float x{}, y{};
15
16 // Constructors
18 constexpr Vector2() noexcept = default;
24 constexpr Vector2(float _x, float _y) noexcept : x(_x), y(_y) {}
25
26 // Compound assignment operators
32 constexpr Vector2& operator+=(const Vector2& rhs) noexcept {
33 x += rhs.x; y += rhs.y; return *this;
34 }
40 constexpr Vector2& operator-=(const Vector2& rhs) noexcept {
41 x -= rhs.x; y -= rhs.y; return *this;
42 }
48 constexpr Vector2& operator*=(const Vector2& rhs) noexcept {
49 x *= rhs.x; y *= rhs.y; return *this;
50 }
56 constexpr Vector2& operator/=(const Vector2& rhs) noexcept {
57 x /= rhs.x; y /= rhs.y; return *this;
58 }
59
65 constexpr Vector2& operator+=(float rhs) noexcept { x += rhs; y += rhs; return *this; }
71 constexpr Vector2& operator-=(float rhs) noexcept { x -= rhs; y -= rhs; return *this; }
77 constexpr Vector2& operator*=(float rhs) noexcept { x *= rhs; y *= rhs; return *this; }
83 constexpr Vector2& operator/=(float rhs) noexcept { x /= rhs; y /= rhs; return *this; }
84};
85
93// Free operators for Vector2
94#define CANGINE_VECTOR2_OP(OP) \
95constexpr Vector2 operator OP(const Vector2& lhs, const Vector2& rhs) noexcept { return Vector2(lhs.x OP rhs.x, lhs.y OP rhs.y); } \
96constexpr Vector2 operator OP(const Vector2& lhs, float rhs) noexcept { return Vector2(lhs.x OP rhs, lhs.y OP rhs); } \
97constexpr Vector2 operator OP(float lhs, const Vector2& rhs) noexcept { return Vector2(lhs OP rhs.x, lhs OP rhs.y); }
98
103
104#undef CANGINE_VECTOR2_OP
105
106}
Definition audiomanager.cpp:4
Two-component floating-point vector.
Definition vector2.hpp:13
constexpr Vector2 & operator+=(const Vector2 &rhs) noexcept
Adds another vector component-wise.
Definition vector2.hpp:32
constexpr Vector2 & operator-=(const Vector2 &rhs) noexcept
Subtracts another vector component-wise.
Definition vector2.hpp:40
float x
Definition vector2.hpp:14
constexpr Vector2 & operator*=(float rhs) noexcept
Multiplies both components by a scalar.
Definition vector2.hpp:77
constexpr Vector2 & operator-=(float rhs) noexcept
Subtracts a scalar from both components.
Definition vector2.hpp:71
constexpr Vector2 & operator/=(const Vector2 &rhs) noexcept
Divides by another vector component-wise.
Definition vector2.hpp:56
float y
Vector components.
Definition vector2.hpp:14
constexpr Vector2 & operator*=(const Vector2 &rhs) noexcept
Multiplies by another vector component-wise.
Definition vector2.hpp:48
constexpr Vector2 & operator/=(float rhs) noexcept
Divides both components by a scalar.
Definition vector2.hpp:83
constexpr Vector2 & operator+=(float rhs) noexcept
Adds a scalar to both components.
Definition vector2.hpp:65
constexpr Vector2() noexcept=default
Constructs a zero vector (x = y = 0).
#define CANGINE_VECTOR2_OP(OP)
Defines the free binary operator OP for Vector2.
Definition vector2.hpp:94