Function: Populate2DArray( )
The code in the previous sections needs should be refactored so it's using functions, this will allow us to separate the code-logic:
creating the shape-color units using design logic
Notice, there are no
xPos, yPosvariables used inPopulate2DArray, instead, this function only populates our 2D data-structure.
displaying the shape-color units in a grid layout
The layout and positioning in grid configuration will be done using a different function: displayShapeMatrix( )
Populate2DArray(PShape[][] shapeMatrix, int rows, int cols, int cellSize){
color c1=color(157, 83, 56);
color c2 = color(258, 66, 96);
for( int i=0; i< rows; i++){
for( int j=0; j< cols; j++){
int k = i + j;
//calculate fill color
float kFraction = map( k, 0, (rows-1) + (cols-1),0.0, 1.0);
color c3 = lerpColor(c1, c2, kFraction);
//fill( c3 ); ///use map? max of k is 10
PShape curShape;
if( k %2==0){
//create Shape and setFill
curShape = cirPattern( cellSize-10, c3);
} else{
curShape = rectPattern( cellSize-10, c3);
}
shapeMatrix[i][j]= curShape; //store in 2D array
} //end of inner loop (cols)
} //end of outer loop (rows)
return shapeMatrix;
}Populate2DArray - with Color input-paramters
It might be better to move the endpoint colors for the lerpColor function outside this function, so they can be passed into the function. When the colors are set inside the function, we would need a new function each time we want to use different colors, it's like the colors are hard-coded into this function. This means we'd need to change the function signature to add the parameters, then, within the function we'd remove the code where we were initializing those colors.
Program using Populate2DArray
In the code example project below, the following code shows how we call the new function: So, basic PShape[][] shapeMatrix = new PShape[rows][cols]; Populate2DArray(shapeMatrix , rows, cols, cellSize);
Logic to render each PShape at a given x,y location
//This code should also be refactored into a function so we can call it multiple times to have the shapeMatrix drawn at different initial x,y positions. This is done in the following section: Function: DisplayShapeMatrix()
Last updated
Was this helpful?