Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Mandelbrot - mandelbrot.c

mandelbrot.c

Caricato da:
Scarica il programma completo

  1. /***************************************************************************
  2.  *   Copyright (C) 2008 by Lorenzo La Porta   *
  3.  *   lorelapo@gmail.com   *
  4.  *                                                                         *
  5.  *   This program is free software; you can redistribute it and/or modify  *
  6.  *   it under the terms of the GNU General Public License as published by  *
  7.  *   the Free Software Foundation; either version 2 of the License, or     *
  8.  *   (at your option) any later version.                                   *
  9.  *                                                                         *
  10.  *   This program is distributed in the hope that it will be useful,       *
  11.  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
  12.  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
  13.  *   GNU General Public License for more details.                          *
  14.  *                                                                         *
  15.  *   You should have received a copy of the GNU General Public License     *
  16.  *   along with this program; if not, write to the                         *
  17.  *   Free Software Foundation, Inc.,                                       *
  18.  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
  19.  ***************************************************************************/
  20.  
  21.  
  22. #ifdef HAVE_CONFIG_H
  23. #include <config.h>
  24. #endif
  25.  
  26. #include <stdio.h>
  27. #include <SDL.h>
  28. #include "SDL_gfxPrimitives.h"
  29. #include <math.h>
  30.  
  31. #define WIDTH 3000
  32. #define HEIGHT 2000
  33. #define BPP 4
  34. #define DEPTH 32
  35. #define ITERATION_DEPTH 1000
  36. #define ITER_COLOR 25
  37.  
  38. typedef float precision;
  39. typedef struct complex
  40. {
  41.         precision r;
  42.         precision i;
  43. }complex;
  44.  
  45. complex square(complex c)
  46. {
  47.         complex res;
  48.         res.r=c.r*c.r-c.i*c.i;
  49.         res.i=2.0*c.r*c.i;
  50.         return res;
  51. }
  52.  
  53. precision module(complex c)
  54. {
  55.         return sqrt(c.r*c.r+c.i*c.i);
  56. }
  57.  
  58. int iterate(complex c)
  59. {
  60.         complex z=c;
  61.         int i;
  62.         for(i=0;i<ITERATION_DEPTH;i++)
  63.         {
  64.                 if(module(z)>2.0)
  65.                         return i;
  66.                 z=square(z);
  67.                 z.r+=c.r;
  68.                 z.i+=c.i;
  69.         }
  70.         return i;
  71. }
  72.  
  73. void mandelbrot(SDL_Surface *surf)
  74. {
  75.         complex c;
  76.         register int x,y,i;
  77.         for(x=0;x<WIDTH;x++)
  78.                 for(y=0;y<HEIGHT;y++)
  79.                 {
  80.                         /*printf("\rCalcolato : %.2f%%",(float)(x*HEIGHT+y)/(WIDTH*HEIGHT)*100.0);
  81.                         fflush(stdout);*/
  82.                         c.r=(precision)(x)*4.0/WIDTH-2.5;
  83.                         c.i=1.5-(precision)(y)*3.0/HEIGHT;
  84.                         i=iterate(c);
  85.                         i=255-(i*255/ITERATION_DEPTH);
  86.                         pixelRGBA(surf,x,y,i*ITER_COLOR,i*(ITER_COLOR-1),i*(ITER_COLOR-2),255);
  87.                        
  88.                 }
  89. }
  90.  
  91. int main(int argc, char* argv[])
  92. {
  93.         SDL_Surface *screen;
  94.         SDL_Event event;
  95.        
  96.         int keypress = 0;
  97.        
  98.        
  99.         if (SDL_Init(SDL_INIT_VIDEO) < 0 ) return 1;
  100.        
  101.         if (!(screen = SDL_CreateRGBSurface(SDL_HWACCEL|SDL_HWSURFACE,WIDTH, HEIGHT, DEPTH,0,0,0,0)))
  102.         {
  103.                 SDL_Quit();
  104.                 return 1;
  105.         }
  106.        
  107.         mandelbrot(screen);
  108.        
  109.         SDL_Flip(screen);
  110.         sleep(1);
  111.        
  112.         SDL_SaveBMP(screen,"Mandelbrot.bmp");
  113.         SDL_FreeSurface(screen);
  114.         SDL_Quit();
  115.  
  116.         return 0;
  117. }