Push and pop not working in p5py?

I’m using the python package p5python package p5

I’m following some tutorials written in javascript, but it seems like push and pop is not the same in the python package.

I’m using the push_matrix() and the pop_matrix() function.
As far as I understand this code shouldn’t do anything:

push()
translate(x,y)
pop()

Right? because there is nothing inside the push pop function.

But In this Python example:

translate(100,100)
pop_matrix()
circle((500,500),50)

The circle has moved to position 600,600. I thought it should be at 500,500.

Would love your input. Full code:

from p5 import *


def setup():
    size(1000, 1000)

def draw():
    background(255)
    fill(0)
    push_matrix()
    translate(100,100)
    pop_matrix()
    circle((500,500),50)

run()

1 Like

According to the documentation, this:

pushMatrix()
translate(width/2, height/2)
point(0, 0)
popMatrix()

becomes:

with push_matrix():
    translate(width/2, height/2)
    point(0, 0)

So in your case, you want something like this:

...

def draw():
    background(255)
    fill(0)

    with push_matrix():
        translate(100, 100)
        circle((500, 500), 50) # translated
    
    no_fill()
    circle((500, 500), 50) # centered

...
3 Likes

thank you very much for the solution @tabreturn :slight_smile:

1 Like