Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: 1D array to "most square" 2D array...

  1. #1
    Intern Newbie
    Join Date
    Sep 2004
    Posts
    30

    1D array to "most square" 2D array...

    Hello,
    Ive been wondering about the following problem...

    Given a 1D array of n values,

    I want to find a 2D array to store it.. But the array has to be as "square as possible",

    ie:
    x_dimension - y_dimension = m
    |m| has to be as small as possible.

    and without any padding, ie: dummy values filling the array..

    Any help?
    ut.

  2. #2
    Super Moderator OpenGL Lord
    Join Date
    Dec 2003
    Location
    Grenoble - France
    Posts
    5,655

    Re: 1D array to "most square" 2D array...

    does no seem so hard :

    x = ceil(sqrt(n));
    y = ceil(n*1.0/x);

  3. #3
    Intern Newbie
    Join Date
    Sep 2004
    Posts
    30

    Re: 1D array to "most square" 2D array...

    Here is what i ended up using, if anyone has anything bit better, please let me know, as i find this solution a bit nasty...

    Code :
    //given array_size (size of the 1D array)
    //work out 2D array dimensions.
     
    float dimX = sqrt(array_size);
     
    while(helper)
    {
    	if(array_size%dimX==0) helper=0;
    	dimX++;
    }
     
    dimY = array_size/dimX;

  4. #4
    Intern Newbie
    Join Date
    Sep 2004
    Posts
    30

    Re: 1D array to "most square" 2D array...

    Thanks for the reply ZbuffeR,

    However with your approach, the 2D array will mostimes be bigger then N. I dont want this..

    ut.

  5. #5
    Super Moderator OpenGL Lord
    Join Date
    Dec 2003
    Location
    Grenoble - France
    Posts
    5,655

    Re: 1D array to "most square" 2D array...

    Ah ok I reversed your priorities. Your strongest need is that there is "not padding at all", I first though is was "as square as possible".

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •