# Math behind subgrid?

Hello !
I know it’s not directly related to OF but I try…
I’m making a step sequencer (see image) (4x4 so 16 steps) with multiple readers. I need to create an iteration loop through each subgrid (pink and blue or any). So I need to create a “counter” that depend on the width(w) and height(h) of each subgrid and I don’t get the math behind that… the output of the counter should be (in that case): for the pink -> 1,2,5,6,9,10… for the blue -> 9,10,11,13,14,15…

what is the formula that can handle any subgrid ?

08|645x500

Hi,
I’m not 100% sure that I understand your question, but look at this:

``````// Return all the grid indices for a subgrid.
// Parameters:
// - subgridFirstIndex : index of the upper left cell of the subgrid
// - subgridWidth : num cols of the subgrid
// - subgridHeight : num rows of the subgrid
// - gridWith : num cols of the grid

vector< int > ofApp::getSubgridIndices( int subgridFirstIndex, int subgridWidth, int subgridHeight, int gridWidth )
{
vector< int > indices ;
for( int y = 0 ; y < subgridHeight ; y++ )
for( int x = 0 ; x < subgridWidth ; x++ )
indices.push_back( subgridFirstIndex + y * gridWidth + x ) ;
return indices ;
}

// Return the grid indice for a subgrid cell
// Parameters :
// - subgridIndex : 0 to get the grid index of the 1rst subgrid cell,
//                  1 to get the grid index of the énd subgrid cell,
//                  etc
// - subgridFirstIndex, subgridWidth, subgridHeight, gridWidth : see comments of getSubgridIndices()

int ofApp::getGridIndex( int subgridIndex, int subgridFirstIndex, int subgridWidth, int subgridHeight, int gridWidth )
{
int max = subgridWidth * subgridHeight ;
if( subgridIndex < 0 || subgridIndex >= max )
throw std::out_of_range( "subgridIndex is out of range !" ) ;
int y = subgridIndex / subgridWidth ;
int x = subgridIndex % subgridWidth ;
return subgridFirstIndex + y * gridWidth + x ;
}
``````

Usage example:

``````    // the pink subgrid starts at index 1, has 2 cols and 3 rows, the whole grid has 4 cols
vector< int > pink = getSubgridIndices( 1, 2, 3, 4 ) ;
// the blue subgrid starts at index 9, has 3 cols and 2 rows, the whole grid has 4 cols
vector< int > blue = getSubgridIndices( 9, 3, 2, 4 ) ;

cout << "Method 1:" << endl ;
cout << "Pink cells indices: " << ofToString( pink ) << endl ;
cout << "Blue cells indices: " << ofToString( blue ) << endl ;

cout << "Method 2:" << endl ;
cout << "Pink cells indices - " ;
for( int i = 0 ; i < 2 * 3 ; i++ )
cout << i << ":" << getGridIndex( i, 1, 2, 3, 4 ) << " - " ;
cout << endl ;
cout << "Blue cells indices - " ;
for( int i = 0 ; i < 3 * 2 ; i++ )
cout << i << ":" << getGridIndex( i, 9, 3, 2, 4 ) << " - " ;
``````

Output:

``````Method 1:
Pink cells indices: {1, 2, 5, 6, 9, 10}
Blue cells indices: {9, 10, 11, 13, 14, 15}
Method 2:
Pink cells indices - 0:1 - 1:2 - 2:5 - 3:6 - 4:9 - 5:10 -
Blue cells indices - 0:9 - 1:10 - 2:11 - 3:13 - 4:14 - 5:15 -
``````

Wow ! thank’s a lot!
it will need little adaptation for my code but it will absolutely do the trick’s.