1D - Array of PShapes for Grid Layout
The first approach for creating grid designs will use a 1-dimensional Array
of PShape Objects and nested for-loops
to control x,y positioning of each PShape object. Arrays are a data structure, a structure for storing data. In this case, our 'data' is PShape objects that represent a graphical element we can render on the canvas, for now, we just want to create the PShape objects by setting the vertices and fill properties, we can access the data elements at a later time, to display them on the canvas.
Declare and Initialize an Array of PShape Objects
The syntax for an array of PShape objects is:
Declare the array:
PShape[ ] myShapes;
Initialize the array by specifying the number of elements.
myShapes = new PShape[ 10 ];
Initialize each element in the array
myShapes[ 0 ] = createShape( RECT, 0,0,30,30);
Remember: array index values range from( 0 to length-1)
myShapes[ 1] //is the second element in the array.
Array - Declare and Initialize - Example Code
Initialize the PShape Array Elements
After we initialize the array object, then we need to set each array element so that it contains some valid value. We'll almost always use a for-loop to modify and access each array element.
Iteration / Repetition
Iteration or Repetition - When working with arrays, we'll often use some type of looping structure so we can do some task with each element of the array. This repetition is also called iteration, and it's a fundamental control-structure used in programming. In the example below,
Pass Array into Functions
Let's put this logic into a function: Since arrays are objects, when we pass an object into a function we are actually passing the address of the object into the function, so anything done to elements of an array within a function are persisted to the object itself. This is a good thing for us.
Display our shapes
In the code above, we've just stored a bunch of PShape objects, but we haven't drawn anything to the canvas using the shape( s, x, y) function for PShapes. Our shapes are stored in a 1-Dimensional array, we can think of that as a long vertical list of PShape items, with one PShape item in each row of the list.
Grid: Nested for-loops
A pair of for-loops will allow us to iterate
through the array to select each PShape object and set it's xy position for display.
We'll create nested for-loops:
the outer for-loop controls which row is being created [ i ]
the inner loop creates determines which column [ j ]
For a single item in the grid, we can refer to that item as having grid coordinates: [ row: i, column: j ].
Rows: i, Columns: j and Row-Major Order
The code below shows nested for-loops to change xPos and yPos in a grid pattern, each time the inner loop code is executed, a single shape is displayed: NOTE: In the textbook, Shiffman uses the opposite convention for specifying rows, columns in section 13-9. Processing.org examples use the same syntax as I am using. These 2 approaches are termed - row-major, or column-major ordering. Row and Column Order, wikipedia
We will follow the convention that the outer for-loop (i)
is specifying the rows, and the inner for-loop (j)
specifies the columns in a grid.
Display a single PShape from our shapes array:
shape( shapes[ shapeListIndex ], xPos, yPos);
Code to display
Complete Code
Last updated