Warning
This page refers to an old version of SFML. Click here to switch to the latest version.
Warning
This page refers to an old version of SFML. Click here to switch to the latest version.
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!= |
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:
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.
|
constexprdefault |
Default constructor.
Creates a Vector2(0, 0).
|
constexpr |
Construct the vector from cartesian coordinates.
x | X coordinate |
y | Y coordinate |
sf::Vector2< T >::Vector2 | ( | T | r, |
Angle | phi ) |
Construct the vector from polar coordinates (floating-point)
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:
|
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.
|
nodiscard |
Signed angle from *this to rhs (floating-point).
|
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.
|
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.
|
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).
|
nodiscardconstexpr |
Dot product of two 2D vectors.
|
nodiscard |
Length of the vector (floating-point).
If you are not interested in the actual length, but only in comparisons, consider using lengthSquared().
|
nodiscardconstexpr |
Square of vector's length.
Suitable for comparisons, more efficient than length().
|
nodiscard |
Vector with same direction but length 1 (floating-point).
|
explicitconstexpr |
Converts the vector to another type of vector.
|
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.
|
nodiscard |
Projection of this vector onto axis (floating-point).
axis | Vector being projected onto. Need not be normalized. |
|
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.
Overload of binary operator!=
This operator compares strict difference between two vectors.
left | Left operand (a vector) |
right | Right operand (a vector) |
Overload of binary operator*
left | Left operand (a scalar value) |
right | Right operand (a vector) |
Overload of binary operator*
left | Left operand (a vector) |
right | Right operand (a scalar value) |
Overload of binary operator*=
This operator performs a member-wise multiplication by right, and assigns the result to left.
left | Left operand (a vector) |
right | Right operand (a scalar value) |
Overload of binary operator+
left | Left operand (a vector) |
right | Right operand (a vector) |
Overload of binary operator+=
This operator performs a member-wise addition of both vectors, and assigns the result to left.
left | Left operand (a vector) |
right | Right operand (a vector) |
Overload of binary operator-
left | Left operand (a vector) |
right | Right operand (a vector) |
Overload of unary operator-
right | Vector to negate |
Overload of binary operator-=
This operator performs a member-wise subtraction of both vectors, and assigns the result to left.
left | Left operand (a vector) |
right | Right operand (a vector) |
left
Overload of binary operator/
left | Left operand (a vector) |
right | Right operand (a scalar value) |
Overload of binary operator/=
This operator performs a member-wise division by right, and assigns the result to left.
left | Left operand (a vector) |
right | Right operand (a scalar value) |
Overload of binary operator==
This operator compares strict equality between two vectors.
left | Left operand (a vector) |
right | Right operand (a vector) |
T sf::Vector2< T >::x {} |
X coordinate of the vector.
Definition at line 203 of file Vector2.hpp.
T sf::Vector2< T >::y {} |
Y coordinate of the vector.
Definition at line 204 of file Vector2.hpp.