Class template for manipulating 2-dimensional vectors. More...
#include <SFML/System/Vector2.hpp>
Public Member Functions | |
| constexpr | Vector2 ()=default |
| Default constructor. | |
| constexpr | Vector2 (T x, T y) |
| Construct the vector from cartesian coordinates. | |
| template<typename U> | |
| constexpr | operator Vector2< U > () const |
| Converts the vector to another type of vector. | |
| Vector2 (T r, Angle phi) | |
| Construct the vector from polar coordinates (floating-point) | |
| T | length () const |
| Length of the vector (floating-point). | |
| constexpr T | lengthSquared () const |
| Square of vector's length. | |
| Vector2 | normalized () const |
| Vector with same direction but length 1 (floating-point). | |
| Angle | angleTo (Vector2 rhs) const |
Signed angle from *this to rhs (floating-point). | |
| Angle | angle () const |
| Signed angle from +X or (1,0) vector (floating-point). | |
| Vector2 | rotatedBy (Angle phi) const |
Rotate by angle phi (floating-point). | |
| Vector2 | projectedOnto (Vector2 axis) const |
Projection of this vector onto axis (floating-point). | |
| constexpr Vector2 | perpendicular () const |
| Returns a perpendicular vector. | |
| constexpr T | dot (Vector2 rhs) const |
| Dot product of two 2D vectors. | |
| constexpr T | cross (Vector2 rhs) const |
| Z component of the cross product of two 2D vectors. | |
| constexpr Vector2 | componentWiseMul (Vector2 rhs) const |
Component-wise multiplication of *this and rhs. | |
| constexpr Vector2 | componentWiseDiv (Vector2 rhs) const |
Component-wise division of *this and rhs. | |
Public Attributes | |
| T | x {} |
| X coordinate of the vector. | |
| T | y {} |
| Y coordinate of the vector. | |
Related Symbols | |
(Note that these are not member symbols.) | |
| template<typename T> | |
| constexpr Vector2< T > | operator- (Vector2< T > right) |
Overload of unary operator- | |
| template<typename T> | |
| constexpr Vector2< T > & | operator+= (Vector2< T > &left, Vector2< T > right) |
Overload of binary operator+= | |
| template<typename T> | |
| constexpr Vector2< T > & | operator-= (Vector2< T > &left, Vector2< T > right) |
Overload of binary operator-= | |
| template<typename T> | |
| constexpr Vector2< T > | operator+ (Vector2< T > left, Vector2< T > right) |
Overload of binary operator+ | |
| template<typename T> | |
| constexpr Vector2< T > | operator- (Vector2< T > left, Vector2< T > right) |
Overload of binary operator- | |
| template<typename T> | |
| constexpr Vector2< T > | operator* (Vector2< T > left, T right) |
Overload of binary operator* | |
| template<typename T> | |
| constexpr Vector2< T > | operator* (T left, Vector2< T > right) |
Overload of binary operator* | |
| template<typename T> | |
| constexpr Vector2< T > & | operator*= (Vector2< T > &left, T right) |
Overload of binary operator*= | |
| template<typename T> | |
| constexpr Vector2< T > | operator/ (Vector2< T > left, T right) |
Overload of binary operator/ | |
| template<typename T> | |
| constexpr Vector2< T > & | operator/= (Vector2< T > &left, T right) |
Overload of binary operator/= | |
| template<typename T> | |
| constexpr bool | operator== (Vector2< T > left, Vector2< T > right) |
Overload of binary operator== | |
| template<typename T> | |
| constexpr bool | operator!= (Vector2< T > left, Vector2< T > right) |
Overload of binary operator!= | |
Detailed Description
class sf::Vector2< T >
Class template for manipulating 2-dimensional vectors.
sf::Vector2 is a simple class that defines a mathematical vector with two coordinates (x and y).
It can be used to represent anything that has two dimensions: a size, a point, a velocity, a scale, etc.
The API provides basic arithmetic (addition, subtraction, scale), as well as more advanced geometric operations, such as dot/cross products, length and angle computations, projections, rotations, etc.
The template parameter T is the type of the coordinates. It can be any type that supports arithmetic operations (+, -, /, *) and comparisons (==, !=), for example int or float. Note that some operations are only meaningful for vectors where T is a floating point type (e.g. float or double), often because results cannot be represented accurately with integers. The method documentation mentions "(floating-point)" in those cases.
You generally don't have to care about the templated form (sf::Vector2<T>), the most common specializations have special type aliases:
sf::Vector2<float>issf::Vector2fsf::Vector2<int>issf::Vector2isf::Vector2<unsigned int>issf::Vector2u
The sf::Vector2 class has a simple interface, its x and y members can be accessed directly (there are no accessors like setX(), getX()).
Usage example:
Note: for 3-dimensional vectors, see sf::Vector3.
Definition at line 40 of file Vector2.hpp.
Constructor & Destructor Documentation
◆ Vector2() [1/3]
|
constexprdefault |
Default constructor.
Creates a Vector2(0, 0).
◆ Vector2() [2/3]
|
constexpr |
Construct the vector from cartesian coordinates.
- Parameters
-
x X coordinate y Y coordinate
◆ Vector2() [3/3]
| sf::Vector2< T >::Vector2 | ( | T | r, |
| Angle | phi ) |
Construct the vector from polar coordinates (floating-point)
- Parameters
-
r Length of vector (can be negative) phi Angle from X axis
Note that this constructor is lossy: calling length() and angle() may return values different to those provided in this constructor.
In particular, these transforms can be applied:
Member Function Documentation
◆ angle()
|
nodiscard |
Signed angle from +X or (1,0) vector (floating-point).
For example, the vector (1,0) corresponds to 0 degrees, (0,1) corresponds to 90 degrees.
- Returns
- Angle in the range [-180, 180) degrees.
- Precondition
- This vector is no zero vector.
◆ angleTo()
|
nodiscard |
Signed angle from *this to rhs (floating-point).
- Returns
- The smallest angle which rotates
*thisin positive or negative direction, until it has the same direction asrhs. The result has a sign and lies in the range [-180, 180) degrees.
- Precondition
- Neither
*thisnorrhsis a zero vector.
◆ componentWiseDiv()
|
nodiscardconstexpr |
Component-wise division of *this and rhs.
Computes (lhs.x/rhs.x, lhs.y/rhs.y).
Scaling is the most common use case for component-wise multiplication/division.
- Precondition
- Neither component of
rhsis zero.
◆ componentWiseMul()
|
nodiscardconstexpr |
Component-wise multiplication of *this and rhs.
Computes (lhs.x*rhs.x, lhs.y*rhs.y).
Scaling is the most common use case for component-wise multiplication/division. This operation is also known as the Hadamard or Schur product.
◆ cross()
|
nodiscardconstexpr |
Z component of the cross product of two 2D vectors.
Treats the operands as 3D vectors, computes their cross product and returns the result's Z component (X and Y components are always zero).
◆ dot()
|
nodiscardconstexpr |
Dot product of two 2D vectors.
◆ length()
|
nodiscard |
Length of the vector (floating-point).
If you are not interested in the actual length, but only in comparisons, consider using lengthSquared().
◆ lengthSquared()
|
nodiscardconstexpr |
Square of vector's length.
Suitable for comparisons, more efficient than length().
◆ normalized()
|
nodiscard |
Vector with same direction but length 1 (floating-point).
- Precondition
*thisis no zero vector.
◆ operator Vector2< U >()
|
explicitconstexpr |
Converts the vector to another type of vector.
◆ perpendicular()
|
nodiscardconstexpr |
Returns a perpendicular vector.
Returns *this rotated by +90 degrees; (x,y) becomes (-y,x). For example, the vector (1,0) is transformed to (0,1).
In SFML's default coordinate system with +X right and +Y down, this amounts to a clockwise rotation.
◆ projectedOnto()
|
nodiscard |
Projection of this vector onto axis (floating-point).
- Parameters
-
axis Vector being projected onto. Need not be normalized.
- Precondition
axismust not have length zero.
◆ rotatedBy()
|
nodiscard |
Rotate by angle phi (floating-point).
Returns a vector with same length but different direction.
In SFML's default coordinate system with +X right and +Y down, this amounts to a clockwise rotation by phi.
Friends And Related Symbol Documentation
◆ operator!=()
Overload of binary operator!=
This operator compares strict difference between two vectors.
- Parameters
-
left Left operand (a vector) right Right operand (a vector)
- Returns
trueifleftis not equal toright
◆ operator*() [1/2]
Overload of binary operator*
- Parameters
-
left Left operand (a scalar value) right Right operand (a vector)
- Returns
- Member-wise multiplication by
left
◆ operator*() [2/2]
Overload of binary operator*
- Parameters
-
left Left operand (a vector) right Right operand (a scalar value)
- Returns
- Member-wise multiplication by
right
◆ operator*=()
Overload of binary operator*=
This operator performs a member-wise multiplication by right, and assigns the result to left.
- Parameters
-
left Left operand (a vector) right Right operand (a scalar value)
- Returns
- Reference to
left
◆ operator+()
Overload of binary operator+
- Parameters
-
left Left operand (a vector) right Right operand (a vector)
- Returns
- Member-wise addition of both vectors
◆ operator+=()
Overload of binary operator+=
This operator performs a member-wise addition of both vectors, and assigns the result to left.
- Parameters
-
left Left operand (a vector) right Right operand (a vector)
- Returns
- Reference to
left
◆ operator-() [1/2]
Overload of binary operator-
- Parameters
-
left Left operand (a vector) right Right operand (a vector)
- Returns
- Member-wise subtraction of both vectors
◆ operator-() [2/2]
Overload of unary operator-
- Parameters
-
right Vector to negate
- Returns
- Member-wise opposite of the vector
◆ operator-=()
Overload of binary operator-=
This operator performs a member-wise subtraction of both vectors, and assigns the result to left.
- Parameters
-
left Left operand (a vector) right Right operand (a vector)
- Returns
- Reference to
left
◆ operator/()
Overload of binary operator/
- Parameters
-
left Left operand (a vector) right Right operand (a scalar value)
- Returns
- Member-wise division by
right
◆ operator/=()
Overload of binary operator/=
This operator performs a member-wise division by right, and assigns the result to left.
- Parameters
-
left Left operand (a vector) right Right operand (a scalar value)
- Returns
- Reference to
left
◆ operator==()
Overload of binary operator==
This operator compares strict equality between two vectors.
- Parameters
-
left Left operand (a vector) right Right operand (a vector)
- Returns
trueifleftis equal toright
Member Data Documentation
◆ x
| T sf::Vector2< T >::x {} |
X coordinate of the vector.
Definition at line 203 of file Vector2.hpp.
◆ y
| T sf::Vector2< T >::y {} |
Y coordinate of the vector.
Definition at line 204 of file Vector2.hpp.
The documentation for this class was generated from the following file: