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>
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
214
224template <typename T>
225[[nodiscard]] constexpr Vector2<T> operator-(Vector2<T> right);
226
240template <typename T>
242
256template <typename T>
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>
325constexpr 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>
354constexpr 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
Represents an angle value.
Definition Angle.hpp:35
Class template for manipulating 2-dimensional vectors.
Definition Vector2.hpp:44
constexpr Vector2(T x, T y)
Construct the vector from cartesian coordinates.
constexpr T cross(Vector2 rhs) const
Z component of the cross product of two 2D vectors.
T length() const
Length of the vector (floating-point).
Vector2(T r, Angle phi)
Construct the vector from polar coordinates (floating-point).
constexpr Vector2 componentWiseDiv(Vector2 rhs) const
Component-wise division of *this and rhs.
constexpr Vector2()=default
Default constructor.
constexpr Vector2< T > operator+(Vector2< T > left, Vector2< T > right)
Overload of binary operator+.
constexpr Vector2 perpendicular() const
Returns a perpendicular vector.
constexpr T lengthSquared() const
Square of vector's length.
Vector2 rotatedBy(Angle phi) const
Rotate by angle phi (floating-point).
Angle angleTo(Vector2 rhs) const
Signed angle from *this to rhs (floating-point).
constexpr Vector2< T > operator-(Vector2< T > right)
Overload of unary operator-.
constexpr Vector2 projectedOnto(Vector2 axis) const
Projection of this vector onto axis (floating-point).
constexpr bool operator==(Vector2< T > left, Vector2< T > right)
Overload of binary operator==.
constexpr Vector2< T > operator-(Vector2< T > left, Vector2< T > right)
Overload of binary operator-.
constexpr Vector2< T > & operator*=(Vector2< T > &left, T right)
Overload of binary operator*=.
constexpr Vector2< T > & operator+=(Vector2< T > &left, Vector2< T > right)
Overload of binary operator+=.
constexpr Vector2< T > & operator-=(Vector2< T > &left, Vector2< T > right)
Overload of binary operator-=.
Vector2 normalized() const
Vector with same direction but length 1 (floating-point).
constexpr Vector2 componentWiseMul(Vector2 rhs) const
Component-wise multiplication of *this and rhs.
Angle angle() const
Signed angle from +X or (1,0) vector (floating-point).
constexpr Vector2< T > & operator/=(Vector2< T > &left, T right)
Overload of binary operator/=.
constexpr T dot(Vector2 rhs) const
Dot product of two 2D vectors.
constexpr bool operator!=(Vector2< T > left, Vector2< T > right)
Overload of binary operator!=.
constexpr Vector2< T > operator*(Vector2< T > left, T right)
Overload of binary operator*.
constexpr Vector2< T > operator*(T left, Vector2< T > right)
Overload of binary operator*.
constexpr Vector2< T > operator/(Vector2< T > left, T right)
Overload of binary operator/.
Vector2< unsigned int > Vector2u
Definition Vector2.hpp:212
Vector2< int > Vector2i
Definition Vector2.hpp:211
Vector2< float > Vector2f
Definition Vector2.hpp:213