2D vector with floating-point or fixed-point components for positions, movement, and physics.
More...
|
| constexpr | Vector2 () noexcept |
| | Default constructor.
|
| constexpr | Vector2 (const T &x, const T &y) noexcept |
| | Constructs a vector with the given components.
|
| constexpr | Vector2 (const T *values) noexcept |
| | Constructs a vector from an array.
|
| constexpr T * | c_arr () noexcept |
| | Pointer to the component array (x, then y).
|
| constexpr const T * | c_arr () const noexcept |
| | Read-only pointer to the component array (x, then y).
|
| constexpr Vector2 & | operator+= (const Vector2 &vector) noexcept |
| | Adds vector to this vector.
|
| constexpr Vector2 & | operator-= (const Vector2 &vector) noexcept |
| | Subtracts vector from this vector.
|
| constexpr Vector2 & | operator*= (const T &scalar) noexcept |
| | Multiplies both components by scalar.
|
| constexpr Vector2 & | operator/= (const T &scalar) noexcept |
| | Divides both components by scalar.
|
| constexpr T | sqrMagnitude () const noexcept |
| | Squared Euclidean length (x² + y²).
|
| constexpr void | setZero () noexcept |
| | Sets x and y to zero.
|
| constexpr bool | isZero () const noexcept |
| | Returns whether both components are zero.
|
| constexpr bool | isEqual (const Vector2 &vector, T absEpsilon=8 *numeric_limits< T >::epsilon(), T relEpsilon=64 *numeric_limits< T >::epsilon()) const noexcept |
| | Equality within absolute and relative epsilon.
|
template<Vector2Component T>
class toy::math::Vector2< T >
2D vector with floating-point or fixed-point components for positions, movement, and physics.
Represents a 2D vector with x and y components. Suited for world/screen coordinates, velocity, forces, and transformations where sub-pixel or fractional precision is needed. For integer pixel coordinates use toy::math::Point.
Key Features
- Precision: Component type T is toy::math::Vector2Component (float, double, or fixed-point).
- Constexpr: Most operations are constexpr.
- Exception safety: All operations are noexcept.
- Small size: Two components; stack-friendly.
Usage Example
auto deltaTime = 0.5f;
pos += velocity * deltaTime;
auto mass = 2.0f;
auto acceleration = force / mass;
2D vector with floating-point or fixed-point components for positions, movement, and physics.
Definition vector2.hpp:96
Umbrella header for the math module.
Performance Characteristics
- Construction, assignment, arithmetic, comparison: O(1).
- Memory: 2 × sizeof(T).
Safety Guarantees
- Contracts: Division by zero is asserted in debug.
- Exception safety: noexcept; no exceptions thrown.
- Note
- For pixel-aligned UI coordinates use toy::math::Point.
- See also
- toy::math::Point