Задача: Рисование множества Мандельброта
Исходник: Рисование множества Мандельброта, язык: C++ [code #139, hits: 10258]
автор: - [добавлен: 17.05.2006]
  1. #define DEPTH 100 // iterations depth
  2. #include <graphics.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <conio.h>
  6. #include <math.h>
  7. // this function initializes graphics mode
  8. // it will work only if you're using Borland C++ compiler & BGI drivers
  9. // if you're using another compiler you should overwrite body of this function
  10. void init_gr(void)
  11. {
  12. /* request autodetection */
  13. int gdriver = DETECT, gmode, errorcode;
  14. /* initialize graphics mode */
  15. initgraph(&gdriver, &gmode, "");
  16. /* read result of initialization */
  17. errorcode = graphresult();
  18. if (errorcode != grOk) /* an error occurred */
  19. {
  20. printf("Graphics error: %s\n", grapherrormsg(errorcode));
  21. printf("Press any key to halt:");
  22. getch();
  23. exit(1); /* return with error code */
  24. }
  25. }
  26. // this function shuts graphics mode down
  27. // it will work only if you're using Borland C++ compiler & BGI drivers
  28. // if you're using another compiler you should overwrite body of this function
  29. void end_gr(void)
  30. {
  31. closegraph();
  32. }
  33. // this function puts pixel on the screen in (x,y) position using color 'color'
  34. // it will work only if you're using Borland C++ compiler & BGI drivers
  35. // if you're using another compiler you should overwrite body of this function
  36. void PutPixel(int x, int y, int color)
  37. {
  38. putpixel(x,y,color);
  39. }
  40. void Mandelbrot(void)
  41. {
  42. float zi, zr, ci, cr, tmp;
  43. // assuming graphics mode 640x480 16 colors
  44. for(int i=-320;i<320;i++) // for all pixels on the X axe
  45. {
  46. ci=((float)i)/320.0; // setting Im c = i/320
  47. for(int j=-380;j<160;j++) // for all pixels on the Y axe
  48. {
  49. cr=((float)j)/240.0; // setting Re c = j/240
  50. zi=zr=0.0; // fixing z=0 we'll change c - it's Mandelbrot set
  51. for(int k=0;k<DEPTH;k++)
  52. {
  53. // z=z*z+c
  54. //(zr+i*zi)*(zr+i*zi)=(zr*zr-zi*zi)+i*(2*zr*zi)
  55. // zi=zr*zr-zi*zi+cr
  56. //
  57. tmp=zr*zr-zi*zi;
  58. zi=2*zr*zi+ci;
  59. zr=tmp+cr;
  60. if (zr*zr+zi*zi>1.0E16) break; // break if |z| is very big
  61. }
  62. if (k<DEPTH)
  63. PutPixel(i+320,j+380,k%3+1); // z was very big => it is external point
  64. else PutPixel(i+320,j+380,11); // internal point
  65. }
  66. if(kbhit()) break;
  67. }
  68. }
  69. int main(void)
  70. {
  71. // initializing graphics mode
  72. init_gr();
  73. /* example */
  74. Mandelbrot();
  75. /* clean up */
  76. getch();
  77. end_gr();
  78. return 0;
  79. }
//////////////////////////////////////////////////////////////////////////////
//
// Mandelbrot set drawing
// (c) Johna Smith, 1996
//
// Method description:
// 1) Assume z(0)=0
// 2) Organize two loops by X and Y axes
// 3) Determine constant C=X+iY
// 4) Using iterative formula z(i)=z(i-1)*z(i-1)+c calculate Z(DEPTH),
// where DEPTH is max depth of iterations
// 5) If Z(DEPTH) is near Z(0) then point (X,Y) belongs to Mandelbrot set
//
// ! This program is NOT optimized for best performance !
// To do so don't use putpixel function - change bytes in video memory directly.
//
//////////////////////////////////////////////////////////////////////////////

+добавить реализацию