The first sketch — drawing and the coordinate system

Environment:
the examples in this lesson are written for Processing Python Mode. If you work in the recommended environment Thonny + py5, most of the code stays the same, and names with a capital letter in the middle are written in lower case with an underscore: mouseX → mouse_x, noStroke() → no_stroke(). See the table of differences.

Lesson 4 of 20 · Python and Processing

What is a sketch?


In the Processing environment every program is called a sketch. It is a small graphical program that draws shapes in a separate window.

A sketch usually has two main functions:

  • setup() — runs once at the start
  • draw() — runs many times a second

In this way we can make animations and interactive graphics.

Note:

In Processing the screen is redrawn all the time. The function draw() usually runs about 60 times a second. This means that we can change the positions of objects and make animations.

If you have not installed Processing with the Python Mode add-on yet, see the lesson:

Installing Processing 4 and Python Mode

The parts of the Processing environment


When you open the Processing IDE, you will see three basic parts:

  • the Run button (▶) — starts the program
  • the Editor — this is where you write code
  • the Console — shows messages and errors

If the program has an error, Processing will show an explanation in the Console window.

Tip: Before you write your first sketch, check that Python Mode is selected.

A minimal Processing program



# setup() runs once at the start of the program
def setup():

    # the size of the window (width, height)
    size(600, 400)

# draw() runs again and again in a loop
def draw():

    # the background colour (light grey)
    background(220)

Experiment:

Change the size of the window.

size(800, 500)
Run the program again and watch the change. Also try these values:
  • 400 × 400
  • 1000 × 600

A program without draw()


If we do not want animation, we can use only setup().



def setup():

    size(600, 400)

    # the background is drawn only once
    background(220)

The program is drawn only once, without being refreshed.


The coordinate system in Processing


Processing uses a coordinate system based on pixels.

  • (0,0) — the top left corner
  • the x axis goes from left → to right
  • the y axis goes from the top → down

This is different from the mathematical coordinate system, where the origin is in the centre.

For example, in a 600 × 400 window the centre point is about:

  • (300, 200)

Where do width, height and mouseX come from?

You may wonder how we know where the centre of the screen is — and why we can use things like width, height or mouseX without defining them.

This is because Processing already has built-in (ready-made) variables and functions that you can use straight away.

  • width — the width of the window
  • height — the height of the window
  • mouseX, mouseY — the current position of the mouse

You can think of them as information that the program already knows and keeps updating.

For example, you can get the centre of the screen like this:


ellipse(width/2, height/2, 100, 100)
Mini experiment:

Try printing these values:

print(width)
print(height)
Run the program and look at the Console.

Important: these are not variables that we create in Python — they come from the Processing environment.

If you want to understand how variables are created in Python, see: Variables and data types in Python


How to explore on your own?

You do not have to remember everything — programmers often look at the documentation.

The official Processing reference:
https://processing.org/reference/

There you can find:

  • how each function works (for example ellipse, rect, line)
  • which parameters it takes
  • more examples

Try it yourself: type "processing ellipse" or "processing mouseX" into a search engine and see what you get.

In the Processing environment you can open the documentation quickly:

  • Right-click on a function (for example ellipse)
  • Choose Find in Reference

Processing will open the matching page of the documentation.

A right-click in the Processing Python Mode editor with the Find in Reference option for opening the documentation of a function
Picture 1: Right-clicking on a function in Processing and choosing "Find in Reference" quickly opens the documentation for that function.
Try it yourself:

Change the coordinates of the circle and watch how it moves around the screen.
  • ellipse(100, 100, 100, 100)
  • ellipse(500, 300, 100, 100)
Also try this:
  • move the circle to the top left corner
  • move the circle to the bottom right corner
  • put the circle exactly in the centre of the screen

Drawing a circle



def setup():

    size(600, 400)

def draw():

    background(240)

    # ellipse(x, y, width, height)
    # x and y are the centre of the circle
    ellipse(300, 200, 100, 100)

If the width and the height are the same — we get a circle.


Drawing a rectangle



def setup():

    size(600, 400)

def draw():

    background(240)

    # rect(x, y, width, height)
    # (x,y) is the top left corner
    rect(250, 150, 100, 80)

Drawing a line



def setup():

    size(600, 400)

def draw():

    background(240)

    # line(x1, y1, x2, y2)
    line(0, 0, 600, 400)

A line is drawn between two points.


Combining several shapes



def setup():

    size(600, 400)

def draw():

    background(240)

    # head
    ellipse(300,120,80,80)

    # body
    rect(260,200,80,80)

    # ground line
    line(0,300,600,300)

Processing draws shapes in order — from the top to the bottom of the code.


A visual display of coordinates (grid)


The following program draws a grid that helps us understand the coordinate system.


def setup():

    size(600,400)

def draw():

    background(255)

    # vertical lines
    for x in range(0,600,50):
        line(x,0,x,400)

    # horizontal lines
    for y in range(0,400,50):
        line(0,y,600,y)

This grid makes it easier to see where the coordinates are.


The position of the mouse


Processing keeps track of the position of the mouse automatically.


def setup():

    size(600,400)

def draw():

    background(240)

    # the circle follows the position of the mouse
    ellipse(mouseX, mouseY, 50, 50)

The circle will move together with the mouse.


Mini task


Mini challenge

Draw a simple scene:
  • The sun — a circle
  • A house — a rectangle
  • A roof — a triangle
  • The ground — a line
  • A door — a small rectangle
Try to arrange the objects using coordinates.


Drawing the coordinate system with a function


Instead of repeating the same code all the time, we can make our own function.

For example, we will make a function that draws the X axis of the coordinate system.


def draw_axis():

    # changing the line colour (red)
    stroke(255, 0, 0)

    # the X axis (a horizontal line)
    line(0, height/2, width, height/2)

    # setting the colour back to black
    stroke(0)

Now we can call this function in draw():


def setup():
    size(600, 400)

def draw():
    background(240)

    draw_axis()

    # a circle in the centre
    ellipse(width/2, height/2, 100, 100)
Important:

When we change the colour with stroke(), that colour stays active. If we do not set the colour back to black, all the following shapes (for example the circle) will be drawn in that new colour. That is why we set the colour back after drawing the axis:
stroke(0)

Why do we use functions?

  • so that we do not repeat the same code
  • so that the program is clearer
  • so that it is easier to add new parts

This is the same concept of functions that exists in Python.

If you want to revise functions in Python, see: Python functions

Practice task:

Extend the function so that it also draws the Y axis (a vertical line). Hints:
  • the line goes from the top to the bottom of the screen
  • the x coordinate should be width/2

Next lesson

In the next lesson you will learn:
  • how to change colours
  • how to use fill() and stroke()
  • how to make nicer graphical scenes

Basic shapes and colours →


Related topics



The same in Processing with Java

The logic is the same; only the syntax of the language is different.

Check your knowledge

Open the quiz

Mini quiz: coordinates

1. The window is size(600, 400). Where is the centre?




2. What does rect(250, 150, 100, 80) draw?




3. What is width?