Fractal de mandelbrot

Description

Le fractal de Mandelbrot est la visualisation d'un objet mathématique. Il s'agit en fait d'une matrice de nombres, où la valeur de chaque nombre est représentée par une couleur.
Chaque point de l'image est un nombre calculé par l'ordinateur selon une équation très simple: Zn+1 = Zn2 + C (pour plus d'informations, http://en.wikipedia.org/wiki/Mandelbrot_set).

Il y a deux méthodes pour dessiner le fractal dans le projet. Une fois avec une méthode qui utilise SetPixel (managé mais très lent) et une fois un bloc unsafe qui permet, via les pointeurs, d'avoir de bonnes performances.

Source / Exemple :


/// ------------------------------------------------------------------------
        /// <summary>
        /// Draw the fractal (with unmanaged code).
        /// </summary>
        /// ------------------------------------------------------------------------
        public void DrawMandelBrotFractalUnmanaged()
        {
            int width = this.Width;
            int height = this.Height;
            int offset = width % 4;

            int halfX = 2 * width;
            int halfY = height / 2;

            using (Graphics gfx = this.CreateGraphics())
            {
                this._bmpBuffer = new Bitmap(width, height, gfx);
                BitmapData bmpData = this._bmpBuffer.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

                unsafe
                {
                    byte* newPixel = (byte*)(void*)bmpData.Scan0;
                    width *= 3;

                    for (int y = 0; y < height; ++y)
                    {
                        for (int x = 0; x < width; x += 3)
                        {
                            Complex complex = new Complex((x - halfX) / 450d, (y - halfY) / 150d);
                            Color color = this.GetColor(this.GetStep(complex));
                            newPixel[0] = color.B;
                            newPixel[1] = color.G;
                            newPixel[2] = color.R;
                            newPixel += 3;
                        }
                        newPixel += offset;
                    }
                }
                this._bmpBuffer.UnlockBits(bmpData);
                Graphics.FromImage(this._bmpBuffer).DrawString("Mandelbrot's Fractal", this._font, this._fontBrush, Point.Empty);
                gfx.DrawImage(this._bmpBuffer, Point.Empty);
            }
        }

Codes Sources

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.