12#ifndef JAU_MATH_VEC2F_HPP_
13#define JAU_MATH_VEC2F_HPP_
39 template <jau::req::packed_
floating_po
int Value_type>
66 return Vector2F(magnitude * std::cos(radians), magnitude * std::sin(radians));
100 explicit operator pointer() noexcept {
return &
x; }
122 {
x=vx;
y=vy;
return *
this; }
126 {
x=xy[0];
y=xy[1];
return *
this; }
130 {
x+=d.x;
y+=d.y;
return *
this; }
134 {
x+=dx;
y+=dy;
return *
this; }
138 {
x*=s.x;
y*=s.y;
return *
this; }
142 {
x*=sx;
y*=sy;
return *
this; }
146 {
x*=s;
y*=s;
return *
this; }
201 return rotate(std::sin(radians), std::cos(radians), ctr);
208 x = x0 * cos - y0 * sin + ctr.x;
209 y = x0 * sin + y0 * cos + ctr.y;
215 return rotate(std::sin(radians), std::cos(radians));
221 x = x0 * cos -
y * sin;
222 y = x0 * sin +
y * cos;
226 std::string
toString() const noexcept {
return std::to_string(
x)+
", "+std::to_string(
y); }
265 return std::atan2(
y,
x );
278 return dx*dx + dy*dy;
293 return x*o.x +
y*o.y;
304 return x * o.y -
y * o.x;
311 return dot(o) / (
length() * o.length() ) ;
329 const value_type eps = std::numeric_limits<value_type>::epsilon();
330 if( std::abs(
x-o.x) >= eps || std::abs(
y-o.y) >= eps ) {
337 template <jau::req::packed_
floating_po
int T>
348 template <jau::req::packed_
floating_po
int T>
353 template <jau::req::packed_
floating_po
int T>
360 template <jau::req::packed_
floating_po
int T>
365 template <jau::req::packed_
floating_po
int T>
370 template <jau::req::packed_
floating_po
int T>
375 template <jau::req::packed_
floating_po
int T>
378 r.
x=s/r.
x; r.
y=s/r.
y;
382 template <jau::req::packed_
floating_po
int T>
384 return Vector2F<T>(std::min(lhs.x, rhs.x), std::min(lhs.y, rhs.y));
387 template <jau::req::packed_
floating_po
int T>
389 return Vector2F<T>(std::max(lhs.x, rhs.x), std::max(lhs.y, rhs.y));
392 template <jau::req::packed_
floating_po
int T>
394 return Vector2F<T>(std::abs(lhs.x), std::abs(lhs.y));
397 template <jau::req::packed_
floating_po
int T>
399 return out << v.toString();
402 static_assert(
sizeof(float) ==
alignof(float));
407 static_assert(
sizeof(float) ==
alignof(
Vec2f));
409 static_assert(
sizeof(float)*2 ==
sizeof(
Vec2f));
414 template <jau::req::packed_
floating_po
int T>
420 static_assert(
sizeof(float) ==
alignof(
Point2f));
422 static_assert(
sizeof(float)*2 ==
sizeof(
Point2f));
436 template <jau::req::packed_
floating_po
int Value_type>
437 class alignas(sizeof(Value_type))
Ray2F {
458 std::string
toString() const noexcept {
return "Ray[orig "+
orig.toString()+
", dir "+
dir.toString() +
"]"; }
461 template <jau::req::packed_
floating_po
int T>
463 return out << v.toString();
469 static_assert(
sizeof(float) ==
alignof(
Ray2f));
471 static_assert(
sizeof(float)*4 ==
sizeof(
Ray2f));
Simple compound denoting a ray.
static constexpr const size_t byte_size
static constexpr const size_t components
static constexpr int value_alignment
const value_type * const_pointer
Point2F< value_type > orig
std::string toString() const noexcept
Vector2F< value_type > dir
2D vector using two value_type components.
constexpr value_type length() const noexcept
Return the length of this vector, a.k.a the norm or magnitude
constexpr Vector2F() noexcept
constexpr value_type cross(const Vector2F &o) const noexcept
Returns cross product of this vectors and the given one, i.e.
constexpr Vector2F & mul(const Vector2F &s) noexcept
this = this * {s.x, s.y}, component wise.
constexpr_cxx26 Vector2F & rotate(const value_type radians, const Vector2F &ctr) noexcept
Rotates this vector in place, returns *this.
constexpr reference operator[](size_t i) noexcept
Returns writeable reference to component.
constexpr iterator get(iterator xy) const noexcept
xy = this, returns xy.
constexpr Vector2F & add(const Vector2F &d) noexcept
this = this + {d.x, d.y}, component wise.
constexpr Vector2F & operator/=(const value_type s) noexcept
this = this / s, component wise.
constexpr Vector2F & set(const_iterator xy) noexcept
this = xy, returns this.
static constexpr int value_alignment
constexpr_cxx23 bool intersects(const Vector2F &o) const noexcept
constexpr Vector2F & operator-=(const Vector2F &rhs) noexcept
this = this - rhs, component wise.
const value_type & const_reference
constexpr Vector2F(const value_type v) noexcept
constexpr Vector2F & operator=(const Vector2F &) noexcept=default
constexpr bool operator==(const Vector2F &rhs) const noexcept
static constexpr const size_t byte_size
constexpr Vector2F & rotate(const value_type sin, const value_type cos, const Vector2F &ctr) noexcept
Rotates this vector in place, returns *this.
constexpr Vector2F & scale(const value_type s) noexcept
this = this * s, component wise.
constexpr Vector2F & add(const value_type dx, const value_type dy) noexcept
this = this + {dx, dy}, component wise.
constexpr Vector2F(const value_type x_, const value_type y_) noexcept
static constexpr const value_type zero
constexpr value_type cos_angle(const Vector2F &o) const noexcept
Return the cosines of the angle between two vectors.
constexpr value_type dist(const Vector2F &o) const noexcept
Return the distance between this vector and the given one.
constexpr Vector2F & mul(const value_type sx, const value_type sy) noexcept
this = this * {sx, sy}, component wise.
constexpr_cxx26 Vector2F & rotate(const value_type radians) noexcept
Rotates this vector in place, returns *this.
constexpr Vector2F normal_ccw() const noexcept
Return the counter-clock-wise (CCW) normal of this vector, i.e.
static constexpr_cxx26 Vector2F from_length_angle(const value_type magnitude, const value_type radians) noexcept
constexpr const_iterator cbegin() const noexcept
constexpr Vector2F & rotate(const value_type sin, const value_type cos) noexcept
Rotates this vector in place, returns *this.
constexpr value_type dot(const Vector2F &o) const noexcept
Return the dot product of this vector and the given one.
constexpr value_type length_sq() const noexcept
Return the squared length of this vector, a.k.a the squared norm or squared magnitude
constexpr Vector2F(Vector2F &&o) noexcept=default
constexpr Vector2F & operator/=(const Vector2F &s) noexcept
this = this / {s.x, s.y}, component wise.
std::string toString() const noexcept
constexpr Vector2F copy() noexcept
constexpr Vector2F & set(const value_type vx, const value_type vy) noexcept
TODO constexpr std::strong_ordering operator<=>(const vec2f_t& rhs) const noexcept { return ....
constexpr Vector2F & operator*=(const value_type s) noexcept
this = this * s, component wise.
constexpr_cxx26 value_type angle(const Vector2F &o) const noexcept
Return the angle between two vectors in radians.
static constexpr const size_t components
static constexpr const value_type one
constexpr Vector2F(const Vector2F &o) noexcept=default
constexpr_cxx26 value_type angle() const noexcept
Return the direction angle of this vector in radians.
constexpr Vector2F & operator=(Vector2F &&) noexcept=default
constexpr Vector2F & operator+=(const Vector2F &rhs) noexcept
this = this + rhs, component wise.
constexpr value_type operator[](size_t i) const noexcept
Returns read-only component.
constexpr bool is_zero() const noexcept
constexpr Vector2F & normalize() noexcept
Normalize this vector in place, returns *this.
constexpr Vector2F & operator*=(const Vector2F &s) noexcept
this = this * {s.x, s.y}, component wise.
const value_type * const_pointer
constexpr value_type dist_sq(const Vector2F &o) const noexcept
Return the squared distance between this vector and the given one.
const value_type * const_iterator
constexpr iterator begin() noexcept
constexpr bool is_zero(const T &a, const T &epsilon=std::numeric_limits< T >::epsilon()) noexcept
Returns true if the given value is less than epsilon, w/ epsilon > 0.
constexpr Quaternion< T > operator-(const Quaternion< T > &lhs, const Quaternion< T > &rhs) noexcept
constexpr Matrix4< T > operator*(const Matrix4< T > &lhs, const Matrix4< T > &rhs) noexcept
constexpr Vector2F< T > abs(const Vector2F< T > &lhs) noexcept
constexpr Quaternion< T > operator+(const Quaternion< T > &lhs, const Quaternion< T > &rhs) noexcept
constexpr Vector2F< T > max(const Vector2F< T > &lhs, const Vector2F< T > &rhs) noexcept
std::ostream & operator<<(std::ostream &out, const Matrix4< T > &v) noexcept
constexpr Vector2F< T > min(const Vector2F< T > &lhs, const Vector2F< T > &rhs) noexcept
Vector2F< T > Point2F
Point2F alias of Vector2F.
constexpr Vector2F< T > operator/(const Vector2F< T > &lhs, const T s) noexcept