tabreturn.github.io

# Processing.py in Ten Lessons – 4.2: Transformations

2018-08-11

## Transformations

Processing’s transform functions provide a convenient means of dealing with complex geometric transformations. Such operations include scaling, rotation, shearing, and translation. Suppose that you wish to rotate and scale the star shape depicted at the top-left, such that it results in the bottom-right:

The code for the untransformed star’s ten vertices look something like this:

To calculate the coordinates for the rotated + scaled vertex positions, a matrix is required.

### Matrices

In mathematics, a matrix (plural: matrices) is a rectangular array of values. As an example, here’s a two-by-three matrix (2 rows, 3 columns):

$\left[\begin{array}{ccc}2& 5& 12\\ 19& 9& 7\end{array}\right]$

Given that digital images are essentially rectangular grids of pixels, it’s easy to imagine how matrices are used extensively in graphics processing. That said, matrices are applied in various fields of mathematics and other sciences.

To step you through how matrices operate, we’ll take a look at a few practical examples. Things get pretty mathematical here, but the idea is to grasp a matrix as a concept. Following this section, you’ll be introduced to Processing’s built-in matrix functions – meaning you’ll no longer need to perform the calculations yourself.

Create a new sketch and save it as “matrices”. Within the sketch’s folder, create a “data” subfolder containing a copy of the grid.png and grid-overlay.png files:

In Python, semicolons (;) serve as a substitute for new lines. The x/y/w/h variable assignments have been arranged in the above manner as a matter of style. If you wish to avoid the semicolons, you may write each variable on its own line. The x/y values represent the coordinates for the top-left corner of the square; the w/h variables, its width and height. Run the sketch and confirm that your display window matches the image below.

Matrices operate on vertices, whereas the rect() function does not. To move forward, substitute the rect() function with a quad().

The output appears the same but is now based on vertices. Of course, this code does not make use of Processing’s vertex() function, but each x-y pair of values comprises a vertex nonetheless.

#### Translate

The first transformation you’ll perform is a translation; this involves moving the square a given distance in some direction. It’s an easy enough task to accomplish without a matrix, but also ideal for introducing how matrices work.

Firstly, take note of how the top-left vertex (x, y) determines the positions of the other three vertices. Therefore the only matrix operation you need to perform is on the top-left vertex – the remaining vertices can be calculated by adding the relevant w and h values. The matrix you are manipulating can, hence, be expressed as:

$\left[\begin{array}{c}x\\ y\end{array}\right]$

Or, if you substitute the variable values:

$\left[\begin{array}{c}400\\ 200\end{array}\right]$

To translate a matrix, add (or subtract) another matrix. To perform a matrix addition, add-up the values of each row:

$\left[\begin{array}{c}x\\ y\end{array}\right]+\left[\begin{array}{c}a\\ b\end{array}\right]=\left[\begin{array}{c}x+a\\ y+b\end{array}\right]$

Append the following code – namely: two new variables representing values a and b (the distance the shape must move horizontally and vertically); a yellow stroke; and a new quad() that integrates the matrix calculations within its arguments:

Expressed in matrix notation, this is:

$\left[\begin{array}{c}400\\ 200\end{array}\right]+\left[\begin{array}{c}100\\ -80\end{array}\right]=\left[\begin{array}{c}400+100\\ 200+-80\end{array}\right]$

Run the sketch. The new yellow square is drawn 100 pixels further right and 80 pixels closer to the top edge than the original.

#### Scale

To scale a shape, one must multiply the matrix you intend to transform by one describing a transformation. In mathematical notation, this can be expressed as:

$\left[\begin{array}{c}x\\ y\end{array}\right]×\left[\begin{array}{cc}a& b\\ c& d\end{array}\right]$

And this is the point where the power of matrices becomes evident! Depending on the values you substitute for a, b, c, and d, the result will be either a scale, reflect, squeeze, rotate, or shear operation. Take a moment to study how matrix multiplication is performed:

$\left[\begin{array}{c}x\\ y\end{array}\right]×\left[\begin{array}{cc}a& b\\ c& d\end{array}\right]=\left[\begin{array}{c}\left(x·a\right)+\left(y·b\right)\\ \left(x·c\right)+\left(y·d\right)\end{array}\right]$

To perform a scale, value a multiplies the width, and value d multiplies the height. To half the square’s width and height, use a matrix where both a and d are equal to 0.5:

$\left[\begin{array}{c}400\\ 200\end{array}\right]×\left[\begin{array}{cc}0.5& 0\\ 0& 0.5\end{array}\right]=\left[\begin{array}{c}\left(400×0.5\right)+\left(\mathrm{200}×0\right)\\ \left(400×0\right)+\left(\mathrm{200}×0.5\right)\end{array}\right]$

Add some code that draws a new orange square half the size of the white one:

Run the sketch. The orange square is half the size of the white square, but the position has also changed:

To reveal why the position changes, add the grid-overlay.png file to your code – but halve its size using a third and fourth image() argument:

As evidenced by the grid overlay, when one performs a scale transformation, the entire grid upon which the shape is plotted scales toward the origin (0,0).

Scaling need not be proportionate. Comment out the grid-overlay image, and adjust your a and b variables for a distorted square:

Expressed in matrix notation, this is:

