2D Arrays with lerpColor
The example code below shows how we can use the i, j indexes of a for-loop to create and display a grid color gradient using: lerpColor( ) and map( ). In each 2D array element, we're simply storing the value that we calculate for k, each time the inner for-loop code is executed, using the loop index variables: int k = i + j;
Diagonal Patterns using k
We can notice that the values of k in the grid forms 2 patterns:
Pattern1: k values across diagonal lines (from lower-left to upper-right) have the same value for k. We can use k to determine color, where each item along a diagonal will have the same color.
Pattern 2: k increases along diagonal (from upper-left to lower-right) k ranges from min value of:
k = 0
, to max value:k = rows+cols-2
. We can use this information to create a color gradient along that diagonal, since lerpColor can calculate a color gradient based on a linear mapping between 2 colors.
Example Code
Grid Gradient using lerpColor( ) and map( )
The image below shows i, j values and the corresponding lerpColor fractional amount that was calculated using the map function in the code above.
Last updated