Thread: array of rectangles

  1. #1
    Registered User xlnk's Avatar
    Join Date
    Mar 2002
    Posts
    186

    Question array of rectangles

    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?
    the best things in life are simple.

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    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);
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    There is no other way.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>There is no other way.<<



    Future proofing.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    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:
    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 );
    }
    Or if you find out a run-time..
    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;

  6. #6
    Registered User xlnk's Avatar
    Join Date
    Mar 2002
    Posts
    186
    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!
    Last edited by xlnk; 12-18-2002 at 07:34 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM