Translate function acting strangely (inverted)

Hi,

I’m learning processing and python with Peter Farrell Math Adventures. I’m coding Madlebrot set rendering and strangly I have to remove the
translate(width/2,height/2)
line to have the set origin centered. If I let this line in my code, the render will be in the right bottom (4th quadrant) which isn’t supposed. It is supposed to be the inverse instead. Here’s the complete code, thx if you can enlight me.

Here’s the complete code (cotes are in french):


from math import sqrt

def cAdd(a,b):
    '''Additionne deux nombres complexes'''
    return [a[0]+b[0],a[1]+b[1]]

def cMult(u,v):
    '''Retourne le produit de deux nombres complexes'''
    return [u[0]*v[0]-u[1]*v[1],u[1]*v[0]+u[0]*v[1]]

def magnitude(z):
    return sqrt(z[0]**2+z[1]**2)

def mandelbrot(z,num):
    '''Execute le processus num fois
    et retourne le nombre de divergences'''
    count = 0
    # Définit z1 en z
    z1 = z
    # Réitère num fois
    while count <= num:
        # Vérifie la divergence
        if magnitude(z1) > 2.0:
            # Retourne le nombre d'itérations pour lequel il diverge
            return count
        # Calcul le prochain z
        z1 = cAdd(cMult(z1,z1),z)
        count += 1
        #print(magnitude(z1))
    # Si z n'a pas divergé à la fin
    return num
    
# Intervalle de valeurs en x
xmin = -0.25
xmax = 0.25

# Intervalle des valeurs de y
ymin = -1
ymax = -0.5

# Calcule la longueur de l'intervalle
rangex = xmax - xmin
rangey = ymax - ymin

def setup():
    global xscl, yscl
    size(600, 600)
    colorMode(HSB)
    noStroke()
    xscl = float(rangex)/width
    yscl = float(rangey)/height

def draw():
    global xscl, yscl
    # L'origine au centre
    translate(width/2,height/2)
    # Parcourt les x, y de la grille
    for x in range(width):
        for y in range(height):
            z=[(xmin + x*xscl),
            (ymin + y*yscl)]
            # Les donne à la fonction mandlebrot()
            col = mandelbrot(z,100)
            # Si  mandelbrot() retourne 0
            if col == 100:
                fill(0) # Rend le rectangle noir
            else:
                fill(3*col,255,255) # Rend le rectangle blanc
            # Trace un petit rectangle
            rect(x,y,1,1)

Welcome @mangouste77

In Processing, the display window’s top-left corner has an x-y coordinate of (0, 0). The y-values increase as you move down so that the bottom-right corner of the display window has an x-y coordinate of (400, 400).

You can rearrange this for a standard Cartesian plane by flipping the y-axis and moving the origin to the centre of the display window:

size(400, 400)
# flip the y-axis
scale(1, -1)
translate(0, -height)
# move the origin to the centre of the display window
translate(width/2, height/2)
# draw a line from the origin to a point in the first quadrant
line(0, 0, 50, 50)
3 Likes