17# pragma GCC diagnostic push
18# pragma GCC diagnostic ignored "-Wpedantic"
21# pragma clang diagnostic push
22# pragma clang diagnostic ignored "-Wgnu-anonymous-struct"
23# pragma clang diagnostic ignored "-Wnested-anon-types"
26#include <vsg/core/type_name.h>
27#include <vsg/maths/numbers.h>
61 constexpr t_vec2(
const t_vec2& v) :
63 constexpr t_vec2& operator=(
const t_vec2&) =
default;
64 constexpr t_vec2(value_type in_x, value_type in_y) :
68 constexpr explicit t_vec2(
const t_vec2<R>& v) :
69 value{
static_cast<T
>(v.x),
static_cast<T
>(v.y)} {}
71 constexpr std::size_t size()
const {
return 2; }
73 value_type& operator[](std::size_t i) {
return value[i]; }
74 value_type operator[](std::size_t i)
const {
return value[i]; }
77 t_vec2& operator=(
const t_vec2<R>& rhs)
79 value[0] =
static_cast<value_type
>(rhs[0]);
80 value[1] =
static_cast<value_type
>(rhs[1]);
84 T* data() {
return value; }
85 const T* data()
const {
return value; }
87 void set(value_type in_x, value_type in_y)
93 inline t_vec2& operator+=(
const t_vec2& rhs)
95 value[0] += rhs.value[0];
96 value[1] += rhs.value[1];
100 inline t_vec2& operator-=(
const t_vec2& rhs)
102 value[0] -= rhs.value[0];
103 value[1] -= rhs.value[1];
107 inline t_vec2& operator*=(value_type rhs)
114 inline t_vec2& operator*=(
const t_vec2& rhs)
116 value[0] *= rhs.value[0];
117 value[1] *= rhs.value[1];
121 friend constexpr t_vec2<T> operator*(
const t_vec2<T>& lhs, T rhs)
123 return t_vec2<T>(lhs[0] * rhs, lhs[1] * rhs);
126 friend constexpr t_vec2<T> operator*(T lhs,
const t_vec2<T>& rhs)
128 return t_vec2<T>(lhs * rhs[0], lhs * rhs[1]);
131 inline t_vec2& operator/=(value_type rhs)
133 if constexpr (std::is_floating_point_v<value_type>)
135 value_type inv = numbers<value_type>::one() / rhs;
147 explicit operator bool()
const noexcept {
return value[0] != numbers<value_type>::zero() || value[1] != numbers<value_type>::zero(); }
160 VSG_type_name(vsg::vec2);
161 VSG_type_name(vsg::dvec2);
162 VSG_type_name(vsg::bvec2);
163 VSG_type_name(vsg::svec2);
164 VSG_type_name(vsg::ivec2);
165 VSG_type_name(vsg::ubvec2);
166 VSG_type_name(vsg::usvec2);
167 VSG_type_name(vsg::uivec2);
172 return lhs[0] == rhs[0] && lhs[1] == rhs[1];
176 constexpr bool operator!=(
const t_vec2<T>& lhs,
const t_vec2<T>& rhs)
178 return lhs[0] != rhs[0] || lhs[1] != rhs[1];
184 if (lhs[0] < rhs[0])
return true;
185 if (lhs[0] > rhs[0])
return false;
186 return lhs[1] < rhs[1];
192 return t_vec2<T>(lhs[0] - rhs[0], lhs[1] - rhs[1]);
204 return t_vec2<T>(lhs[0] + rhs[0], lhs[1] + rhs[1]);
210 return t_vec2<T>(lhs[0] * rhs[0], lhs[1] * rhs[1]);
216 if constexpr (std::is_floating_point_v<T>)
218 T inv = numbers<T>::one() / rhs;
219 return t_vec2<T>(lhs[0] * inv, lhs[1] * inv);
223 return t_vec2<T>(lhs[0] / rhs, lhs[1] / rhs);
230 return std::sqrt(v[0] * v[0] + v[1] * v[1]);
236 return v[0] * v[0] + v[1] * v[1];
242 return v / length(v);
248 return lhs[0] * rhs[0] + lhs[1] * rhs[1];
256 return (lhs[0] * rhs[1] - rhs[0] * lhs[1]);
262 T one_minus_r = numbers<T>::one() - r;
263 return t_vec2<T>(start[0] * one_minus_r + end[0] * r,
264 start[1] * one_minus_r + end[1] * r);
269#if defined(__clang__)
270# pragma clang diagnostic pop
273# pragma GCC diagnostic pop
t_vec2 template class that represents a 2D vector
Definition vec2.h:39