Loading...
Searching...
No Matches

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)
 
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

x {}
 X coordinate of the vector.
 
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

template<typename T>
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:

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:

sf::Vector2f v(16.5f, 24.f);
v.x = 18.2f;
float y = v.y;
sf::Vector2f w = v * 5.f;
u = v + w;
float s = v.dot(w);
bool different = (v != u);
T y
Y coordinate of the vector.
Definition Vector2.hpp:204
constexpr T dot(Vector2 rhs) const
Dot product of two 2D vectors.

Note: for 3-dimensional vectors, see sf::Vector3.

Definition at line 40 of file Vector2.hpp.

Constructor & Destructor Documentation

◆ Vector2() [1/3]

template<typename T >
sf::Vector2< T >::Vector2 ( )
constexprdefault

Default constructor.

Creates a Vector2(0, 0).

◆ Vector2() [2/3]

template<typename T >
sf::Vector2< T >::Vector2 ( T x,
T y )
constexpr

Construct the vector from cartesian coordinates.

Parameters
xX coordinate
yY coordinate

◆ Vector2() [3/3]

template<typename T >
sf::Vector2< T >::Vector2 ( T r,
Angle phi )

Construct the vector from polar coordinates (floating-point)

Parameters
rLength of vector (can be negative)
phiAngle 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:

  • Vector2(r, phi) == Vector2(-r, phi + 180_deg)
  • Vector2(r, phi) == Vector2(r, phi + n * 360_deg)

Member Function Documentation

◆ angle()

template<typename T >
Angle sf::Vector2< T >::angle ( ) const
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()

template<typename T >
Angle sf::Vector2< T >::angleTo ( Vector2< T > rhs) const
nodiscard

Signed angle from *this to rhs (floating-point).

Returns
The smallest angle which rotates *this in positive or negative direction, until it has the same direction as rhs. The result has a sign and lies in the range [-180, 180) degrees.
Precondition
Neither *this nor rhs is a zero vector.

◆ componentWiseDiv()

template<typename T >
Vector2 sf::Vector2< T >::componentWiseDiv ( Vector2< T > rhs) const
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 rhs is zero.

◆ componentWiseMul()

template<typename T >
Vector2 sf::Vector2< T >::componentWiseMul ( Vector2< T > rhs) const
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()

template<typename T >
T sf::Vector2< T >::cross ( Vector2< T > rhs) const
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()

template<typename T >
T sf::Vector2< T >::dot ( Vector2< T > rhs) const
nodiscardconstexpr

Dot product of two 2D vectors.

◆ length()

template<typename T >
T sf::Vector2< T >::length ( ) const
nodiscard

Length of the vector (floating-point).

If you are not interested in the actual length, but only in comparisons, consider using lengthSquared().

◆ lengthSquared()

template<typename T >
T sf::Vector2< T >::lengthSquared ( ) const
nodiscardconstexpr

Square of vector's length.

Suitable for comparisons, more efficient than length().

◆ normalized()

template<typename T >
Vector2 sf::Vector2< T >::normalized ( ) const
nodiscard

Vector with same direction but length 1 (floating-point).

Precondition
*this is no zero vector.

◆ operator Vector2< U >()

template<typename T >
template<typename U >
sf::Vector2< T >::operator Vector2< U > ( ) const
explicitconstexpr

Converts the vector to another type of vector.

◆ perpendicular()

template<typename T >
Vector2 sf::Vector2< T >::perpendicular ( ) const
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()

template<typename T >
Vector2 sf::Vector2< T >::projectedOnto ( Vector2< T > axis) const
nodiscard

Projection of this vector onto axis (floating-point).

Parameters
axisVector being projected onto. Need not be normalized.
Precondition
axis must not have length zero.

◆ rotatedBy()

template<typename T >
Vector2 sf::Vector2< T >::rotatedBy ( Angle phi) const
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!=()

template<typename T >
bool operator!= ( Vector2< T > left,
Vector2< T > right )
related

Overload of binary operator!=

This operator compares strict difference between two vectors.

Parameters
leftLeft operand (a vector)
rightRight operand (a vector)
Returns
true if left is not equal to right

◆ operator*() [1/2]

template<typename T >
Vector2< T > operator* ( T left,
Vector2< T > right )
related

Overload of binary operator*

Parameters
leftLeft operand (a scalar value)
rightRight operand (a vector)
Returns
Member-wise multiplication by left

◆ operator*() [2/2]

template<typename T >
Vector2< T > operator* ( Vector2< T > left,
T right )
related

Overload of binary operator*

Parameters
leftLeft operand (a vector)
rightRight operand (a scalar value)
Returns
Member-wise multiplication by right

◆ operator*=()

template<typename T >
Vector2< T > & operator*= ( Vector2< T > & left,
T right )
related

Overload of binary operator*=

This operator performs a member-wise multiplication by right, and assigns the result to left.

Parameters
leftLeft operand (a vector)
rightRight operand (a scalar value)
Returns
Reference to left

◆ operator+()

template<typename T >
Vector2< T > operator+ ( Vector2< T > left,
Vector2< T > right )
related

Overload of binary operator+

Parameters
leftLeft operand (a vector)
rightRight operand (a vector)
Returns
Member-wise addition of both vectors

◆ operator+=()

template<typename T >
Vector2< T > & operator+= ( Vector2< T > & left,
Vector2< T > right )
related

Overload of binary operator+=

This operator performs a member-wise addition of both vectors, and assigns the result to left.

Parameters
leftLeft operand (a vector)
rightRight operand (a vector)
Returns
Reference to left

◆ operator-() [1/2]

template<typename T >
Vector2< T > operator- ( Vector2< T > left,
Vector2< T > right )
related

Overload of binary operator-

Parameters
leftLeft operand (a vector)
rightRight operand (a vector)
Returns
Member-wise subtraction of both vectors

◆ operator-() [2/2]

template<typename T >
Vector2< T > operator- ( Vector2< T > right)
related

Overload of unary operator-

Parameters
rightVector to negate
Returns
Member-wise opposite of the vector

◆ operator-=()

template<typename T >
Vector2< T > & operator-= ( Vector2< T > & left,
Vector2< T > right )
related

Overload of binary operator-=

This operator performs a member-wise subtraction of both vectors, and assigns the result to left.

Parameters
leftLeft operand (a vector)
rightRight operand (a vector)
Returns
Reference to left

◆ operator/()

template<typename T >
Vector2< T > operator/ ( Vector2< T > left,
T right )
related

Overload of binary operator/

Parameters
leftLeft operand (a vector)
rightRight operand (a scalar value)
Returns
Member-wise division by right

◆ operator/=()

template<typename T >
Vector2< T > & operator/= ( Vector2< T > & left,
T right )
related

Overload of binary operator/=

This operator performs a member-wise division by right, and assigns the result to left.

Parameters
leftLeft operand (a vector)
rightRight operand (a scalar value)
Returns
Reference to left

◆ operator==()

template<typename T >
bool operator== ( Vector2< T > left,
Vector2< T > right )
related

Overload of binary operator==

This operator compares strict equality between two vectors.

Parameters
leftLeft operand (a vector)
rightRight operand (a vector)
Returns
true if left is equal to right

Member Data Documentation

◆ x

template<typename T >
T sf::Vector2< T >::x {}

X coordinate of the vector.

Definition at line 203 of file Vector2.hpp.

◆ y

template<typename T >
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: