IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Contribuez Python Discussion :

fractals:Koch,C,sierpinski,mandelbrot,julias,courbe du dragon


Sujet :

Contribuez Python

  1. #1
    Membre régulier Avatar de fifafou
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Janvier 2016
    Messages
    173
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 22
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Janvier 2016
    Messages : 173
    Points : 92
    Points
    92
    Par défaut fractals:Koch,C,sierpinski,mandelbrot,julias,courbe du dragon
    Bonjour
    Ayant beaucoup travaillé sur les fractals,je vous présente tout mes résultats.
    Vous pouvez proposer d'autres codes ou des amélioration possibles.
    D'abord,l'ensemble de Mandelbrot
    Voici mon code
    Code Mandelbrot : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    from numpy import *
    import pygame
    from pygame.locals import *
    def pycode(c_1,c_2,c_3):
        return c_3+c_2*256+c_1*256**2
    couleur=zeros((150))
    ri,vi,bi=250,0,0
    for i in range(25):
        couleur[i]=pycode(ri,vi,bi)
        vi+=10
    for i in range(25,50):
        couleur[i]=pycode(ri,vi,bi)
        ri-=10
    for i in range(50,75):
        couleur[i]=pycode(ri,vi,bi)
        bi+=10
    for i in range(75,100):
        couleur[i]=pycode(ri,vi,bi)
        vi-=10
    for i in range(100,125):
        couleur[i]=pycode(ri,vi,bi)
        ri+=10
    for i in range(125,150):
        couleur[i]=pycode(ri,vi,bi)
        bi-=10
    iteration_max=input("iteration maxi")
    po=input("point")
    zoom=float(input("zoom"))
    eca=zoom/200
    x1 = po[0]-1.5/eca
    x2 = po[0]+1.5/eca
    y1 = po[1]-1.2/eca
    y2 = po[1]+1.2/eca
    image_x = (x2 - x1) * zoom
    image_y = (y2 - y1) * zoom
    a=zeros((image_x,image_y),dtype=uint32)
    for x in range(0,image_x):
        for y in range(0,image_y):
            c_r = x / zoom + x1
            c_i = y / zoom + y1
            z_r = 0
            z_i = 0
            i = 0
            while z_r*z_r + z_i*z_i <4 and i < iteration_max:
                tmp = z_r
                z_r = z_r*z_r - z_i*z_i + c_r
                z_i = 2*z_i*tmp + c_i
                i = i+1
            if i < iteration_max:
                a[x][y]=couleur[(i*2)%150]
    M=a
    hM=len(M)
    lM=len(M[0])
    print hM,lM
    pygame.init()
    fenetre = pygame.display.set_mode((hM,lM))
    pygame.display.set_caption('mandelbrot')
    pygame.surfarray.blit_array(fenetre,M)
    #pygame.image.save(fenetre,"mandelbrot.png")
    continuer = True
    while continuer:
        pygame.time.Clock().tick(1)
        pygame.display.flip()
        for event in pygame.event.get():	
    		if event.type == QUIT:
    			continuer = False
    pygame.quit()
    Voila celui des julias
    Code julias : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    from numpy import *
    import pygame
    from pygame.locals import *
    def pycode(c_1,c_2,c_3):
        return c_3+c_2*256+c_1*256**2
    couleur=zeros((150))
    ri,vi,bi=250,0,0
    for i in range(25):
        couleur[i]=pycode(ri,vi,bi)
        vi+=10
    for i in range(25,50):
        couleur[i]=pycode(ri,vi,bi)
        ri-=10
    for i in range(50,75):
        couleur[i]=pycode(ri,vi,bi)
        bi+=10
    for i in range(75,100):
        couleur[i]=pycode(ri,vi,bi)
        vi-=10
    for i in range(100,125):
        couleur[i]=pycode(ri,vi,bi)
        ri+=10
    for i in range(125,150):
        couleur[i]=pycode(ri,vi,bi)
        bi-=10
    iteration_max=input("iteration maxi")
    zoom=float(input("zoom"))
    point=input("point")
    eca=zoom/200
    x1 = -1.5
    x2 = 1.5
    y1 = -1.2
    y2 = 1.2
    image_x = (x2 - x1) * zoom
    image_y = (y2 - y1) * zoom
    a=zeros((image_x,image_y),dtype=uint32)
    for x in range(0,image_x):
        for y in range(0,image_y):
            c_r =point[0]
            c_i =point[1]
            z_r =  x / zoom + x1
            z_i = y / zoom + y1
            i = 0
            while z_r*z_r + z_i*z_i <4 and i < iteration_max:
                tmp = z_r
                z_r = z_r*z_r - z_i*z_i + c_r
                z_i = 2*z_i*tmp + c_i
                i = i+1
            if i < iteration_max:
                a[x][y]=couleur[(i*2)%150]
    M=a
    hM=len(M)
    lM=len(M[0])
    print hM,lM
    pygame.init()
    fenetre = pygame.display.set_mode((hM,lM))
    pygame.display.set_caption('julias')
    pygame.surfarray.blit_array(fenetre,M)
    #pygame.image.save(fenetre,"julias.png")
    continuer = True
    while continuer:
        pygame.time.Clock().tick(1)
        pygame.display.flip()
        for event in pygame.event.get():	
    		if event.type == QUIT:
    			continuer = False
    pygame.quit()
    Pour ces deux programmes,l'input("point") attend un tuple avec les deux coordonnées du point
    Pour vous aider a trouver des point pour les julias,j'ai aussi fait ce programme qui donne les coordonnées du point où l'on a cliqué.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    import pygame
    from pygame.locals import *
    from numpy import *
    def pycode(c_1,c_2,c_3):
        return c_3+c_2*256+c_1*256**2
    couleur=zeros((250))
    ri,vi,bi=0,0,0
    for i in range(35):
        couleur[i]=pycode(ri,vi,bi)
        ri+=7
    for i in range(35,71):
        couleur[i]=pycode(ri,vi,bi)
        vi+=7
    for i in range(71,107):
        couleur[i]=pycode(ri,vi,bi)
        ri-=7
    for i in range(107,142):
        couleur[i]=pycode(ri,vi,bi)
        bi+=7
    for i in range(142,178):
        couleur[i]=pycode(ri,vi,bi)
        vi-=7
    for i in range(178,214):
        couleur[i]=pycode(ri,vi,bi)
        ri+=7
    for i in range(214,250):
        couleur[i]=pycode(ri,vi,bi)
        vi+=7
    couleur[249]=0
    zoom=200.0
    iteration_max=50
    x1 = -2.5
    x2 = 0.5
    y1 = -1.2
    y2 = 1.2
    image_x = (x2 - x1) * zoom
    image_y = (y2 - y1) * zoom
    a=zeros((image_x,image_y),dtype=uint32)
    pas=250.0/iteration_max
    for x in range(0,image_x):
        #print x
        for y in range(0,image_y):
            c =complex( x / zoom + x1,y / zoom + y1)
            z =complex(0.0,0.0)
            i = 0
            while z.real**2+z.imag**2<4 and i < iteration_max:
                z=z*z+c
                i = i+1
            if i <= iteration_max:
                a[x][y]=couleur[(i*pas)-1]
    M=a
    hM=len(M)
    lM=len(M[0])
    pygame.init()
    print (hM,lM)
    fenetre = pygame.display.set_mode((hM,lM))
    pygame.surfarray.blit_array(fenetre,M)
    continuer = True
    pygame.display.flip()
    while continuer:
        for event in pygame.event.get():
            if event.type == QUIT:
                continuer = False
            if event.type == MOUSEBUTTONDOWN:
                print str(event.pos[0] / zoom + x1)+","+str(event.pos[1] / zoom + y1)
        pygame.display.flip()
    pygame.quit()
    Ensuite,pour la fractal du C et de Koch,j'ai fait ce programme qui dessine la fractal a partir de la figure de base.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    from numpy import *
    import pygame
    from pygame.locals import *
    M=(ones((950,550))*16777215)
    base=list(input("base"))
    for i in range(len(base)):
        base[i]=(base[i]/180.0)*pi
    n=len(base)
    it=input("iteration")
    lt=[]
    liste=base[:]
    for i in range(it):
        for j in range(n):
            for k in range(0,len(liste)):
                lt.append(liste[k]+base[j])
        liste=lt[:]
        lt=[]
    x=5
    y=5
    for i in range(len(liste)):
        x+=cos(liste[i])
        y+=sin(liste[i])
        try:
            M[int(x)][int(y)]=0
        except:
            pass
    hM=len(M)
    lM=len(M[0])
    print hM,lM
    pygame.init()
    fenetre = pygame.display.set_mode((hM,lM))
    pygame.display.set_caption('Fractal du C')
    pygame.surfarray.blit_array(fenetre,M)
    #pygame.image.save(fenetre,"frac_C.png")
    continuer = True
    while continuer:
        pygame.time.Clock().tick(1)
        pygame.display.flip()
        for event in pygame.event.get():
    		if event.type == QUIT:
    			continuer = False
    pygame.quit()
    exemples:pour la fractal du C,la base sera 0,90
    pour la fractal de Koch,la base sera 0,60,-60,0
    mais on peut en faire bien d'autres
    Pour la courbe du dragon,je me suis amusé a la colorer de différentes façon:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    from numpy import *
    import pygame
    from pygame.locals import *
    def pycode(c_1,c_2,c_3):
        return c_3+c_2*256+c_1*256**2
    couleur=zeros((150))
    ri,vi,bi=250,0,0
    for i in range(25):
        couleur[i]=pycode(ri,vi,bi)
        vi+=10
    for i in range(25,50):
        couleur[i]=pycode(ri,vi,bi)
        ri-=10
    for i in range(50,75):
        couleur[i]=pycode(ri,vi,bi)
        bi+=10
    for i in range(75,100):
        couleur[i]=pycode(ri,vi,bi)
        vi-=10
    for i in range(100,125):
        couleur[i]=pycode(ri,vi,bi)
        ri+=10
    for i in range(125,150):
        couleur[i]=pycode(ri,vi,bi)
        bi-=10
    M=(ones((950,550))*16777215)
    lx=[1]
    ly=[1]
    lc=[1]
    it=input("iteration")
    for k in range(it):
        ltc=lc[:]
        for i in range(0,len(ltc)):
            lc.append(1+ltc[len(ltc)-i-1])
    for k in range(it):
        ltx=lx[:]
        lty=ly[:]
        for i in range(0,len(ltx)):
            ly.append(ltx[len(ltx)-i-1])
        for i in range(0,len(lty)):
            lx.append(-lty[len(lty)-i-1])
    x=400
    y=275
    for i in range(len(lx)):
        x=x+lx[i]
        try:
            M[int(x)][int(y)]=couleur[(lc[i]*9)%250]
        except:
            pass
        y=y+ly[i]
        try:
            M[int(x)][int(y)]=couleur[(lc[i]*9)%250]
        except:
            pass
    hM=len(M)
    lM=len(M[0])
    print hM,lM
    pygame.init()
    fenetre = pygame.display.set_mode((hM,lM))
    pygame.display.set_caption('dragon')
    pygame.surfarray.blit_array(fenetre,M)
    continuer = True
    while continuer:
        pygame.time.Clock().tick(1)
        pygame.display.flip()
        for event in pygame.event.get():	#Attente des événements
    		if event.type == QUIT:
    			continuer = False
    pygame.quit()
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    from numpy import *
    import pygame
    from pygame.locals import *
    def pycode(c_1,c_2,c_3):
        return c_3+c_2*256+c_1*256**2
    couleur=zeros((150))
    ri,vi,bi=250,0,0
    for i in range(25):
        couleur[i]=pycode(ri,vi,bi)
        vi+=10
    for i in range(25,50):
        couleur[i]=pycode(ri,vi,bi)
        ri-=10
    for i in range(50,75):
        couleur[i]=pycode(ri,vi,bi)
        bi+=10
    for i in range(75,100):
        couleur[i]=pycode(ri,vi,bi)
        vi-=10
    for i in range(100,125):
        couleur[i]=pycode(ri,vi,bi)
        ri+=10
    for i in range(125,150):
        couleur[i]=pycode(ri,vi,bi)
        bi-=10
    M=(ones((950,550))*16777215)
    lx=[1]
    ly=[1]
    it=input("iteration")
    for k in range(it):
        ltx=lx[:]
        lty=ly[:]
        for i in range(0,len(ltx)):
            ly.append(ltx[len(ltx)-i-1])
        for i in range(0,len(lty)):
            lx.append(-lty[len(lty)-i-1])
    x=400
    y=275
    co=[0]
    for i in range(0,len(lx)-1):
        co.append(co[len(co)-1]+(lx[i]*-ly[i])*-10)
        co.append(co[len(co)-1]+(ly[i]*lx[i+1])*-10)
    print co
    for i in range(len(lx)):
        x=x+lx[i]
        try:
            M[int(x)][int(y)]=couleur[co[i]%150]
        except:
            pass
        y=y+ly[i]
        try:
            M[int(x)][int(y)]=couleur[co[i]%150]
        except:
            pass
    hM=len(M)
    lM=len(M[0])
    print hM,lM
    pygame.init()
    fenetre = pygame.display.set_mode((hM,lM))
    pygame.display.set_caption('Fractal du C')
    pygame.surfarray.blit_array(fenetre,M)
    continuer = True
    while continuer:
        pygame.time.Clock().tick(1)
        pygame.display.flip()
        for event in pygame.event.get():	
    		if event.type == QUIT:
    			continuer = False
    pygame.quit()
    finalement c'est quasiment la même chose(mais pas exactement)
    Sinon,il y a celui là qui est plus classique:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    from numpy import *
    import pygame
    from pygame.locals import *
    def pycode(c_1,c_2,c_3):
        return c_3+c_2*256+c_1*256**2
    couleur=zeros((150))
    ri,vi,bi=250,0,0
    for i in range(25):
        couleur[i]=pycode(ri,vi,bi)
        vi+=10
    for i in range(25,50):
        couleur[i]=pycode(ri,vi,bi)
        ri-=10
    for i in range(50,75):
        couleur[i]=pycode(ri,vi,bi)
        bi+=10
    for i in range(75,100):
        couleur[i]=pycode(ri,vi,bi)
        vi-=10
    for i in range(100,125):
        couleur[i]=pycode(ri,vi,bi)
        ri+=10
    for i in range(125,150):
        couleur[i]=pycode(ri,vi,bi)
        bi-=10
    M=(ones((950,550))*16777215)
    lx=[1]
    ly=[1]
    lc=[1]
    it=input("iteration")
    for k in range(it):
        ltx=lx[:]
        lty=ly[:]
        for i in range(0,len(ltx)):
            ly.append(ltx[len(ltx)-i-1])
        for i in range(0,len(lty)):
            lx.append(-lty[len(lty)-i-1])
    x=400
    y=275
    co=0
    for i in range(len(lx)):
        co+=0.0025
        x=x+lx[i]
        try:
            M[int(x)][int(y)]=couleur[co%150]
        except:
            pass
        y=y+ly[i]
        try:
            M[int(x)][int(y)]=couleur[co%150]
        except:
            pass
    hM=len(M)
    lM=len(M[0])
    print hM,lM
    pygame.init()
    fenetre = pygame.display.set_mode((hM,lM))
    pygame.display.set_caption('dragon')
    pygame.surfarray.blit_array(fenetre,M)
    continuer = True
    while continuer:
        pygame.time.Clock().tick(1)
        pygame.display.flip()
        for event in pygame.event.get():
    		if event.type == QUIT:
    			continuer = False
    pygame.quit()
    pour le triangle de Sierpiński,j'ai trouvé celui là qui marche bien ,sur internet dans un forum.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    from Tkinter import *
    def triangle(canevas,coord=((0,200),(100,0),(200,200)),color='#000000'):
            canevas.create_polygon(coord[0][0],coord[0][1],coord[1][0],coord[1][1],coord[2][0],coord[2][1],fill=color)
    def genereTriangle(canevas,larg=200,iteration=5):
            larg=float(larg)
            canevas.configure(height=larg,width=larg,bg='#00FF00')
            triangle(canevas,((0,larg),(larg/2,0),(larg,larg)),color='#FF0000')
            liste_1=[((larg/4,larg/2),(larg*3/4,larg/2),(larg/2,larg))]
            i=0
            larg=larg/2
            while i<iteration:
                    liste_2=[]
                    for coord in liste_1:
                            triangle(canevas,coord)
                            x,y=coord[0][0],coord[0][1]
                            haut=coord[2][1]-coord[0][1]
                            quart=larg/4
                            #definition des trois nouveau triangles
                            liste_2.append(((x+quart,y-(haut/2)),(x+(3*quart),y-(haut/2)),(x+(2*quart),y)))#triangle haut
                            liste_2.append(((x-quart,y+(haut/2)),(x+quart,y+(haut/2)),(x,y+haut)))#triangle bas gauche
                            liste_2.append(((x+(3*quart),y+(haut/2)),(x+(5*quart),y+(haut/2)),(x+(4*quart),y+haut)))#triangle bas droit
                    larg=larg/2
                    liste_1=liste_2[:]
                    i+=1
    taille=int(input('Entrez la largeur du triangle de sierpinski:'))
    iter=int(input('Entrez le nombre d\'iteration:'))
    fen=Tk()
    can=Canvas(fen)
    can.pack()
    genereTriangle(can,taille,iter)
    fen.mainloop()
    J'en ai aussi fait un avec une autre technique
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    from numpy import *
    import pygame
    from pygame.locals import *
    def test(x,y):
        if x==y:
            return 1
        else:
            return 0
    it=2**input("iterations")
    M=ones((it+1,it+1))
    M[0][1]=0
    for a in range(it):
        for b in range(1,a+1):
            M[b][a+1]=test(M[b-1][a],M[b][a])
    M*=16777215
    hM=len(M)
    lM=len(M[0])
    print hM,lM
    pygame.init()
    fenetre = pygame.display.set_mode((hM,lM))
    pygame.display.set_caption('sierpinski')
    pygame.surfarray.blit_array(fenetre,M)
    continuer = True
    while continuer:
        pygame.time.Clock().tick(1)
        pygame.display.flip()
        for event in pygame.event.get():
    		if event.type == QUIT:
    			continuer = False
    pygame.quit()
    Sinon,pour mandelbrot j'ai fait ce code la en C++ pour plus de performances
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    #include <SDL/SDL.h>
    int pycode(int c_1,int c_2,int c_3)
    {
        return c_3+c_2*256+c_1*256*256;
    }
    void definirPixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
    {
        int nbOctetsParPixel = surface->format->BytesPerPixel;
        Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * nbOctetsParPixel;
        switch(nbOctetsParPixel)
        {
            case 1:
                *p = pixel;
                break;
            case 2:
                *(Uint16 *)p = pixel;
                break;
            case 3:
                if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
                {
                    p[0] = (pixel >> 16) & 0xff;
                    p[1] = (pixel >> 8) & 0xff;
                    p[2] = pixel & 0xff;
                }
                else
                {
                    p[0] = pixel & 0xff;
                    p[1] = (pixel >> 8) & 0xff;
                    p[2] = (pixel >> 16) & 0xff;
                }
                break;
            case 4:
                *(Uint32 *)p = pixel;
                break;
        }
    }
    int main(int argc, char *argv[])
    {
        int ri=250;
        int vi=0;
        int bi=0;
        int i;
        int couleur[150];
        for (i=0;i<25;i++)
        {
            couleur[i]=pycode(ri,vi,bi);
            vi+=10;
        }
        for (;i<50;i++)
        {
            couleur[i]=pycode(ri,vi,bi);
            ri-=10;
        }
        for (;i<75;i++)
        {
            couleur[i]=pycode(ri,vi,bi);
            bi+=10;
        }
        for (;i<100;i++)
        {
            couleur[i]=pycode(ri,vi,bi);
            vi-=10;
        }
        for (;i<125;i++)
        {
            couleur[i]=pycode(ri,vi,bi);
            ri+=10;
        }
        for (;i<150;i++)
        {
            couleur[i]=pycode(ri,vi,bi);
            bi-=10;
        }
        double xa=0,ya=0;
        SDL_Surface *ecran = NULL;
        SDL_Event event;
        int continuer = 1;
        int tempsPrecedent = 0, tempsActuel = 0;
        SDL_Init(SDL_INIT_VIDEO);
        ecran = SDL_SetVideoMode(600, 480, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
        SDL_WM_SetCaption("Mandelbrot", NULL);
        int iteration_max=150;
        double zoom=200;
        double eca=zoom/200;
        double x1 =-1.5/eca;
        double x2 =1.5/eca;
        double y1 =-1.2/eca;
        double y2 =1.2/eca;
        int image_x = (x2 - x1) * zoom;
        int image_y = (y2 - y1) * zoom;
        unsigned int tab[600][480];
        for(int x=0;x<image_x;x++)
        {
            for(int y=0;y<image_y;y++)
            {
                double c_r = x / zoom + x1;
                double c_i = y / zoom + y1;
                double z_r = 0;
                double z_i = 0;
                int i = 0;
                while(z_r*z_r + z_i*z_i <4 && i < iteration_max)
                {
                    double tmp = z_r;
                    z_r = z_r*z_r - z_i*z_i + c_r;
                    z_i = 2*z_i*tmp + c_i;
                    i++;
                }
                if (i < iteration_max)
                    tab[x][y]=couleur[i%150];
            }
        }
        SDL_EnableKeyRepeat(3, 3);
        while (continuer)
        {
            SDL_PollEvent(&event);
            switch(event.type)
            {
                case SDL_QUIT:
                    continuer = 0;
                    break;
                case SDL_KEYDOWN:
                    switch (event.key.keysym.sym)
                    {
                        case SDLK_u:
                            iteration_max+=50;
                            break;
                        case SDLK_RALT:
                            zoom/=1.5;
                            break;
                        case SDLK_r:
                            zoom=200;
                            break;
                        case SDLK_LEFT:
                            xa-=50/zoom;
                            break;
                        case SDLK_RIGHT:
                            xa+=50/zoom;
                            break;
                        case SDLK_UP:
                            ya-=50/zoom;
                            break;
                        case SDLK_DOWN:
                            ya+=50/zoom;
                            break;
                        case SDLK_SPACE:
                            zoom*=1.5;
                            break;
                        case SDLK_BACKSPACE:
                            iteration_max+=20;
                            break;
                        case SDLK_e:
                            iteration_max-=50;
                            break;
                    }
                    long double eca=zoom/200;
                    long double x1 =xa-1.5/eca;
                    long double x2 =xa+1.5/eca;
                    long double y1 =ya-1.2/eca;
                    long double y2 =ya+1.2/eca;
                    int image_x = (x2 - x1) * zoom;
                    int image_y = (y2 - y1) * zoom;
                    for(int x=0;x<image_x;x++)
                    {
                        for(int y=0;y<image_y;y++)
                        {
                            long double c_r = x / zoom + x1,c_i = y / zoom + y1,z_r = 0,z_i = 0;
                            int i = 0;
                            while(z_r*z_r + z_i*z_i <4 && i < iteration_max)
                            {
                                long double tmp = z_r;
                                z_r = z_r*z_r - z_i*z_i + c_r;
                                z_i = 2*z_i*tmp + c_i;
                                i++;
                            }
                            if (i < iteration_max)
                                tab[x][y]=couleur[i%150];
                            else
                                tab[x][y]=0;
                        }
                    }
            }
            tempsActuel = SDL_GetTicks();
            if (tempsActuel - tempsPrecedent > 30)
            {
                tempsPrecedent = tempsActuel;
                for (int i = 0; i < 600; i++)
                {
                    for (int j = 0; j < 480;j++)
                    {
                        definirPixel(ecran,i,j,tab[i][j]);
                    }
                }
            }
            else
            {
            SDL_Delay(30 - (tempsActuel - tempsPrecedent));
            }
            SDL_Flip(ecran);
        }
        SDL_Quit();
        return EXIT_SUCCESS;
    }
    Voila,c'est tout.
    N'hésitez pas à demander des explication.

  2. #2
    Membre régulier Avatar de fifafou
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Janvier 2016
    Messages
    173
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 22
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Janvier 2016
    Messages : 173
    Points : 92
    Points
    92
    Par défaut
    Comme cela n'a pas intéressé grand monde,voici des image obtenus avec chacun de mes codes
    premièrement avec le code Mandelbrot:
    Nom : Mandelbrot.png
Affichages : 1315
Taille : 44,0 Ko
    ensuite avec le code pour les Julias:
    Nom : Julias.png
Affichages : 1147
Taille : 42,5 Ko
    après mon code pour faire plein de fractales differentes
    ex koch:base=0,60,-60,0:
    Nom : Koch.png
Affichages : 1270
Taille : 8,8 Ko
    ex fractale du C:base=0,90:
    Nom : frac_C.png
Affichages : 1246
Taille : 14,7 Ko
    et une autre fractale inventé:base=0,90,0,0,-90,180,-90,0,0,90,0:
    Nom : vague.png
Affichages : 1230
Taille : 11,6 Ko

  3. #3
    Expert éminent
    Avatar de tyrtamos
    Homme Profil pro
    Retraité
    Inscrit en
    Décembre 2007
    Messages
    4 461
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Var (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Retraité

    Informations forums :
    Inscription : Décembre 2007
    Messages : 4 461
    Points : 9 248
    Points
    9 248
    Billets dans le blog
    6
    Par défaut
    Bonjour,

    C'est très intéressant!

    C'est seulement dommage que ce soit encore sous Python 2. Mais malgré la conversion py2 => py3, j'aimerais avoir plus d'infos avec exemples sur les réponses à faire aux questions posées:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    iteration maxi
    point
    zoom
    Merci d'avance!
    Un expert est une personne qui a fait toutes les erreurs qui peuvent être faites, dans un domaine étroit... (Niels Bohr)
    Mes recettes python: http://www.jpvweb.com

  4. #4
    Membre régulier Avatar de fifafou
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Janvier 2016
    Messages
    173
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 22
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Janvier 2016
    Messages : 173
    Points : 92
    Points
    92
    Par défaut
    pour la courbe du dragon,je me suis amusé à la colorer de différentes façons:
    Nom : ddddddd.png
Affichages : 1264
Taille : 22,0 Ko
    Nom : dragonimg.png
Affichages : 1100
Taille : 25,4 Ko
    Nom : dragonsimg2.png
Affichages : 1198
Taille : 29,9 Ko
    Images attachées Images attachées  

  5. #5
    Membre régulier Avatar de fifafou
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Janvier 2016
    Messages
    173
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 22
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Janvier 2016
    Messages : 173
    Points : 92
    Points
    92
    Par défaut
    Citation Envoyé par tyrtamos Voir le message
    Bonjour,

    C'est très intéressant!

    C'est seulement dommage que ce soit encore sous Python 2. Mais malgré la conversion py2 => py3, j'aimerais avoir plus d'infos avec exemples sur les réponses à faire aux questions posées:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    iteration maxi
    point
    zoom
    Merci d'avance!
    Salut,
    itération maxi,est,comme son nom l'indique,le nombre d'iteration de z2+c
    point:
    sur le code mandelbrot,point est un tuple des coordonées du point où l'image est centré
    ex:-0.6,0 pour l'image que j'ai posté
    sur le code des julias c'est le point c fixé
    zoom est le niveau de zoom:200 normalement
    Si on l'augmente,sur le code mandelbrot cela va zoomer mais sur les julias,cela va juste agrandir la taille de l'image
    voila,merci d'avoir repondu.

  6. #6
    Membre régulier Avatar de fifafou
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Janvier 2016
    Messages
    173
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 22
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Janvier 2016
    Messages : 173
    Points : 92
    Points
    92
    Par défaut
    Avec mon code en C++,j'ai obtenu de très belles image mais a cause du nombre de couleurs énorme je n'ai pas pu les afficher

  7. #7
    Expert éminent
    Avatar de tyrtamos
    Homme Profil pro
    Retraité
    Inscrit en
    Décembre 2007
    Messages
    4 461
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Var (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Retraité

    Informations forums :
    Inscription : Décembre 2007
    Messages : 4 461
    Points : 9 248
    Points
    9 248
    Billets dans le blog
    6
    Par défaut
    Bonjour,

    Merci, ça marche!

    J'ai fait tourner "mandelbrot" en Python 3. J'ai converti le code avec 2to3 + quelques corrections dans les expressions avec divisions (en Python 2, la division entre 2 entiers donne un entier).
    Un expert est une personne qui a fait toutes les erreurs qui peuvent être faites, dans un domaine étroit... (Niels Bohr)
    Mes recettes python: http://www.jpvweb.com

  8. #8
    Membre régulier Avatar de fifafou
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Janvier 2016
    Messages
    173
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 22
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Janvier 2016
    Messages : 173
    Points : 92
    Points
    92
    Par défaut
    Avec mon code qui fait plein de fractales différentes,j'en ai obtenu une très jolie avec comme base:0,60,-60,180,-60,60,0
    Nom : fractal1.png
Affichages : 1187
Taille : 13,5 Ko

  9. #9
    Membre régulier Avatar de fifafou
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Janvier 2016
    Messages
    173
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 22
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Janvier 2016
    Messages : 173
    Points : 92
    Points
    92
    Par défaut
    J'ai amélioré ce code pour qu'il dimensionne la fenêtre à la taille de la fractale
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    from numpy import *
    import pygame
    from pygame.locals import *
    def pycode(c_1,c_2,c_3):
        return c_3+c_2*256+c_1*256**2
    couleur=zeros((150))
    ri,vi,bi=250,0,0
    for i in range(25):
        couleur[i]=pycode(ri,vi,bi)
        vi+=10
    for i in range(25,50):
        couleur[i]=pycode(ri,vi,bi)
        ri-=10
    for i in range(50,75):
        couleur[i]=pycode(ri,vi,bi)
        bi+=10
    for i in range(75,100):
        couleur[i]=pycode(ri,vi,bi)
        vi-=10
    for i in range(100,125):
        couleur[i]=pycode(ri,vi,bi)
        ri+=10
    for i in range(125,150):
        couleur[i]=pycode(ri,vi,bi)
        bi-=10
    base=list(input("base"))
    for i in range(len(base)):
        base[i]=(base[i]/180.0)*pi
    n=len(base)
    it=input("iteration")
    lt=[]
    liste=base[:]
    for i in range(it):
        for j in range(n):
            for k in range(0,len(liste)):
                lt.append(liste[k]+base[j])
        liste=lt[:]
        lt=[]
    x=0
    y=0
    coor=[[],[]]
    for i in range(len(liste)):
        x+=cos(liste[i])
        y+=sin(liste[i])
        coor[0].append(x)
        coor[1].append(y)
    max_x=max(coor[0])
    min_x=min(coor[0])
    max_y=max(coor[1])
    min_y=min(coor[1])
    if max_x-min_x>1000:
        max_x=1000-min_x
    if max_y-min_y>550:
        max_y=550-min_y
    M=(ones((max_x-min_x+10,max_y-min_y+10))*16777215)
    for i in range(len(coor[0])):
        try:
            M[coor[0][i]-min_x+5][coor[1][i]-min_y+5]=0
        except:
            pass
    hM=len(M)
    lM=len(M[0])
    print hM,lM
    pygame.init()
    fenetre = pygame.display.set_mode((hM,lM))
    pygame.display.set_caption('Fractale')
    pygame.surfarray.blit_array(fenetre,M)
    #pygame.image.save(fenetre,"fractal1.png")
    continuer = True
    while continuer:
        pygame.time.Clock().tick(1)
        pygame.display.flip()
        for event in pygame.event.get():
    		if event.type == QUIT:
    			continuer = False
    pygame.quit()

  10. #10
    Nouveau Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mars 2017
    Messages
    1
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Algérie

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Enseignement

    Informations forums :
    Inscription : Mars 2017
    Messages : 1
    Points : 1
    Points
    1
    Par défaut Aide
    Bonsoir est ce que si possible de me donner le code de courbe du dragon en C (OpenGL)
    Mercii

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [Fractales 4/5] Courbe et flocon de Koch - Koch curve and snowflake
    Par Jerome Briot dans le forum Téléchargez
    Réponses: 1
    Dernier message: 14/04/2017, 11h43
  2. [Fractales] Triangle de Sierpinski
    Par forum dans le forum Téléchargez
    Réponses: 0
    Dernier message: 20/05/2012, 13h40
  3. Fractales de mandelbrot 3D
    Par sarosto dans le forum Composants VCL
    Réponses: 2
    Dernier message: 29/11/2007, 16h09
  4. Fractales de Mandelbrot
    Par LAS_184 dans le forum Delphi
    Réponses: 3
    Dernier message: 20/12/2006, 20h26
  5. [Fractale] Triangle de Sierpinski
    Par Florian.L dans le forum Algorithmes et structures de données
    Réponses: 6
    Dernier message: 18/01/2005, 23h20

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo