2

I'm working with PyGame and attempting to create a zoomable/scaleable Mandelbrot Set. I have this set up for square windows and coordinates that only from -1 to 1 on both axes in the complex plane. The way I do this is for every pixel on the screen I call this function:

#Import pygame and initialize
xSize = 50
ySize = 50
scale = 20
size = width, height = (xSize * scale), (ySize * scale)
screen = pygame.display.set_mode(size)

def getCoords(x, y):
    complexX = (x/((xSize * scale)/2)) - 1
    complexY = (y/((ySize * scale)/2)) - 1
    return complexX, complexY

And here is the loop where I actually plot the pixels:

for y in range(0, (ySize * scale)):
    for x in range(0, (xSize * scale)):
        i = 0
        z = getCoords(x, y)
        complexNum = complex(z[0], z[1])
        zOld = 0
        blowsUp = False
        #Check to see if (z^2 + c) "blows up"
        if blowsUp: 
            screen.set_at((x, y), color1)
        else:
            screen.set_at((x, y), color0)

Essentially what I want to be able to do is to have two tuples (one for x and one for y) that contain the maximum and minimum values that get plotted from the complex plane (i.e. here I'm just plotting 1 to -1 on both the real and imaginary axes). I imagine that this would be done by editing the getCoords() function, but after much tinkering with the expression there I can't seem to find a way to do this properly.

martineau
  • 119,623
  • 25
  • 170
  • 301

1 Answers1

2

I think your question is only marginally related to pygame programming, and is really mostly a math problem.

If I've understood what you're trying to do correctly, essentially it amounts to mapping an integer range of 0..scale to a specified subrange within ±1.0 in both the x and y dimensions. Visualize it as the transformation of the x and y coordinates in one rectangular area or box so they fit within the boundaries of another.

Here's code showing the essence of the math involved.

(Note that the code shown (largely) follows the PEP 8 - Style Guide for Python Code, which I strongly suggest you read and start following.)

scale = 2
size_x, size_y = 15, 15
subrange_x, subrange_y = (-.20, .20), (-.20, .20)

delta_x, delta_y = (subrange_x[1] - subrange_x[0]), (subrange_y[1] - subrange_y[0])
scale_x, scale_y = (size_x * scale), (size_y * scale)

def get_coords(x, y):
    real = (x/scale_x * delta_x) + subrange_x[0]
    imag = (y/scale_y * delta_y) + subrange_y[0]
    return real, imag

for y in range(scale_y):
    z_values = []
    for x in range(scale_x):
        z = get_coords(x, y)
        complex_num = complex(z[0], z[1])
        z_values.append(f'{complex_num:.2f}')

    print(f'y={y:02}:', ' '.join(z_values))

Results printed:

y=00: -0.20-0.20j -0.19-0.20j -0.17-0.20j -0.16-0.20j -0.15-0.20j -0.13-0.20j -0.12-0.20j -0.11-0.20j -0.09-0.20j -0.08-0.20j -0.07-0.20j -0.05-0.20j -0.04-0.20j -0.03-0.20j -0.01-0.20j 0.00-0.20j 0.01-0.20j 0.03-0.20j 0.04-0.20j 0.05-0.20j 0.07-0.20j 0.08-0.20j 0.09-0.20j 0.11-0.20j 0.12-0.20j 0.13-0.20j 0.15-0.20j 0.16-0.20j 0.17-0.20j 0.19-0.20j
y=01: -0.20-0.19j -0.19-0.19j -0.17-0.19j -0.16-0.19j -0.15-0.19j -0.13-0.19j -0.12-0.19j -0.11-0.19j -0.09-0.19j -0.08-0.19j -0.07-0.19j -0.05-0.19j -0.04-0.19j -0.03-0.19j -0.01-0.19j 0.00-0.19j 0.01-0.19j 0.03-0.19j 0.04-0.19j 0.05-0.19j 0.07-0.19j 0.08-0.19j 0.09-0.19j 0.11-0.19j 0.12-0.19j 0.13-0.19j 0.15-0.19j 0.16-0.19j 0.17-0.19j 0.19-0.19j
y=02: -0.20-0.17j -0.19-0.17j -0.17-0.17j -0.16-0.17j -0.15-0.17j -0.13-0.17j -0.12-0.17j -0.11-0.17j -0.09-0.17j -0.08-0.17j -0.07-0.17j -0.05-0.17j -0.04-0.17j -0.03-0.17j -0.01-0.17j 0.00-0.17j 0.01-0.17j 0.03-0.17j 0.04-0.17j 0.05-0.17j 0.07-0.17j 0.08-0.17j 0.09-0.17j 0.11-0.17j 0.12-0.17j 0.13-0.17j 0.15-0.17j 0.16-0.17j 0.17-0.17j 0.19-0.17j
y=03: -0.20-0.16j -0.19-0.16j -0.17-0.16j -0.16-0.16j -0.15-0.16j -0.13-0.16j -0.12-0.16j -0.11-0.16j -0.09-0.16j -0.08-0.16j -0.07-0.16j -0.05-0.16j -0.04-0.16j -0.03-0.16j -0.01-0.16j 0.00-0.16j 0.01-0.16j 0.03-0.16j 0.04-0.16j 0.05-0.16j 0.07-0.16j 0.08-0.16j 0.09-0.16j 0.11-0.16j 0.12-0.16j 0.13-0.16j 0.15-0.16j 0.16-0.16j 0.17-0.16j 0.19-0.16j
y=04: -0.20-0.15j -0.19-0.15j -0.17-0.15j -0.16-0.15j -0.15-0.15j -0.13-0.15j -0.12-0.15j -0.11-0.15j -0.09-0.15j -0.08-0.15j -0.07-0.15j -0.05-0.15j -0.04-0.15j -0.03-0.15j -0.01-0.15j 0.00-0.15j 0.01-0.15j 0.03-0.15j 0.04-0.15j 0.05-0.15j 0.07-0.15j 0.08-0.15j 0.09-0.15j 0.11-0.15j 0.12-0.15j 0.13-0.15j 0.15-0.15j 0.16-0.15j 0.17-0.15j 0.19-0.15j
y=05: -0.20-0.13j -0.19-0.13j -0.17-0.13j -0.16-0.13j -0.15-0.13j -0.13-0.13j -0.12-0.13j -0.11-0.13j -0.09-0.13j -0.08-0.13j -0.07-0.13j -0.05-0.13j -0.04-0.13j -0.03-0.13j -0.01-0.13j 0.00-0.13j 0.01-0.13j 0.03-0.13j 0.04-0.13j 0.05-0.13j 0.07-0.13j 0.08-0.13j 0.09-0.13j 0.11-0.13j 0.12-0.13j 0.13-0.13j 0.15-0.13j 0.16-0.13j 0.17-0.13j 0.19-0.13j
y=06: -0.20-0.12j -0.19-0.12j -0.17-0.12j -0.16-0.12j -0.15-0.12j -0.13-0.12j -0.12-0.12j -0.11-0.12j -0.09-0.12j -0.08-0.12j -0.07-0.12j -0.05-0.12j -0.04-0.12j -0.03-0.12j -0.01-0.12j 0.00-0.12j 0.01-0.12j 0.03-0.12j 0.04-0.12j 0.05-0.12j 0.07-0.12j 0.08-0.12j 0.09-0.12j 0.11-0.12j 0.12-0.12j 0.13-0.12j 0.15-0.12j 0.16-0.12j 0.17-0.12j 0.19-0.12j
y=07: -0.20-0.11j -0.19-0.11j -0.17-0.11j -0.16-0.11j -0.15-0.11j -0.13-0.11j -0.12-0.11j -0.11-0.11j -0.09-0.11j -0.08-0.11j -0.07-0.11j -0.05-0.11j -0.04-0.11j -0.03-0.11j -0.01-0.11j 0.00-0.11j 0.01-0.11j 0.03-0.11j 0.04-0.11j 0.05-0.11j 0.07-0.11j 0.08-0.11j 0.09-0.11j 0.11-0.11j 0.12-0.11j 0.13-0.11j 0.15-0.11j 0.16-0.11j 0.17-0.11j 0.19-0.11j
y=08: -0.20-0.09j -0.19-0.09j -0.17-0.09j -0.16-0.09j -0.15-0.09j -0.13-0.09j -0.12-0.09j -0.11-0.09j -0.09-0.09j -0.08-0.09j -0.07-0.09j -0.05-0.09j -0.04-0.09j -0.03-0.09j -0.01-0.09j 0.00-0.09j 0.01-0.09j 0.03-0.09j 0.04-0.09j 0.05-0.09j 0.07-0.09j 0.08-0.09j 0.09-0.09j 0.11-0.09j 0.12-0.09j 0.13-0.09j 0.15-0.09j 0.16-0.09j 0.17-0.09j 0.19-0.09j
y=09: -0.20-0.08j -0.19-0.08j -0.17-0.08j -0.16-0.08j -0.15-0.08j -0.13-0.08j -0.12-0.08j -0.11-0.08j -0.09-0.08j -0.08-0.08j -0.07-0.08j -0.05-0.08j -0.04-0.08j -0.03-0.08j -0.01-0.08j 0.00-0.08j 0.01-0.08j 0.03-0.08j 0.04-0.08j 0.05-0.08j 0.07-0.08j 0.08-0.08j 0.09-0.08j 0.11-0.08j 0.12-0.08j 0.13-0.08j 0.15-0.08j 0.16-0.08j 0.17-0.08j 0.19-0.08j
y=10: -0.20-0.07j -0.19-0.07j -0.17-0.07j -0.16-0.07j -0.15-0.07j -0.13-0.07j -0.12-0.07j -0.11-0.07j -0.09-0.07j -0.08-0.07j -0.07-0.07j -0.05-0.07j -0.04-0.07j -0.03-0.07j -0.01-0.07j 0.00-0.07j 0.01-0.07j 0.03-0.07j 0.04-0.07j 0.05-0.07j 0.07-0.07j 0.08-0.07j 0.09-0.07j 0.11-0.07j 0.12-0.07j 0.13-0.07j 0.15-0.07j 0.16-0.07j 0.17-0.07j 0.19-0.07j
y=11: -0.20-0.05j -0.19-0.05j -0.17-0.05j -0.16-0.05j -0.15-0.05j -0.13-0.05j -0.12-0.05j -0.11-0.05j -0.09-0.05j -0.08-0.05j -0.07-0.05j -0.05-0.05j -0.04-0.05j -0.03-0.05j -0.01-0.05j 0.00-0.05j 0.01-0.05j 0.03-0.05j 0.04-0.05j 0.05-0.05j 0.07-0.05j 0.08-0.05j 0.09-0.05j 0.11-0.05j 0.12-0.05j 0.13-0.05j 0.15-0.05j 0.16-0.05j 0.17-0.05j 0.19-0.05j
y=12: -0.20-0.04j -0.19-0.04j -0.17-0.04j -0.16-0.04j -0.15-0.04j -0.13-0.04j -0.12-0.04j -0.11-0.04j -0.09-0.04j -0.08-0.04j -0.07-0.04j -0.05-0.04j -0.04-0.04j -0.03-0.04j -0.01-0.04j 0.00-0.04j 0.01-0.04j 0.03-0.04j 0.04-0.04j 0.05-0.04j 0.07-0.04j 0.08-0.04j 0.09-0.04j 0.11-0.04j 0.12-0.04j 0.13-0.04j 0.15-0.04j 0.16-0.04j 0.17-0.04j 0.19-0.04j
y=13: -0.20-0.03j -0.19-0.03j -0.17-0.03j -0.16-0.03j -0.15-0.03j -0.13-0.03j -0.12-0.03j -0.11-0.03j -0.09-0.03j -0.08-0.03j -0.07-0.03j -0.05-0.03j -0.04-0.03j -0.03-0.03j -0.01-0.03j 0.00-0.03j 0.01-0.03j 0.03-0.03j 0.04-0.03j 0.05-0.03j 0.07-0.03j 0.08-0.03j 0.09-0.03j 0.11-0.03j 0.12-0.03j 0.13-0.03j 0.15-0.03j 0.16-0.03j 0.17-0.03j 0.19-0.03j
y=14: -0.20-0.01j -0.19-0.01j -0.17-0.01j -0.16-0.01j -0.15-0.01j -0.13-0.01j -0.12-0.01j -0.11-0.01j -0.09-0.01j -0.08-0.01j -0.07-0.01j -0.05-0.01j -0.04-0.01j -0.03-0.01j -0.01-0.01j 0.00-0.01j 0.01-0.01j 0.03-0.01j 0.04-0.01j 0.05-0.01j 0.07-0.01j 0.08-0.01j 0.09-0.01j 0.11-0.01j 0.12-0.01j 0.13-0.01j 0.15-0.01j 0.16-0.01j 0.17-0.01j 0.19-0.01j
y=15: -0.20+0.00j -0.19+0.00j -0.17+0.00j -0.16+0.00j -0.15+0.00j -0.13+0.00j -0.12+0.00j -0.11+0.00j -0.09+0.00j -0.08+0.00j -0.07+0.00j -0.05+0.00j -0.04+0.00j -0.03+0.00j -0.01+0.00j 0.00+0.00j 0.01+0.00j 0.03+0.00j .04+0.00j 0.05+0.00j 0.07+0.00j 0.08+0.00j 0.09+0.00j 0.11+0.00j 0.12+0.00j 0.13+0.00j 0.15+0.00j 0.16+0.00j 0.17+0.00j 0.19+0.00j
y=16: -0.20+0.01j -0.19+0.01j -0.17+0.01j -0.16+0.01j -0.15+0.01j -0.13+0.01j -0.12+0.01j -0.11+0.01j -0.09+0.01j -0.08+0.01j -0.07+0.01j -0.05+0.01j -0.04+0.01j -0.03+0.01j -0.01+0.01j 0.00+0.01j 0.01+0.01j 0.03+0.01j 0.04+0.01j 0.05+0.01j 0.07+0.01j 0.08+0.01j 0.09+0.01j 0.11+0.01j 0.12+0.01j 0.13+0.01j 0.15+0.01j 0.16+0.01j 0.17+0.01j 0.19+0.01j
y=17: -0.20+0.03j -0.19+0.03j -0.17+0.03j -0.16+0.03j -0.15+0.03j -0.13+0.03j -0.12+0.03j -0.11+0.03j -0.09+0.03j -0.08+0.03j -0.07+0.03j -0.05+0.03j -0.04+0.03j -0.03+0.03j -0.01+0.03j 0.00+0.03j 0.01+0.03j 0.03+0.03j 0.04+0.03j 0.05+0.03j 0.07+0.03j 0.08+0.03j 0.09+0.03j 0.11+0.03j 0.12+0.03j 0.13+0.03j 0.15+0.03j 0.16+0.03j 0.17+0.03j 0.19+0.03j
y=18: -0.20+0.04j -0.19+0.04j -0.17+0.04j -0.16+0.04j -0.15+0.04j -0.13+0.04j -0.12+0.04j -0.11+0.04j -0.09+0.04j -0.08+0.04j -0.07+0.04j -0.05+0.04j -0.04+0.04j -0.03+0.04j -0.01+0.04j 0.00+0.04j 0.01+0.04j 0.03+0.04j 0.04+0.04j 0.05+0.04j 0.07+0.04j 0.08+0.04j 0.09+0.04j 0.11+0.04j 0.12+0.04j 0.13+0.04j 0.15+0.04j 0.16+0.04j 0.17+0.04j 0.19+0.04j
y=19: -0.20+0.05j -0.19+0.05j -0.17+0.05j -0.16+0.05j -0.15+0.05j -0.13+0.05j -0.12+0.05j -0.11+0.05j -0.09+0.05j -0.08+0.05j -0.07+0.05j -0.05+0.05j -0.04+0.05j -0.03+0.05j -0.01+0.05j 0.00+0.05j 0.01+0.05j 0.03+0.05j 0.04+0.05j 0.05+0.05j 0.07+0.05j 0.08+0.05j 0.09+0.05j 0.11+0.05j 0.12+0.05j 0.13+0.05j 0.15+0.05j 0.16+0.05j 0.17+0.05j 0.19+0.05j
y=20: -0.20+0.07j -0.19+0.07j -0.17+0.07j -0.16+0.07j -0.15+0.07j -0.13+0.07j -0.12+0.07j -0.11+0.07j -0.09+0.07j -0.08+0.07j -0.07+0.07j -0.05+0.07j -0.04+0.07j -0.03+0.07j -0.01+0.07j 0.00+0.07j 0.01+0.07j 0.03+0.07j 0.04+0.07j 0.05+0.07j 0.07+0.07j 0.08+0.07j 0.09+0.07j 0.11+0.07j 0.12+0.07j 0.13+0.07j 0.15+0.07j 0.16+0.07j 0.17+0.07j 0.19+0.07j
y=21: -0.20+0.08j -0.19+0.08j -0.17+0.08j -0.16+0.08j -0.15+0.08j -0.13+0.08j -0.12+0.08j -0.11+0.08j -0.09+0.08j -0.08+0.08j -0.07+0.08j -0.05+0.08j -0.04+0.08j -0.03+0.08j -0.01+0.08j 0.00+0.08j 0.01+0.08j 0.03+0.08j 0.04+0.08j 0.05+0.08j 0.07+0.08j 0.08+0.08j 0.09+0.08j 0.11+0.08j 0.12+0.08j 0.13+0.08j 0.15+0.08j 0.16+0.08j 0.17+0.08j 0.19+0.08j
y=22: -0.20+0.09j -0.19+0.09j -0.17+0.09j -0.16+0.09j -0.15+0.09j -0.13+0.09j -0.12+0.09j -0.11+0.09j -0.09+0.09j -0.08+0.09j -0.07+0.09j -0.05+0.09j -0.04+0.09j -0.03+0.09j -0.01+0.09j 0.00+0.09j 0.01+0.09j 0.03+0.09j 0.04+0.09j 0.05+0.09j 0.07+0.09j 0.08+0.09j 0.09+0.09j 0.11+0.09j 0.12+0.09j 0.13+0.09j 0.15+0.09j 0.16+0.09j 0.17+0.09j 0.19+0.09j
y=23: -0.20+0.11j -0.19+0.11j -0.17+0.11j -0.16+0.11j -0.15+0.11j -0.13+0.11j -0.12+0.11j -0.11+0.11j -0.09+0.11j -0.08+0.11j -0.07+0.11j -0.05+0.11j -0.04+0.11j -0.03+0.11j -0.01+0.11j 0.00+0.11j 0.01+0.11j 0.03+0.11j 0.04+0.11j 0.05+0.11j 0.07+0.11j 0.08+0.11j 0.09+0.11j 0.11+0.11j 0.12+0.11j 0.13+0.11j 0.15+0.11j 0.16+0.11j 0.17+0.11j 0.19+0.11j
y=24: -0.20+0.12j -0.19+0.12j -0.17+0.12j -0.16+0.12j -0.15+0.12j -0.13+0.12j -0.12+0.12j -0.11+0.12j -0.09+0.12j -0.08+0.12j -0.07+0.12j -0.05+0.12j -0.04+0.12j -0.03+0.12j -0.01+0.12j 0.00+0.12j 0.01+0.12j 0.03+0.12j 0.04+0.12j 0.05+0.12j 0.07+0.12j 0.08+0.12j 0.09+0.12j 0.11+0.12j 0.12+0.12j 0.13+0.12j 0.15+0.12j 0.16+0.12j 0.17+0.12j 0.19+0.12j
y=25: -0.20+0.13j -0.19+0.13j -0.17+0.13j -0.16+0.13j -0.15+0.13j -0.13+0.13j -0.12+0.13j -0.11+0.13j -0.09+0.13j -0.08+0.13j -0.07+0.13j -0.05+0.13j -0.04+0.13j -0.03+0.13j -0.01+0.13j 0.00+0.13j 0.01+0.13j 0.03+0.13j 0.04+0.13j 0.05+0.13j 0.07+0.13j 0.08+0.13j 0.09+0.13j 0.11+0.13j 0.12+0.13j 0.13+0.13j 0.15+0.13j 0.16+0.13j 0.17+0.13j 0.19+0.13j
y=26: -0.20+0.15j -0.19+0.15j -0.17+0.15j -0.16+0.15j -0.15+0.15j -0.13+0.15j -0.12+0.15j -0.11+0.15j -0.09+0.15j -0.08+0.15j -0.07+0.15j -0.05+0.15j -0.04+0.15j -0.03+0.15j -0.01+0.15j 0.00+0.15j 0.01+0.15j 0.03+0.15j 0.04+0.15j 0.05+0.15j 0.07+0.15j 0.08+0.15j 0.09+0.15j 0.11+0.15j 0.12+0.15j 0.13+0.15j 0.15+0.15j 0.16+0.15j 0.17+0.15j 0.19+0.15j
y=27: -0.20+0.16j -0.19+0.16j -0.17+0.16j -0.16+0.16j -0.15+0.16j -0.13+0.16j -0.12+0.16j -0.11+0.16j -0.09+0.16j -0.08+0.16j -0.07+0.16j -0.05+0.16j -0.04+0.16j -0.03+0.16j -0.01+0.16j 0.00+0.16j 0.01+0.16j 0.03+0.16j 0.04+0.16j 0.05+0.16j 0.07+0.16j 0.08+0.16j 0.09+0.16j 0.11+0.16j 0.12+0.16j 0.13+0.16j 0.15+0.16j 0.16+0.16j 0.17+0.16j 0.19+0.16j
y=28: -0.20+0.17j -0.19+0.17j -0.17+0.17j -0.16+0.17j -0.15+0.17j -0.13+0.17j -0.12+0.17j -0.11+0.17j -0.09+0.17j -0.08+0.17j -0.07+0.17j -0.05+0.17j -0.04+0.17j -0.03+0.17j -0.01+0.17j 0.00+0.17j 0.01+0.17j 0.03+0.17j 0.04+0.17j 0.05+0.17j 0.07+0.17j 0.08+0.17j 0.09+0.17j 0.11+0.17j 0.12+0.17j 0.13+0.17j 0.15+0.17j 0.16+0.17j 0.17+0.17j 0.19+0.17j
y=29: -0.20+0.19j -0.19+0.19j -0.17+0.19j -0.16+0.19j -0.15+0.19j -0.13+0.19j -0.12+0.19j -0.11+0.19j -0.09+0.19j -0.08+0.19j -0.07+0.19j -0.05+0.19j -0.04+0.19j -0.03+0.19j -0.01+0.19j 0.00+0.19j 0.01+0.19j 0.03+0.19j 0.04+0.19j 0.05+0.19j 0.07+0.19j 0.08+0.19j 0.09+0.19j 0.11+0.19j 0.12+0.19j 0.13+0.19j 0.15+0.19j 0.16+0.19j 0.17+0.19j 0.19+0.19j
martineau
  • 119,623
  • 25
  • 170
  • 301
  • I think you understand most of what I'm trying to do, but I'm having a bit of trouble implementing your code into my program. I can't find a way to simply display the entire set (i.e. setting both subsets to `(-.2, .2)` does not yield a centered image even though 0,0 should be the center of the image. I appreciate your help on this! Image of `(-.2, .2)`: https://imgur.com/a/2DTc0L9 – PhysicsLover999 Oct 27 '19 at 02:36
  • @PhysicsLover999: Think I got it now. See recent update. – martineau Oct 27 '19 at 07:49
  • @PhysicsLover999: In case it's not obvious, the values of `scale_x * delta_x` and `scale_y * delta_y` are always to same whenever `get_coords()` is called. Since I expect that will be done many, many, times, it would probably be worthwhile to optimize things by calculating and storing both of them in global variables instead of doing the computations everytime it's called. – martineau Oct 27 '19 at 20:53