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, yPos
variables 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 - 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