Dessinateur de fractales

Contenu du snippet

Dessine la courbe de Mandelbrot. Code optimise, a compiler avec mingw32 ( ou DevCpp ). Les calculs sont faits en assembleurs. Cependant, la partie affichage ggnerait à être améliorée pour ne pas réafficher le fractale à chaque dessinage de la fenetre.

Source / Exemple :


#include <windows.h>
#include <stdio.h>
#include <math.h>

#define ID_BUTTON_DESSINER 100
#define ID_EDIT_CRMIN 101
#define ID_EDIT_CRMAX 102
#define ID_EDIT_CIMIN 103
#define ID_EDIT_CIMAX 104
#define ID_EDIT_N 105
#define ID_STATIC_CR 106
#define ID_STATIC_CI 107

/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);
/* Make the class name into a global variable */
char szClassName[ ] = "WindowsApp";
COLORREF fractal[600][800];
double crmax,crmin, cimax,cimin, crpas,cipas;
HWND hwndmain, hbouton, hedit_crmin, hedit_crmax, hedit_cimin, hedit_cimax, hedit_n, hstatic_cr, hstatic_ci;
MSG messages;            /* Here messages to the application are saved */
int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil){
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof(WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor(NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL; /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use light-gray as the background of the window */
    wincl.hbrBackground =(HBRUSH) GetStockObject(LTGRAY_BRUSH);

    /* Register the window class, if fail quit the program */
    if(!RegisterClassEx(&wincl)) return 0;

    /* The class is registered, let's create the program*/
    hwndmain = CreateWindowEx(
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "Mandelbrot",         /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           1015,                 /* The programs width */
           627,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    int i,j;
    for(i=0;i<600;i++)
      for(j=0;j<800;j++)
         fractal[i][j]=RGB(0,0,0);
    /* Make the window visible on the screen */

    hbouton=CreateWindowEx (0,"BUTTON","dessiner",WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON,805,260,100,25,
                           hwndmain,(HMENU)ID_BUTTON_DESSINER,hThisInstance,NULL);

    hedit_crmin=CreateWindowEx (0,"EDIT","-2.1",WS_CHILD|WS_VISIBLE|WS_BORDER ,805,30,200,20,
                               hwndmain,(HMENU)ID_EDIT_CRMIN,hThisInstance,NULL);
    hedit_crmax=CreateWindowEx (0,"EDIT","0.9",WS_CHILD|WS_VISIBLE|WS_BORDER,805,80,200,20,
                               hwndmain,(HMENU)ID_EDIT_CRMAX,hThisInstance,NULL);
    hedit_cimin=CreateWindowEx (0,"EDIT","-1.2",WS_CHILD|WS_VISIBLE|WS_BORDER,805,130,200,20,
                               hwndmain,(HMENU)ID_EDIT_CIMIN,hThisInstance,NULL);
    hedit_cimax=CreateWindowEx (0,"EDIT","1.2",WS_CHILD|WS_VISIBLE|WS_BORDER,805,180,200,20,
                               hwndmain,(HMENU)ID_EDIT_CIMAX,hThisInstance,NULL);
    hedit_n=CreateWindowEx (0,"EDIT","255",WS_CHILD|WS_VISIBLE|WS_BORDER,805,230,75,20,
                               hwndmain,(HMENU)ID_EDIT_N,hThisInstance,NULL);

    CreateWindowEx (0,"STATIC","Cr min =",WS_CHILD|WS_VISIBLE,805,5,60,20,
                    hwndmain,(HMENU)0,hThisInstance,NULL);
    CreateWindowEx (0,"STATIC","Cr max =",WS_CHILD|WS_VISIBLE,805,55,60,20,
                    hwndmain,(HMENU)0,hThisInstance,NULL);
    CreateWindowEx (0,"STATIC","Ci min =",WS_CHILD|WS_VISIBLE,805,105,60,20,
                    hwndmain,(HMENU)0,hThisInstance,NULL);
    CreateWindowEx (0,"STATIC","Ci max =",WS_CHILD|WS_VISIBLE,805,155,60,20,
                    hwndmain,(HMENU)0,hThisInstance,NULL);
    CreateWindowEx (0,"STATIC","N =",WS_CHILD|WS_VISIBLE,805,205,30,20,
                    hwndmain,(HMENU)0,hThisInstance,NULL);

    CreateWindowEx (0,"STATIC","cr =",WS_CHILD|WS_VISIBLE,805,500,30,20,
                    hwndmain,(HMENU)0,hThisInstance,NULL);
    CreateWindowEx (0,"STATIC","ci =",WS_CHILD|WS_VISIBLE,805,550,30,20,
                    hwndmain,(HMENU)0,hThisInstance,NULL);

    hstatic_cr= CreateWindowEx (0,"STATIC","",WS_CHILD|WS_VISIBLE,805,525,200,20,
                               hwndmain,(HMENU)ID_STATIC_CR,hThisInstance,NULL);
    hstatic_ci= CreateWindowEx (0,"STATIC","",WS_CHILD|WS_VISIBLE,805,575,200,20,
                               hwndmain,(HMENU)ID_STATIC_CI,hThisInstance,NULL);

    ShowWindow(hwndmain, nFunsterStil);

    /* Run the message loop. It will run until GetMessage( ) returns 0 */
    while(GetMessage(&messages, NULL, 0, 0))
    {
           /* Translate virtual-key messages into character messages */
           TranslateMessage(&messages);
           /* Send message to WindowProcedure */
           DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage( ) gave */
    return messages.wParam;
}

COLORREF Palette(int e){
         int k=((int)sqrt(e)*48)%768;
         COLORREF r;
         if(k<256) r=RGB(k,0,255-k);
         if(k<512 && k>=256) r=RGB(511-k,k-256,0);
         if(k>=512) r=RGB(0,767-k,k-512);
         if(r==0){
           char tampontxt[100];
           sprintf(tampontxt,"k=%d m=%d",k,e);
           MessageBox(hwndmain,tampontxt,"",MB_OK);
         }
         return r;
}

/* This function is called by the Windows function DispatchMessage( ) */
LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HDC hdc;
    PAINTSTRUCT ps;
    int x,y,i,j;
    switch (message)                  /* handle the messages */
    {
           case WM_DESTROY:
                PostQuitMessage(0);        /* send a WM_QUIT to the message queue */
           break;
           case WM_COMMAND:

                if ((LOWORD(wParam) == ID_BUTTON_DESSINER) && (HIWORD(wParam) == BN_CLICKED)){
                   long double cr,ci;
                   char tampontxt[10000];
                   int Nmax;

                   GetDlgItemText(hwndmain,ID_EDIT_CRMIN,tampontxt,100);
                   sscanf(tampontxt,"%lf",&crmin);

                   GetDlgItemText(hwndmain,ID_EDIT_CRMAX,tampontxt,100);
                   sscanf(tampontxt,"%lf",&crmax);

                   GetDlgItemText(hwndmain,ID_EDIT_CIMIN,tampontxt,100);
                   sscanf(tampontxt,"%lf",&cimin);

                   GetDlgItemText(hwndmain,ID_EDIT_CIMAX,tampontxt,100);
                   sscanf(tampontxt,"%lf",&cimax);

                   Nmax=GetDlgItemInt(hwndmain,ID_EDIT_N,NULL,FALSE);

                   crpas=(crmax-crmin)/800;
                   cipas=(cimax-cimin)/600;

                   hdc=GetDC (hwnd);
                   for (x=0;x<800;x++){
                       cr=x*crpas+crmin;

                       for (y=0;y<600;y++){
                           ci=y*cipas+cimin;
                           char diverge=1;
                           asm volatile("fld1\n"
                                "fadd %%st(0)\n"
                                "fadd %%st(0)\n"//pour charger 4
                                "fldt %1\n"
                                "fldt %0\n"
                                "fldz\n"
                                "fldz"
                                :
                                : "m"(cr), "m"(ci)
                                );
                           int k;
                           for (k=0;k<Nmax && diverge;k++){
                             asm volatile( "movb $0,%0\n"
                                  //calcul de zi
                                  "fld %%st(0)\n"
                                  "fmul %%st(2)\n"
                                  "fadd %%st(0)\n"    //multiplie par 2
                                  "fadd %%st(4)\n"
                                  //calcul de zr
                                  "fld %%st(1)\n"
                                  "fmul %%st(0)\n"  //élève au carré
                                  "fld %%st(3)\n"
                                  "fmul %%st(0)\n"
                                  "fsubrp\n"
                                  "fadd %%st(4)\n"
                                  //on remet les données a leur place
                                  "fstp %%st(2)\n"
                                  "fstp %%st(2)\n"
                                  //comparaison de la valeur absolue pour voir si on sort
                                  "fld %%st(0)\n"
                                  "fmul %%st(0)\n"  //élève au carré
                                  "fld %%st(2)\n"
                                  "fmul %%st(0)\n"
                                  "faddp\n"
                                  "fcomip %%st(5),%%st(0)\n"
                                  "sbbb $0,%0"                                 
                                  :"=g" (diverge)
                                  );
                                  
                           }
                           // on libère le FPU
                           asm volatile( "fstp %%st(0)\n"
                                "fstp %%st(0)\n"
                                "fstp %%st(0)\n"
                                "fstp %%st(0)\n"
                                "fstp %%st(0)\n":::"st");
                           
                           if (diverge){
                              fractal[y][x]=RGB(0,0,0);
                              SetPixel(hdc,x,y,RGB(0,0,0));
                           }   else {
                              fractal[y][x]=Palette(k);
                              SetPixel(hdc,x,y,Palette(k));
                           }
                       }
                       
                   }
                   ReleaseDC(hwnd,hdc);
                } else return DefWindowProc(hwnd, message, wParam, lParam);
           break;
           case WM_PAINT:
                hdc=BeginPaint(hwndmain,&ps);
                for (i=0;i<800;i++)
                    for (j=0;j<600;j++)
                        SetPixel(hdc,i,j,fractal[j][i]);
                EndPaint(hwndmain,&ps);

           break;
           case WM_MOUSEMOVE:
                x=LOWORD(lParam);
                y=HIWORD(lParam);
                if (x>0 && x<800 && y>0 && y<600){
                   char tampon[1000];
                   sprintf(tampon,"%.20lf",(double)(x*crpas+crmin) );
                   SetWindowText(hstatic_cr,tampon);
                   sprintf(tampon,"%.20lf",(double)(y*cipas+cimin) );
                   SetWindowText(hstatic_ci,tampon);
                }
           break;
           case WM_LBUTTONDOWN:
                x=LOWORD(lParam);
                y=HIWORD(lParam);
                if (x>0 && x<800 && y>0 && y<600){
                   char tampon[1000];
                   sprintf(tampon,"%.20lf",(double)(x*crpas+crmin) );
                   SetWindowText(hedit_crmin,tampon);
                   sprintf(tampon,"%.20lf",(double)(y*cipas+cimin) );
                   SetWindowText(hedit_cimin,tampon);
                }
           break;
           case WM_LBUTTONUP:
                x=LOWORD(lParam);
                y=HIWORD(lParam);
                if (x>0 && x<800 && y>0 && y<600){
                   char tampon[1000];
                   sprintf(tampon,"%.20lf",(double)(x*crpas+crmin) );
                   SetWindowText(hedit_crmax,tampon);
                   sprintf(tampon,"%.20lf",(double)(y*cipas+cimin) );
                   SetWindowText(hedit_cimax,tampon);
                }
           break;
           default:                   /* for messages that we don't deal with */
                return DefWindowProc(hwnd, message, wParam, lParam);

    }
    return 0;
}

Conclusion :


Pour zoomer, il faut appuyer le bouton de la souris en haut a gauche du rectangle que l'on veut voir et relacher le bouton de la souris en bas a droite.
N, c'est le nombre d'itérations, il faut l'augmenter si on zoome pas mal.

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.

Du même auteur (jourgun)