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

Rectangle

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

A 2D rectangle class modeled after the XNA Game Studio 4 (Microsoft.Xna.Framework.Rectangle) math library

For Universal Windows Platform and Xbox One, similar functionality is provided by the Windows::Foundation::Rect Windows Runtime structure

Header

#include <SimpleMath.h>

Initialization

using namespace DirectX::SimpleMath;

Rectangle r;                    // Creates an empty rectangle [0, 0, 0, 0]
Rectangle r(10, 20, 100, 200);  // Creates a rectangle with upper-left [10,20]
                                // 100 width, and 200 height

For Windows desktop apps, you may find that Rectangle conflicts with the GDI function of the same name. You can resolve this three ways: Use #define NOGDI before you include <Windows.h>; use SimpleMath::Rectangle, or in local scope use using Rectangle = SimpleMath::Rectangle;.

Fields

  • x location of the upper-left corner
  • y location of the upper-left corner
  • width of the rectangle
  • height of the rectangle

Methods

  • Comparison operators: == and !=

  • Assignment operator: =

  • Location, Center: Returns the rectangle position as a Vector2

  • IsEmpty: Returns true if the rectangle is [0, 0, 0, 0]

  • Contains: Tests to see if a point or rectangle is contained by the rectangle

  • Inflate: Grows the rectangle by a specified amount in the horizontal and vertical dimensions

  • Intersects: Tests to see if another rectangle intersects with this rectangle

  • Offset: Moves or offsets the rectangle by moving the upper-left position

Statics

  • Intersect: Computes the intersection of two rectangles or the empty rectangle [0, 0, 0, 0] if there is no intersection

  • Union: Computes the union of two rectangles

Exceptions

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

Remark

Rectangle will freely convert to and from a RECT. In UWP and Xbox One apps, you can also convert it to a Windows::Foundation::Rect.

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.