I want to create an array of rectangles. How would I got about doing this? Do I create an array of RECT? are there other ways?
Printable View
I want to create an array of rectangles. How would I got about doing this? Do I create an array of RECT? are there other ways?
Use RECT with win32, CRect or RECT with mfc; other class libraries may have similar structures/classes defined for use. How you set up your array is entirely up to you. Here's 3 possible ways:
RECT rc[100];
const int NUM_RECTS=100;
RECT *rc=new RECT[NUM_RECTS]; /*...and later*/ delete[] rc;
#define NUM_RECTS 100;
RECT *rc=(RECT*)malloc(sizeof(RECT)*NUM_RECTS); /*...and later*/ free(rc);
There is no other way. :)
>>There is no other way.<<
:D
Future proofing. ;)
Well its the same as any other data type. Do you know how many you need before hand or at run-time?
If you know how many you need then you can do it like this:
Or if you find out a run-time..Code:// An array of 5 rects
RECT myRects[5];
int i;
// Set a certain rect to something
for( i = 0; i < 5; ++i )
{
SetRect( &myRects[i], 10, 20, 30, 40 );
}
Code:// Assume nRects is initialized to number of RECT's we need
RECT *pRects = NULL;
int i;
pRects = new RECT[ nRects ];
if( pRects == NULL )
{
FatalAppExit( 0, "Out of memory" );
}
// Setup the rects
for( i = 0; i < nRects; ++i )
{
SetRect( (pRects + i), 10, 20, 30, 40 );
}
// Free memory
delete [] pRects;
Thanks!
Well, i want to build a cellular automata program (Conways game of life) so I'll have a set amount of rectangles before hand.
Thanks again!