17 # pragma GCC diagnostic push
18 # pragma GCC diagnostic ignored "-Wpedantic"
20 #if defined(__clang__)
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>
61 constexpr
t_vec2(value_type in_x, value_type in_y) :
66 value{
static_cast<T
>(v.x),
static_cast<T
>(v.y)} {}
68 constexpr std::size_t size()
const {
return 2; }
70 value_type& operator[](std::size_t i) {
return value[i]; }
71 value_type operator[](std::size_t i)
const {
return value[i]; }
76 value[0] =
static_cast<value_type
>(rhs[0]);
77 value[1] =
static_cast<value_type
>(rhs[1]);
81 T* data() {
return value; }
82 const T* data()
const {
return value; }
84 void set(value_type in_x, value_type in_y)
92 value[0] += rhs.value[0];
93 value[1] += rhs.value[1];
99 value[0] -= rhs.value[0];
100 value[1] -= rhs.value[1];
104 inline t_vec2& operator*=(value_type rhs)
113 value[0] *= rhs.value[0];
114 value[1] *= rhs.value[1];
118 inline t_vec2& operator/=(value_type rhs)
120 if constexpr (std::is_floating_point_v<value_type>)
122 value_type inv =
static_cast<value_type
>(1.0) / rhs;
156 return lhs[0] == rhs[0] && lhs[1] == rhs[1];
160 constexpr
bool operator!=(
const t_vec2<T>& lhs,
const t_vec2<T>& rhs)
162 return lhs[0] != rhs[0] || lhs[1] != rhs[1];
166 constexpr
bool operator<(
const t_vec2<T>& lhs,
const t_vec2<T>& rhs)
168 if (lhs[0] < rhs[0])
return true;
169 if (lhs[0] > rhs[0])
return false;
170 return lhs[1] < rhs[1];
174 constexpr t_vec2<T> operator-(
const t_vec2<T>& lhs,
const t_vec2<T>& rhs)
176 return t_vec2<T>(lhs[0] - rhs[0], lhs[1] - rhs[1]);
180 constexpr t_vec2<T> operator-(
const t_vec2<T>& v)
182 return t_vec2<T>(-v[0], -v[1]);
186 constexpr t_vec2<T> operator+(
const t_vec2<T>& lhs,
const t_vec2<T>& rhs)
188 return t_vec2<T>(lhs[0] + rhs[0], lhs[1] + rhs[1]);
192 constexpr t_vec2<T> operator*(
const t_vec2<T>& lhs, T rhs)
194 return t_vec2<T>(lhs[0] * rhs, lhs[1] * rhs);
198 constexpr t_vec2<T> operator*(
const t_vec2<T>& lhs,
const t_vec2<T>& rhs)
200 return t_vec2<T>(lhs[0] * rhs[0], lhs[1] * rhs[1]);
204 constexpr t_vec2<T> operator/(
const t_vec2<T>& lhs, T rhs)
206 if constexpr (std::is_floating_point_v<T>)
208 T inv =
static_cast<T
>(1.0) / rhs;
209 return t_vec2<T>(lhs[0] * inv, lhs[1] * inv);
213 return t_vec2<T>(lhs[0] / rhs, lhs[1] / rhs);
218 constexpr T length(
const t_vec2<T>& v)
220 return std::sqrt(v[0] * v[0] + v[1] * v[1]);
224 constexpr T length2(
const t_vec2<T>& v)
226 return v[0] * v[0] + v[1] * v[1];
230 constexpr t_vec2<T> normalize(
const t_vec2<T>& v)
232 return v / length(v);
236 constexpr T dot(
const t_vec2<T>& lhs,
const t_vec2<T>& rhs)
238 return lhs[0] * rhs[0] + lhs[1] * rhs[1];
244 constexpr T cross(
const t_vec2<T>& lhs,
const t_vec2<T>& rhs)
246 return (lhs[0] * rhs[1] - rhs[0] * lhs[1]);
250 constexpr t_vec2<T> mix(
const t_vec2<T>& start,
const t_vec2<T>& end, T r)
252 T one_minus_r = 1 - r;
253 return t_vec2<T>(start[0] * one_minus_r + end[0] * r,
254 start[1] * one_minus_r + end[1] * r);
259 #if defined(__clang__)
260 # pragma clang diagnostic pop
262 #if defined(__GNUC__)
263 # pragma GCC diagnostic pop
t_vec2 template class that a represents a 2D vector
Definition: vec2.h:36