Loading...
Searching...
No Matches
Vector2.hpp
Go to the documentation of this file.
1
2//
3// SFML - Simple and Fast Multimedia Library
4// Copyright (C) 2007-2024 Laurent Gomila (laurent@sfml-dev.org)
5//
6// This software is provided 'as-is', without any express or implied warranty.
7// In no event will the authors be held liable for any damages arising from the use of this software.
8//
9// Permission is granted to anyone to use this software for any purpose,
10// including commercial applications, and to alter it and redistribute it freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
25#pragma once
26
28
29#include <SFML/System/Angle.hpp>
30
31
32namespace sf
33{
39template <typename T>
41{
42public:
49 constexpr Vector2() = default;
50
58 constexpr Vector2(T x, T y);
59
71 template <typename U>
72 constexpr explicit Vector2(Vector2<U> vector);
73
89
96 [[nodiscard]] SFML_SYSTEM_API T length() const;
97
104 [[nodiscard]] constexpr T lengthSquared() const;
105
112 [[nodiscard]] SFML_SYSTEM_API Vector2 normalized() const;
113
123 [[nodiscard]] SFML_SYSTEM_API Angle angleTo(Vector2 rhs) const;
124
134 [[nodiscard]] SFML_SYSTEM_API Angle angle() const;
135
145 [[nodiscard]] SFML_SYSTEM_API Vector2 rotatedBy(Angle phi) const;
146
154 [[nodiscard]] SFML_SYSTEM_API Vector2 projectedOnto(Vector2 axis) const;
155
166 [[nodiscard]] constexpr Vector2 perpendicular() const;
167
172 [[nodiscard]] constexpr T dot(Vector2 rhs) const;
173
181 [[nodiscard]] constexpr T cross(Vector2 rhs) const;
182
192 [[nodiscard]] constexpr Vector2 componentWiseMul(Vector2 rhs) const;
193
204 [[nodiscard]] constexpr Vector2 componentWiseDiv(Vector2 rhs) const;
205
206
208 // Member data
210 T x{};
211 T y{};
212
213
215 // Static member data
217 // NOLINTBEGIN(readability-identifier-naming)
218 static const Vector2 UnitX;
219 static const Vector2 UnitY;
220 // NOLINTEND(readability-identifier-naming)
221};
222
223// Define the most common types
227
237template <typename T>
238[[nodiscard]] constexpr Vector2<T> operator-(Vector2<T> right);
239
253template <typename T>
255
269template <typename T>
271
282template <typename T>
283[[nodiscard]] constexpr Vector2<T> operator+(Vector2<T> left, Vector2<T> right);
284
295template <typename T>
296[[nodiscard]] constexpr Vector2<T> operator-(Vector2<T> left, Vector2<T> right);
297
308template <typename T>
309[[nodiscard]] constexpr Vector2<T> operator*(Vector2<T> left, T right);
310
321template <typename T>
322[[nodiscard]] constexpr Vector2<T> operator*(T left, Vector2<T> right);
323
337template <typename T>
338constexpr Vector2<T>& operator*=(Vector2<T>& left, T right);
339
350template <typename T>
351[[nodiscard]] constexpr Vector2<T> operator/(Vector2<T> left, T right);
352
366template <typename T>
367constexpr Vector2<T>& operator/=(Vector2<T>& left, T right);
368
381template <typename T>
382[[nodiscard]] constexpr bool operator==(Vector2<T> left, Vector2<T> right);
383
396template <typename T>
397[[nodiscard]] constexpr bool operator!=(Vector2<T> left, Vector2<T> right);
398
399} // namespace sf
400
401#include <SFML/System/Vector2.inl>
402
403
#define SFML_SYSTEM_API
Represents an angle value.
Definition Angle.hpp:35
Class template for manipulating 2-dimensional vectors.
Definition Vector2.hpp:41
constexpr Vector2(T x, T y)
Construct the vector from cartesian coordinates.
constexpr T cross(Vector2 rhs) const
Z component of the cross product of two 2D vectors.
constexpr Vector2(Vector2< U > vector)
Construct the vector from another type of vector.
T length() const
Length of the vector (floating-point).
Vector2(T r, Angle phi)
Construct the vector from polar coordinates (floating-point)
T x
X coordinate of the vector.
Definition Vector2.hpp:210
constexpr Vector2 componentWiseDiv(Vector2 rhs) const
Component-wise division of *this and `rhs`.
constexpr Vector2()=default
Default constructor.
constexpr Vector2< T > operator+(Vector2< T > left, Vector2< T > right)
Overload of binary operator+
T y
Y coordinate of the vector.
Definition Vector2.hpp:211
constexpr Vector2 perpendicular() const
Returns a perpendicular vector.
constexpr T lengthSquared() const
Square of vector's length.
Vector2 projectedOnto(Vector2 axis) const
Projection of this vector onto `axis` (floating-point).
Vector2 rotatedBy(Angle phi) const
Rotate by angle phi (floating-point).
Angle angleTo(Vector2 rhs) const
Signed angle from *this to `rhs` (floating-point).
constexpr Vector2< T > operator-(Vector2< T > right)
Overload of unary operator-
constexpr bool operator==(Vector2< T > left, Vector2< T > right)
Overload of binary operator==
constexpr Vector2< T > operator-(Vector2< T > left, Vector2< T > right)
Overload of binary operator-
constexpr Vector2< T > & operator*=(Vector2< T > &left, T right)
Overload of binary operator*=
static const Vector2 UnitY
The Y unit vector (0, 1), usually facing down.
Definition Vector2.hpp:219
constexpr Vector2< T > & operator+=(Vector2< T > &left, Vector2< T > right)
Overload of binary operator+=
constexpr Vector2< T > & operator-=(Vector2< T > &left, Vector2< T > right)
Overload of binary operator-=
static const Vector2 UnitX
The X unit vector (1, 0), usually facing right.
Definition Vector2.hpp:218
Vector2 normalized() const
Vector with same direction but length 1 (floating-point).
constexpr Vector2 componentWiseMul(Vector2 rhs) const
Component-wise multiplication of *this and `rhs`.
Angle angle() const
Signed angle from +X or (1,0) vector (floating-point).
constexpr Vector2< T > & operator/=(Vector2< T > &left, T right)
Overload of binary operator/=
constexpr T dot(Vector2 rhs) const
Dot product of two 2D vectors.
constexpr bool operator!=(Vector2< T > left, Vector2< T > right)
Overload of binary operator!=
constexpr Vector2< T > operator*(Vector2< T > left, T right)
Overload of binary operator*
constexpr Vector2< T > operator*(T left, Vector2< T > right)
Overload of binary operator*
constexpr Vector2< T > operator/(Vector2< T > left, T right)
Overload of binary operator/