|
SFML
Simple and Fast Multimedia Library
|
Class template for manipulating 2-dimensional vectors. More...
#include <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 | Vector2 (Vector2< U > vector) |
| Construct the vector from another type of vector. | |
| SFML_SYSTEM_API | Vector2 (T r, Angle phi) |
| Construct the vector from polar coordinates (floating-point). | |
| SFML_SYSTEM_API T | length () const |
| Length of the vector (floating-point). | |
| constexpr T | lengthSquared () const |
| Square of vector's length. | |
| SFML_SYSTEM_API Vector2 | normalized () const |
| Vector with same direction but length 1 (floating-point). | |
| SFML_SYSTEM_API Angle | angleTo (Vector2 rhs) const |
Signed angle from *this to rhs (floating-point). | |
| SFML_SYSTEM_API Angle | angle () const |
| Signed angle from +X or (1,0) vector (floating-point). | |
| SFML_SYSTEM_API Vector2 | rotatedBy (Angle phi) const |
Rotate by angle phi (floating-point). | |
| SFML_SYSTEM_API 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. | |
Static Public Attributes | |
| static const Vector2 | UnitX |
| The X unit vector (1, 0), usually facing right. | |
| static const Vector2 | UnitY |
| The Y unit vector (0, 1), usually facing down. | |
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.
|
constexprdefault |
Default constructor.
Creates a Vector2(0, 0).
|
constexpr |
|
explicitconstexpr |
Construct the vector from another type of vector.
This constructor doesn't replace the copy constructor, it's called only when U != T. A call to this constructor will fail to compile if U is not convertible to T.
| vector | Vector to convert |
| SFML_SYSTEM_API 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).
*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. *this nor rhs is a zero vector.
|
nodiscardconstexpr |
|
nodiscardconstexpr |
|
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).
*this is no zero 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. |
axis must not have length zero.
|
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) |
left is not equal to right Overload of binary operator *.
| left | Left operand (a scalar value) |
| right | Right operand (a vector) |
left Overload of binary operator *.
| left | Left operand (a vector) |
| right | Right operand (a scalar value) |
right Overload of binary operator *=.
This operator performs a memberwise multiplication by right, and assigns the result to left.
| left | Left operand (a vector) |
| right | Right operand (a scalar value) |
left Overload of binary operator +.
| left | Left operand (a vector) |
| right | Right operand (a vector) |
Overload of binary operator +=.
This operator performs a memberwise addition 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 vector) |
Overload of unary operator -.
| right | Vector to negate |
Overload of binary operator -=.
This operator performs a memberwise 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) |
right Overload of binary operator /=.
This operator performs a memberwise division by right, and assigns the result to left.
| left | Left operand (a vector) |
| right | Right operand (a scalar value) |
left Overload of binary operator ==.
This operator compares strict equality between two vectors.
| left | Left operand (a vector) |
| right | Right operand (a vector) |
left is equal to right
|
static |
The X unit vector (1, 0), usually facing right.
|
static |
The Y unit vector (0, 1), usually facing down.
| T sf::Vector2< T >::x {} |
X coordinate of the vector.
| T sf::Vector2< T >::y {} |
Y coordinate of the vector.