Euphoria
vec2.h
Go to the documentation of this file.
1#pragma once
2
3#include "base/angle.h"
4
5
6namespace eu
7{
12 struct v2;
13 struct n2;
14
16 struct v2
17 {
18 float x;
19 float y;
20
21 v2() = default;
22 constexpr v2(float ax, float ay)
23 : x(ax)
24 , y(ay)
25 {
26 }
27 explicit v2(const std::tuple<float, float>& a);
28 explicit v2(const n2& u);
29
30 [[nodiscard]] static v2 from_to(const v2& from, const v2& to);
31 [[nodiscard]] static v2 from(const n2& n);
32
36 bool normalize();
37
39 [[nodiscard]] v2 get_rotated(const An& a) const;
40
43 float* get_data_ptr();
44
46 [[nodiscard]] const float* get_data_ptr() const;
47
50
53 [[nodiscard]] float get_length_squared() const;
54
56 [[nodiscard]] float get_length() const;
57
61 [[nodiscard]] std::optional<n2> get_normalized() const;
62
63 void operator+=(const v2& rhs);
64 void operator-=(const v2& rhs);
65 v2 operator-() const;
66 void operator/=(float rhs);
67 void operator*=(float rhs);
68 void operator*=(const v2& rhs);
69 };
70
71 constexpr v2 zero2f = v2{ 0, 0 };
72
74 struct n2
75 {
76 float x;
77 float y;
78
80 explicit n2(float ax, float ay);
81
83 explicit n2(const v2& v);
84
86 [[nodiscard]] n2 get_rotated(const An& a) const;
87
89 [[nodiscard]] const float* get_data_ptr() const;
90
93
95 [[nodiscard]] bool is_valid() const;
96
97 n2 operator-() const;
98 };
99
100
101 v2 operator+(const v2& lhs, const v2& rhs);
102 v2 operator-(const v2& lhs, const v2& rhs);
103 v2 operator*(const v2& lhs, float rhs);
104 v2 operator*(const v2& lhs, const v2& rhs);
105 v2 operator*(float lhs, const v2& rhs);
106 v2 operator*(const n2& lhs, float rhs);
107 v2 operator*(float lhs, const n2& rhs);
108 v2 operator/(const v2& lhs, float rhs);
109
110
111 float dot(const v2& lhs, const v2& rhs);
112
113 v2 lerp_vec2f(const v2& from, float v, const v2& to);
114
116 std::string string_from(const v2& v);
117
119 std::string string_from(const n2& v);
120
125}
126
127ADD_DEFAULT_FORMATTER(eu::v2, std::string, eu::string_from);
128ADD_DEFAULT_FORMATTER(eu::n2, std::string, eu::string_from);
#define ADD_CATCH_FORMATTER_DEF(TYPE)
Definition pch.public.h:22
constexpr v2 zero2f
Definition vec2.h:71
An operator/(const An &lhs, float rhs)
An operator-(const An &lhs, const An &rhs)
v2 lerp_vec2f(const v2 &from, float v, const v2 &to)
float dot(const Q &lhs, const Q &rhs)
An operator+(const An &lhs, const An &rhs)
An operator*(const An &lhs, float rhs)
std::string string_from(const An &a)
Definition assert.h:111
An angle in both degrees and radians.
Definition angle.h:13
A (inclusive) range between two values.
Definition range.h:19
a 2d unit (vector)
Definition vec2.h:75
n2 operator-() const
n2 get_rotated(const An &a) const
Assumes the unit vector is a point and returns it if it was rotated around (0,0)
n2(float ax, float ay)
asserts that the length is 1
float y
Definition vec2.h:77
float x
Definition vec2.h:76
n2 get_flipped_y() const
Returns a new unit vector where the sign of the y component is flipped.
bool is_valid() const
returns false if the length isn't 1
const float * get_data_ptr() const
Returns an array to the data.
n2(const v2 &v)
asserts that the length is 1
a 2d vector
Definition vec2.h:17
std::optional< n2 > get_normalized() const
Changes the length to 1.
void operator/=(float rhs)
void operator*=(float rhs)
float get_length() const
Returns the length of the vector.
float get_length_squared() const
Returns the squared length of the vector.
const float * get_data_ptr() const
Returns an array to the data.
void operator-=(const v2 &rhs)
float * get_data_ptr()
Returns an array to the data.
float y
Definition vec2.h:19
v2 get_flipped_y() const
Returns a new vector where the sign of the y component is flipped.
constexpr v2(float ax, float ay)
Definition vec2.h:22
void operator+=(const v2 &rhs)
v2(const n2 &u)
static v2 from_to(const v2 &from, const v2 &to)
v2 get_rotated(const An &a) const
Assumes the vector is a point and returns it if it was rotated around (0,0)
float x
Definition vec2.h:18
v2()=default
v2 operator-() const
static v2 from(const n2 &n)
v2(const std::tuple< float, float > &a)
void operator*=(const v2 &rhs)
bool normalize()
Changes the length to 1.