Skip to main content

Command Palette

Search for a command to run...

2D Transformations

Updated
•2 min read•View as Markdown
2D Transformations
P

Working as an online tutor for the Bachelor of Computer Science Degree from Goldsmiths, University of London on the Coursera platform. This is the first undergraduate degree programme available on Coursera, one of the world’s leading online education providers.

The programme has been designed to equip students to access careers in emerging technologies, providing opportunities for students to study machine learning, data science, virtual reality, game development and web programming to meet the needs of career changers in industry as well as those taking their first steps into the innovative computer science field.

The P5.js library has built-in Transform functions that make it easy to manipulate objects in a sketch, making them translate , rotate, and scale. This tutorial will introduce you to these functions so that you can use them in your sketches.

Before we begin, think back to your school days when you played with graph paper, and were learning the Cartesian coordinate system, back at school you were taught to place the origin (0,0) in the centre, with the y-axis pointing up and the x-axis pointing right

Cartesian-coordinate-system.png

In computing however, this is rarely the case. Most programming languages place the origin (0,0) at the top left, and the y-axis is reversed, this is also true for the Coordinate System and Shapes in p5.js .

p5-coordinate.png

So now you know this, lets write a simple function to actually draw this grid in a p5 sketch:

const grid = { size: 50, stroke: '#dff', colour:'teal'};

function setup() {
  createCanvas(400, 400);  
}

function draw() {
  background(220);
  drawGrid(grid);  
}

const drawGrid = g => {
  push();
    stroke(g.stroke);
    fill(g.colour);

    for (let lineX = -width; lineX < width; lineX += grid.size) {
      line(lineX, -height, lineX, height);
      text(lineX, lineX+1, 10);
    }

    for (let lineY = -height; lineY < height; lineY += grid.size) {
      line(-width, lineY, width, lineY);
      text(lineY, 1, lineY+10);
    }
  pop();
}

check out the examples in my p5 collection

Graphics Programming

Part 1 of 1

This module aims to show you how to work with images in a variety of ways. You will learn how to synthesise graphics and how to process visual signals.

More from this blog

pedbad

39 posts

Head of eLearning at the University of Cambridge Language Centre & Online Tutor for the BSc Computer Science, Goldsmiths, University of London.