import pygame
from random import random, randint

"""a = random()
b = random()"""
a,b = 0.36252875310307275, 0.030707778123997054

width = 1500
height = 500
xmin = -2.5
xmax = 2.5
ymin = -1.25
ymax = 1.25
iterationmax = 50
COEF_R, COEF_G, COEF_B = 33, 10, 90#randint(0,100), randint(0,100), randint(0,100)

pygame.init()
screen = pygame.display.set_mode((width,height))
rectScreen = screen.get_rect()
#pygame.display.set_caption("Fractale de Julia")
"""
police = pygame.font.Font(None,18)
texte = police.render( f"a = {a}, b = {b}", True , pygame.Color('#ffffff') )
texte2 = police.render( f"R = {COEF_R}, G = {COEF_G}, B = {COEF_B}", True , pygame.Color('#ffffff') )
rectTexte = texte.get_rect()
rectTexte.bottom = rectScreen.bottom
rectTexte2 = texte2.get_rect()
rectTexte2.top = rectScreen.top"""


for x in range(width):
    for y in range(height):
        i = 1
        xM = xmin + x * ( xmax - xmin ) / width
        yM = ymax - y * ( ymax - ymin) / height
        while i<=iterationmax and (xM**2+yM**2)<=4:
            stock = xM
            xM = xM**2-yM**2+a
            yM = 2*stock*yM+b
            i += 1
        if i>iterationmax and (x**2+y**2)<=4:
            screen.set_at((x, y), (0, 0, 0))
        else:
            screen.set_at((x, y),((COEF_R * i) % 256, (COEF_G * i) % 256, (COEF_B * i) % 256))
            
"""screen.blit(texte,rectTexte)
screen.blit(texte2,rectTexte2)"""
pygame.image.save(screen, "image.jpg")

pygame.display.flip()
loop = True
while loop:
    for event in pygame.event.get():
        if event.type == pygame.QUIT: # Pour quitter l'application en fermant la fenêtre
            loop = False

pygame.quit()