$\left[\begin{array}{c}400\\ 200\end{array}\right]×\left[\begin{array}{cc}0.3& 0\\ 0& 1.8\end{array}\right]=\left[\begin{array}{c}\left(400×0.3\right)+\left(\mathrm{200}×0\right)\\ \left(400×0\right)+\left(\mathrm{200}×1.8\right)\end{array}\right]$

#### Reflect

Reflecting a shape is a matter of scaling one axis by a negative value; then multiplying the other by 1.

For a horizontal reflection use:

$\left[\begin{array}{c}x\\ y\end{array}\right]×\left[\begin{array}{cc}-a& 0\\ 0& 1\end{array}\right]$

And for a vertical reflection:

$\left[\begin{array}{c}x\\ y\end{array}\right]×\left[\begin{array}{cc}1& 0\\ 0& -d\end{array}\right]$

Be aware, though: reflection operations are relative to the origin (0,0). As an example, add a new red square that’s a horizontal reflection of the white original:

Expressed in matrix notation, this is:

$\left[\begin{array}{c}400\\ 200\end{array}\right]×\left[\begin{array}{cc}-1& 0\\ 0& 1\end{array}\right]=\left[\begin{array}{c}\left(400×-1\right)+\left(\mathrm{200}×0\right)\\ \left(400×0\right)+\left(\mathrm{200}×1\right)\end{array}\right]$

The result is a square with a width of -200, drawn from a starting coordinate of (-400,200). As this lies beyond left-edge of the display window, it cannot be seen.

#### Rotate

Rotation transformations require the trigonometric functions cos() and sin(). Recall, though, that Processing deals in radians rather than degrees, so any arguments you pass these functions must be expressed in radians.

Add a pink version of the white square, rotated 45 degrees (roughly 0.785 radians).

Expressed in matrix notation, this is:

$\left[\begin{array}{c}400\\ 200\end{array}\right]×\left[\begin{array}{cc}cos\left(0.785\right)& -sin\left(0.785\right)\\ sin\left(0.785\right)& cos\left(0.785\right)\end{array}\right]$

The result is a ‘top-left’ coordinate of roughly (141, 424).

$\left[\begin{array}{c}282.96+-141.37\\ 282.73+141.47\end{array}\right]=\left[\begin{array}{c}141.58\\ 424.2\end{array}\right]$

As with the other transformations, the axis runs through (0,0). To better visualise this, here’s the rotation represented with a grid-overlay graphic:

#### Shear

Shearing a shape slants/skews it along the horizontal or vertical axis. The area (or volume), however, remains constant.

To shear horizontally, use:

$\left[\begin{array}{c}x\\ y\end{array}\right]×\left[\begin{array}{cc}1& b\\ 0& 1\end{array}\right]$

And for a vertical shear:

$\left[\begin{array}{c}x\\ y\end{array}\right]×\left[\begin{array}{cc}1& 0\\ c& 1\end{array}\right]$

Add a red version of the white square, sheared vertically using a coefficient of 0.4.

Expressed in matrix notation, this is:

$\left[\begin{array}{c}400\\ 200\end{array}\right]×\left[\begin{array}{cc}1& 0\\ 0.4& 1\end{array}\right]=\left[\begin{array}{c}\left(400×1\right)+\left(\mathrm{200}×0\right)\\ \left(400×0.4\right)+\left(\mathrm{200}×1\right)\end{array}\right]$

Below is the result, along with a grid-overlay and x-y coordinate label:

This section has introduced some fundamental transformation concepts using matrices while avoiding anything larger than 2 × 2 matrix. If you want to handle larger matrices, take a look at Python’s NumPy library.

In programming, a library is a collection of prewritten code. Instead of you having to write everything from scratch, Python provides a system for utilising packages of other people’s code. We’ll be leveraging a few different libraries in these lessons.

Next up: performing all of the above transformations without having to worry about the math!

#### Processing Transform Functions

After having done transformations the hard way, grasping Processing’s transform functions is a breeze.

Create a new sketch and save it as “transform_functions”. Within the sketch’s folder, create a “data” subfolder containing a copy of the grid.png and grid-overlay.png files:

Add the following setup code (note that grid-overlay.png is loaded but not drawn):

Add a white rectangle, drawn in the same position as that of the last sketch. Because you’ll be using Processing’s built-in transform functions, a rect() will work fine, i.e. there’s no need to use a quad() unless you prefer to.

Add a translate(100,-80) function and a duplicate rect() in yellow:

Add a square, 100 × 100 pixels in width/height, with an x-y coordinate of zero:

Run the sketch. The red square can be seen cut-off near the top-left of the display window. The shapes do not change position – rather it’s the coordinate system that does. To better visualise this behaviour, draw the grid-overlay.png after the translate() function:

To return the red square to the top-left corner, one could add a translate(-100,80) to offset the prior one – or better yet, isolate the translation. Wrap the translate, image, and yellow square code in a pushMatrix() and popMatrix() function. These will create a new matrix stack within which only the nested shapes (a yellow square, for now) are translated:

This makes for a useful drawing approach – moving the coordinate system to avoid keeping track using variables, then adding a popMatrix() to return to the original coordinate system. It ultimately depends on what works best for what you are drawing.

In addition to translate(), Processing’s 2D transform functions include, rotate(), scale(), shearX(), and shearY().

What is more, you may combine these as you wish. Add further transform functions to the existing matrix stack:

You’ll be using a mix of coordinate tracking and transformation techniques in the tasks to come.