Search code examples
direct2drect

Rectangle manipulation is missing in Direct2D


Direct2D has D2D1_RECT_F

{
    FLOAT left;
    FLOAT top;
    FLOAT right;
    FLOAT bottom;
}

It's similar to GDI RECT structure except it uses float values.

typedef struct tagRECT
{
    LONG    left;
    LONG    top;
    LONG    right;
    LONG    bottom;
} RECT;

GDI provides all those RECT manipulation functions like

BOOL IntersectRect(
   _Out_  LPRECT lprcDst,
   _In_   const RECT *lprcSrc1,
   _In_   const RECT *lprcSrc2
);

BOOL SubtractRect(
   _Out_  LPRECT lprcDst,
   _In_   const RECT *lprcSrc1,
   _In_   const RECT *lprcSrc2
);

I can't believe Direct2D doesn't provide similar functions for D2D1_RECT_F.

I guess I can create rectangular geometries and combine them any way I want but that's creating and allocating objects instead of doing simple math. Or I guess I may just create my own versions of them.

Am I missing something? Thanks.


Solution

  • For IntersectRect, Direct2D has ID2D1Geometry::CompareWithGeometry which will determine the relationship between two geometries, that's will work for you. note, this function only return the relationship of the two geometries, such as overlap, contains, it will not return the intersection rectangle as what IntersectRect did.

    For SubtractRect, Direct2D has no such function, you need to write it yourself.