SFML 3.0.2
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-2025 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
27#include <SFML/System/Export.hpp>
28
29#include <SFML/System/Angle.hpp>
30
31
32 namespace sf
33{
39 template <typename T>
40 class Vector2
41{
42 public:
49 constexpr Vector2() = default;
50
58 constexpr Vector2(T x, T y);
59
64 template <typename U>
65 constexpr explicit operator Vector2<U>() const;
66
81 SFML_SYSTEM_API Vector2(T r, Angle phi);
82
89 [[nodiscard]] SFML_SYSTEM_API T length() const;
90
97 [[nodiscard]] constexpr T lengthSquared() const;
98
105 [[nodiscard]] SFML_SYSTEM_API Vector2 normalized() const;
106
116 [[nodiscard]] SFML_SYSTEM_API Angle angleTo(Vector2 rhs) const;
117
127 [[nodiscard]] SFML_SYSTEM_API Angle angle() const;
128
138 [[nodiscard]] SFML_SYSTEM_API Vector2 rotatedBy(Angle phi) const;
139
147 [[nodiscard]] SFML_SYSTEM_API Vector2 projectedOnto(Vector2 axis) const;
148
159 [[nodiscard]] constexpr Vector2 perpendicular() const;
160
165 [[nodiscard]] constexpr T dot(Vector2 rhs) const;
166
174 [[nodiscard]] constexpr T cross(Vector2 rhs) const;
175
185 [[nodiscard]] constexpr Vector2 componentWiseMul(Vector2 rhs) const;
186
197 [[nodiscard]] constexpr Vector2 componentWiseDiv(Vector2 rhs) const;
198
199
201 // Member data
203 T x{};
204 T y{};
205};
206
207// Define the most common types
208 using Vector2i = Vector2<int>;
209 using Vector2u = Vector2<unsigned int>;
210 using Vector2f = Vector2<float>;
211
221 template <typename T>
222[[nodiscard]] constexpr Vector2<T> operator-(Vector2<T> right);
223
237 template <typename T>
238 constexpr Vector2<T>& operator+=(Vector2<T>& left, Vector2<T> right);
239
253 template <typename T>
254 constexpr Vector2<T>& operator-=(Vector2<T>& left, Vector2<T> right);
255
266 template <typename T>
267[[nodiscard]] constexpr Vector2<T> operator+(Vector2<T> left, Vector2<T> right);
268
279 template <typename T>
280[[nodiscard]] constexpr Vector2<T> operator-(Vector2<T> left, Vector2<T> right);
281
292 template <typename T>
293[[nodiscard]] constexpr Vector2<T> operator*(Vector2<T> left, T right);
294
305 template <typename T>
306[[nodiscard]] constexpr Vector2<T> operator*(T left, Vector2<T> right);
307
321 template <typename T>
322 constexpr Vector2<T>& operator*=(Vector2<T>& left, T right);
323
334 template <typename T>
335[[nodiscard]] constexpr Vector2<T> operator/(Vector2<T> left, T right);
336
350 template <typename T>
351 constexpr Vector2<T>& operator/=(Vector2<T>& left, T right);
352
365 template <typename T>
366[[nodiscard]] constexpr bool operator==(Vector2<T> left, Vector2<T> right);
367
380 template <typename T>
381[[nodiscard]] constexpr bool operator!=(Vector2<T> left, Vector2<T> right);
382
383} // namespace sf
384
385#include <SFML/System/Vector2.inl>
386
387
Angle.hpp
Export.hpp
SFML_SYSTEM_API
#define SFML_SYSTEM_API
Definition System/Export.hpp:42
sf::Angle
Represents an angle value.
Definition Angle.hpp:35
sf::Vector2
Class template for manipulating 2-dimensional vectors.
Definition Vector2.hpp:41
sf::Vector2::Vector2
constexpr Vector2(T x, T y)
Construct the vector from cartesian coordinates.
sf::Vector2::cross
constexpr T cross(Vector2 rhs) const
Z component of the cross product of two 2D vectors.
sf::Vector2::length
T length() const
Length of the vector (floating-point).
sf::Vector2::Vector2
Vector2(T r, Angle phi)
Construct the vector from polar coordinates (floating-point)
sf::Vector2< float >::x
float x
Definition Vector2.hpp:203
sf::Vector2::componentWiseDiv
constexpr Vector2 componentWiseDiv(Vector2 rhs) const
Component-wise division of *this and rhs.
sf::Vector2::Vector2
constexpr Vector2()=default
Default constructor.
sf::Vector2::operator+
constexpr Vector2< T > operator+(Vector2< T > left, Vector2< T > right)
Overload of binary operator+
sf::Vector2< float >::y
float y
Definition Vector2.hpp:204
sf::Vector2::perpendicular
constexpr Vector2 perpendicular() const
Returns a perpendicular vector.
sf::Vector2::lengthSquared
constexpr T lengthSquared() const
Square of vector's length.
sf::Vector2::projectedOnto
Vector2 projectedOnto(Vector2 axis) const
Projection of this vector onto axis (floating-point).
sf::Vector2::rotatedBy
Vector2 rotatedBy(Angle phi) const
Rotate by angle phi (floating-point).
sf::Vector2::angleTo
Angle angleTo(Vector2 rhs) const
Signed angle from *this to rhs (floating-point).
sf::Vector2::operator-
constexpr Vector2< T > operator-(Vector2< T > right)
Overload of unary operator-
sf::Vector2::operator==
constexpr bool operator==(Vector2< T > left, Vector2< T > right)
Overload of binary operator==
sf::Vector2::operator-
constexpr Vector2< T > operator-(Vector2< T > left, Vector2< T > right)
Overload of binary operator-
sf::Vector2::operator*=
constexpr Vector2< T > & operator*=(Vector2< T > &left, T right)
Overload of binary operator*=
sf::Vector2::operator+=
constexpr Vector2< T > & operator+=(Vector2< T > &left, Vector2< T > right)
Overload of binary operator+=
sf::Vector2::operator-=
constexpr Vector2< T > & operator-=(Vector2< T > &left, Vector2< T > right)
Overload of binary operator-=
sf::Vector2::normalized
Vector2 normalized() const
Vector with same direction but length 1 (floating-point).
sf::Vector2::componentWiseMul
constexpr Vector2 componentWiseMul(Vector2 rhs) const
Component-wise multiplication of *this and rhs.
sf::Vector2::angle
Angle angle() const
Signed angle from +X or (1,0) vector (floating-point).
sf::Vector2::operator/=
constexpr Vector2< T > & operator/=(Vector2< T > &left, T right)
Overload of binary operator/=
sf::Vector2::dot
constexpr T dot(Vector2 rhs) const
Dot product of two 2D vectors.
sf::Vector2::operator!=
constexpr bool operator!=(Vector2< T > left, Vector2< T > right)
Overload of binary operator!=
sf::Vector2::operator*
constexpr Vector2< T > operator*(Vector2< T > left, T right)
Overload of binary operator*
sf::Vector2::operator*
constexpr Vector2< T > operator*(T left, Vector2< T > right)
Overload of binary operator*
sf::Vector2::operator/
constexpr Vector2< T > operator/(Vector2< T > left, T right)
Overload of binary operator/
sf
Definition AudioResource.hpp:36
sf::Vector2u
Vector2< unsigned int > Vector2u
Definition Vector2.hpp:209
sf::Vector2i
Vector2< int > Vector2i
Definition Vector2.hpp:208
sf::Vector2f
Vector2< float > Vector2f
Definition Vector2.hpp:210