Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Vector2

Chuck Walbourn edited this page Aug 17, 2025 · 16 revisions
DirectXTK SimpleMath

Two component vector class modeled after the XNA Game Studio 4 (Microsoft.Xna.Framework.Vector2) math library.

Header

#include <SimpleMath.h>

Initialization

using namespace DirectX::SimpleMath;

Vector2 v;        // Creates a vector [0, 0]
Vector2 v(10);    // Creates a vector [10, 10]
Vector2 v(1, 2);  // Creates a vector [1, 2]

float arr[2] = { 1, 2 };
Vector2 v(arr);   // Creates a vector [1, 2]

Fields

  • x component of vector
  • y component of vector

Methods

  • Comparison operators: == and !=

  • Assignment operators: =, +=, -=, *=, /=

  • Unary operators: +, -

  • Binary operators: +, -, *, /

  • InBounds: Tests if the vector is within the bounds set by the positive and negative of the input bounds.

  • Length, LengthSquared: Compute vector length.

  • Dot: Returns the dot-product between two 2-vectors.

  • Cross: Computes the geometric cross-product of two 2-vectors. See XMVector2Cross.

  • Normalize: Computes a unit-length vector.

  • Clamp: Clamps the values of the vector per component between a minimum and a maximum.

Statics

  • Distance, DistanceSquared: Compute distance between two vectors.

  • Min, Max: Determine minimum/maximum vector elements.

  • Lerp: Performs a linear interpolation.

  • SmoothStep: Performs a cube interpolation.

  • Barycentric: Returns a point in Barycentric coordinates.

  • CatmullRom: Performs a Catmull-Rom interpolation.

  • Hermite: Performs a Hermite spline interpolation.

  • Reflect: Reflects an incident vector across a normal vector.

  • Refract: Refracts an incident vector across a normal vector.

  • Transform: Transform the vector by the rotation and scale in the matrix or a quaternion. There is also an overload for transforming an array of vectors. See XMVector2Transform.

  • TransformNormal: Transforms the vector by the rotation and scale in the matrix, ignoring translation (rows 2 & 3). There is also an overload for transforming an array of vectors. See XMVector2TransformNormal.

Constants

  • Zero: Zero vector [0, 0]
  • One: One vector [1, 1]
  • UnitX: Unit X vector [1, 0]
  • UnitY: Unit Y vector [0, 1]

Exceptions

These methods are marked noexcept, and do not throw C++ exceptions.

Remark

Vector2 can freely convert to and from a XMFLOAT2 and XMVECTOR (the z and w components are lost).

See also

Vector3, Vector4, Matrix

For Use

  • Universal Windows Platform apps
  • Windows desktop apps
  • Windows 11
  • Windows 10
  • Windows 8.1
  • Xbox One

Architecture

  • x86
  • x64
  • ARM64

For Development

  • Visual Studio 2022
  • Visual Studio 2019 (16.11)
  • clang/LLVM v12 - v20
  • MinGW 12.2, 13.2
  • CMake 3.21

Related Projects

DirectX Tool Kit for DirectX 12

DirectXMesh

DirectXTex

DirectXMath

Win2D

Tools

Test Suite

Model Viewer

Content Exporter

DxCapsViewer

Clone this wiki locally

Morty Proxy This is a proxified and sanitized view of the page, visit original site.