**Microsoft : Working With Rectangles**

**Introduction**

Rectangles figure prominently in Windows, and as you can guess, there are a number of functions that deal specifically with rectangles. This tutorial looks at the various rectangle functions that we have at our disposal. Before we do that though, it might be an idea to look at the rectangle structure called

**RECT**. The structure has four integer members known as

**left**,

**top**,

**right**and

**bottom**. They are signed integers, so it is quite acceptable to have a rectangle with some or all of it's members negative. The only caveat on that statement is that

**right**must be numerically larger than

**left**and

**bottom**must be numerically larger than

**top**.

**Setting a rectangle**

To set a rectangle, we can use the SetRect function. This will set the

**left**,

**top**,

**right**and

**bottom**member fields of the rectangle passed as an address to the function.

RECT rectangle_1; SetRect(&rectangle_1, 10, 20, 300, 40);

**Copying a rectangle**

To copy a rectangle, we use the

**CopyRect**function. This will take a copy of a rectangle specified as the second parameter and place the copy in the rectangle specified as the first parameter.

RECT rectangle_1 = { 10, 20, 300, 40 }; RECT rectangle_2; CopyRect(&rectangle_2, &rectangle_1);

**Setting a rectangle empty**

To set a rectangle to empty (i.e. { 0, 0, 0, 0 }), we use SetRectEmpty. This will clear the contents of the rectangle passed as the parameter to zero.

RECT rectangle_1 = { 10, 20, 300, 40 }; SetRectEmpty(&rectangle_1);

**Testing if a rectangle is empty**

To test a rectangle for empty, we use IsRectEmpty function. This will test to see if the rectangle passed as a parameter is empty and return non-zero if it is; zero if it is not. Be careful with this function because an empty rectangle is one where the

**right**member is less that or equal to the

**left**member

**OR**the

**bottom**member is less than or equal to the

**top**member.

RECT rectangle_1 = { 10, 20, 300, 40 }; if ( IsRectEmpty(&rectangle_1) == 0 ) SetRectEmpty(&rectangle_1);

**Testing if two rectangles are equal**

To test if two rectangles are equal, we use EqualRect function. This will test the rectangles for equality (i.e. all four member fields are the same), and return non-zero if it is; zero if it is not. Two rectangles that are empty may not be equal if their

**left**and

**top**coordinates are not the same!

RECT rectangle_1 = { 10, 20, 300, 40 }; RECT rectangle_2 = { 10, 20, 290, 30 }; if ( EqualRect(&rectangle_1, &rectangle_2) == 0 ) ...

**Change the size of a rectangle**

To change the size of a rectangle, we use InflateRect function. This takes the address of a rectangle as it's first parameter then a delta cx and cy value. The cx specifies how much to increase the rectangle width (+ve delta) or decrease the rectangle width (-ve delta). The cy specifies how much to increase the rectangle height (+ve delta) or decrease the rectangle height (-ve delta).

RECT rectangle_1 = { 10, 20, 300, 40 }; InflateRect(&rectangle_1, -100, 10); // The rectangle would now contain { 10, 20, 200, 50 }

**Moving a rectangle**

To move a rectangle up or down, left or right, we use the OffsetRect function. This takes the address of a rectangle as it's first parameter then a delta x and y value. The x value specifies how much to move the rectangle right (+ve delta) or left (-ve delta). The y value specifies how much to move the rectangle down (+ve delta) or up (-ve delta).

RECT rectangle_1 = { 10, 20, 300, 40 }; OffsetRect(&rectangle_1, 100, 10); // The rectangle would now contain { 110, 30, 400, 50 }

**To determine if a point lies within the bounds of a rectangle**

To determine if a point lies within the bounds of a rectangle, we use the PtInRect function. This takes the address of a rectangle as it's first parameter and a

**POINT**structure as it's second parameter. If the point lies in the rectangle non-zero is returned; zero if it is not.

POINT point = { 20, 30 }; RECT rectangle_1 = { 10, 20, 40, 50 }; if ( PtInRect(&rectangle_1, point) != 0 ) ...

**To get the intersection of two rectangles**

To get the intersection of two rectangles, we use the IntersectRect function. This takes the address of three rectangles. The second and third rectangles are used for computing the intersecting rectangle which is return as the first parameter.

RECT rectangle_1 = { 10, 20, 300, 40 }; RECT rectangle_2 = { 60, 10, 400, 90 }; RECT rectangle_3; IntersectRect(&rectangle_3, &rectangle_1, &rectangle_2); // rectangle_3 will contain { 60, 20, 300, 40 }

**Subtracting two rectangles**

To subtract two rectangles, we use the SubtractRect function. This takes the address of three rectangles. The third rectangle is subtracted from the second rectangle and the result is returned as the first rectangle.

RECT rectangle_1 = { 10, 20, 300, 40 }; RECT rectangle_2 = { 10, 20, 300, 40 }; RECT rectangle_3; SubtractRect(&rectangle_3, &rectangle_1, &rectangle_2); // rectangle_3 will contain { 0, 0, 0, 0 }

**To obtain the union of two rectangles**

To obtain the union of two rectangles, we use the UnionRect function. This takes the address of three rectangles. The second and third rectangles are used to forma union of both and the result is returned as the first rectangle.

RECT rectangle_1 = { 10, 20, 300, 40 }; RECT rectangle_2 = { 60, 10, 500, 140 }; RECT rectangle_3; UnionRect(&rectangle_3, &rectangle_1, &rectangle_2); // rectangle_3 will contain { 10, 10, 500, 140 }