SFML - Simple and Fast Multimedia Library

SFML 3.1.0
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-2026 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/Angle.hpp>
28
29#include <type_traits>
30
31#include <cassert>
32#include <cmath>
33
34
35namespace sf
36{
42template <typename T>
43 class Vector2
44{
45public:
52 constexpr Vector2() = default;
53
61 constexpr Vector2(T x, T y);
62
67 template <typename U>
68 constexpr explicit operator Vector2<U>() const;
69
84 Vector2(T r, Angle phi);
85
92 [[nodiscard]] T length() const;
93
100 [[nodiscard]] constexpr T lengthSquared() const;
101
108 [[nodiscard]] Vector2 normalized() const;
109
119 [[nodiscard]] Angle angleTo(Vector2 rhs) const;
120
130 [[nodiscard]] Angle angle() const;
131
141 [[nodiscard]] Vector2 rotatedBy(Angle phi) const;
142
150 [[nodiscard]] constexpr Vector2 projectedOnto(Vector2 axis) const;
151
162 [[nodiscard]] constexpr Vector2 perpendicular() const;
163
168 [[nodiscard]] constexpr T dot(Vector2 rhs) const;
169
177 [[nodiscard]] constexpr T cross(Vector2 rhs) const;
178
188 [[nodiscard]] constexpr Vector2 componentWiseMul(Vector2 rhs) const;
189
200 [[nodiscard]] constexpr Vector2 componentWiseDiv(Vector2 rhs) const;
201
202
204 // Member data
206 T x{};
207 T y{};
208};
209
210// Define the most common types
211 using Vector2i = Vector2<int>;
212 using Vector2u = Vector2<unsigned int>;
213 using Vector2f = Vector2<float>;
214
224template <typename T>
225[[nodiscard]] constexpr Vector2<T> operator-(Vector2<T> right);
226
240template <typename T>
241 constexpr Vector2<T>& operator+=(Vector2<T>& left, Vector2<T> right);
242
256template <typename T>
257 constexpr Vector2<T>& operator-=(Vector2<T>& left, Vector2<T> right);
258
269template <typename T>
270[[nodiscard]] constexpr Vector2<T> operator+(Vector2<T> left, Vector2<T> right);
271
282template <typename T>
283[[nodiscard]] constexpr Vector2<T> operator-(Vector2<T> left, Vector2<T> right);
284
295template <typename T>
296[[nodiscard]] constexpr Vector2<T> operator*(Vector2<T> left, T right);
297
308template <typename T>
309[[nodiscard]] constexpr Vector2<T> operator*(T left, Vector2<T> right);
310
324template <typename T>
325 constexpr Vector2<T>& operator*=(Vector2<T>& left, T right);
326
337template <typename T>
338[[nodiscard]] constexpr Vector2<T> operator/(Vector2<T> left, T right);
339
353template <typename T>
354 constexpr Vector2<T>& operator/=(Vector2<T>& left, T right);
355
368template <typename T>
369[[nodiscard]] constexpr bool operator==(Vector2<T> left, Vector2<T> right);
370
383template <typename T>
384[[nodiscard]] constexpr bool operator!=(Vector2<T> left, Vector2<T> right);
385
386} // namespace sf
387
388#include <SFML/System/Vector2.inl>
389
390
Angle.hpp
sf::Angle
Represents an angle value.
Definition Angle.hpp:35
sf::Vector2
Class template for manipulating 2-dimensional vectors.
Definition Vector2.hpp:44
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:206
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:207
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::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::projectedOnto
constexpr Vector2 projectedOnto(Vector2 axis) const
Projection of this vector onto axis (floating-point).
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:212
sf::Vector2i
Vector2< int > Vector2i
Definition Vector2.hpp:211
sf::Vector2f
Vector2< float > Vector2f
Definition Vector2.hpp:213