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-2024 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
28
29#include <SFML/System/Angle.hpp>
30
31
32namespace sf
33{
39template <typename T>
41{
42public:
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
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
211
221template <typename T>
222[[nodiscard]] constexpr Vector2<T> operator-(Vector2<T> right);
223
237template <typename T>
239
253template <typename T>
255
266template <typename T>
267[[nodiscard]] constexpr Vector2<T> operator+(Vector2<T> left, Vector2<T> right);
268
279template <typename T>
280[[nodiscard]] constexpr Vector2<T> operator-(Vector2<T> left, Vector2<T> right);
281
292template <typename T>
293[[nodiscard]] constexpr Vector2<T> operator*(Vector2<T> left, T right);
294
305template <typename T>
306[[nodiscard]] constexpr Vector2<T> operator*(T left, Vector2<T> right);
307
321template <typename T>
322constexpr Vector2<T>& operator*=(Vector2<T>& left, T right);
323
334template <typename T>
335[[nodiscard]] constexpr Vector2<T> operator/(Vector2<T> left, T right);
336
350template <typename T>
351constexpr Vector2<T>& operator/=(Vector2<T>& left, T right);
352
365template <typename T>
366[[nodiscard]] constexpr bool operator==(Vector2<T> left, Vector2<T> right);
367
380template <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
#define SFML_SYSTEM_API
Represents an angle value.
Definition Angle.hpp:35
Class template for manipulating 2-dimensional vectors.
Definition Vector2.hpp:41
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)
T x
X coordinate of the vector.
Definition Vector2.hpp:203
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+
T y
Y coordinate of the vector.
Definition Vector2.hpp:204
constexpr Vector2 perpendicular() const
Returns a perpendicular vector.
constexpr T lengthSquared() const
Square of vector's length.
Vector2 projectedOnto(Vector2 axis) const
Projection of this vector onto axis (floating-point).
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 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/