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 };
69
70 constexpr v2 zero2f = v2{ 0, 0 };
71
73 struct n2
74 {
75 float x;
76 float y;
77
79 explicit n2(float ax, float ay);
80
82 explicit n2(const v2& v);
83
85 [[nodiscard]] n2 get_rotated(const An& a) const;
86
88 [[nodiscard]] const float* get_data_ptr() const;
89
92
94 [[nodiscard]] bool is_valid() const;
95
96 n2 operator-() const;
97 };
98
99
100 v2 operator+(const v2& lhs, const v2& rhs);
101 v2 operator-(const v2& lhs, const v2& rhs);
102 v2 operator*(const v2& lhs, float rhs);
103 v2 operator*(float lhs, const v2& rhs);
104 v2 operator*(const n2& lhs, float rhs);
105 v2 operator*(float lhs, const n2& rhs);
106 v2 operator/(const v2& lhs, float rhs);
107
108
109 float dot(const v2& lhs, const v2& rhs);
110
111 v2 lerp_vec2f(const v2& from, float v, const v2& to);
112
114 std::string string_from(const v2& v);
115
117 std::string string_from(const n2& v);
118
123}
124
125ADD_DEFAULT_FORMATTER(eu::v2, std::string, eu::string_from);
126ADD_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:70
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:109
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:74
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:76
float x
Definition vec2.h:75
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)
bool normalize()
Changes the length to 1.