Задача: Рисование Фрактала (листьев папоротника)
Исходник: Рисование Фрактала (листьев папоротника), язык: C++ [code #138, hits: 8811]
автор: - [добавлен: 17.05.2006]
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <dos.h>
  4. #include <graphics.h>
  5. #include <conio.h>
  6. // this function initializes graphics mode
  7. // it will work only if you're using Borland C++ compiler & BGI drivers
  8. // if you're using another compiler you should overwrite body of this function
  9. void init_gr(void)
  10. {
  11. /* request autodetection */
  12. int gdriver = DETECT, gmode, errorcode;
  13. /* initialize graphics mode */
  14. initgraph(&gdriver, &gmode, "");
  15. /* read result of initialization */
  16. errorcode = graphresult();
  17. if (errorcode != grOk) /* an error occurred */
  18. {
  19. printf("Graphics error: %s\n", grapherrormsg(errorcode));
  20. printf("Press any key to halt:");
  21. getch();
  22. exit(1); /* return with error code */
  23. }
  24. }
  25. // this function shuts graphics mode down
  26. // it will work only if you're using Borland C++ compiler & BGI drivers
  27. // if you're using another compiler you should overwrite body of this function
  28. void end_gr(void)
  29. {
  30. closegraph();
  31. }
  32. // this function puts pixel on the screen in (x,y) position using color 'color'
  33. // it will work only if you're using Borland C++ compiler & BGI drivers
  34. // if you're using another compiler you should overwrite body of this function
  35. void PutPixel(int x, int y, int color)
  36. {
  37. putpixel(x,y,color);
  38. }
  39. #define N 200000L // number of points
  40. float x=0, y=0; // iteration variables (are global)
  41. // a b c d e f probability
  42. float coeff[4][6] = {{ 0.00, 0.00, 0.00,0.16,0.00,0.00,}, // 01%
  43. { 0.85, 0.04,-0.04,0.85,0.00,1.60,}, // 85%
  44. { 0.05,-0.26, 0.23,0.22,0.00,1.60,}, // 07%
  45. {-0.15, 0.30, 0.26,0.24,0.00,0.44,},}; // 07%
  46. int colors[5]={10,11,2,3,2};
  47. void iterate(char i,int c)
  48. {
  49. float x1,y1;
  50. x1=x*coeff[i][0]+y*coeff[i][1]+coeff[i][4];
  51. y1=x*coeff[i][2]+y*coeff[i][3]+coeff[i][5];
  52. x=x1;
  53. y=y1;
  54. putpixel ((int)(y*64),240-(int)(x*48),c);
  55. }
  56. int main (void)
  57. {
  58. int leaf,color;
  59. // initializing graphics mode
  60. init_gr();
  61. // drawing leafs
  62. for (long int i=0;i<N;i++)
  63. {
  64. color=colors[random(5)]; // random color
  65. leaf=random(1000); // selecting leaf to draw
  66. if (leaf<=10) iterate(0,color);
  67. else if (leaf<=860) iterate(1,color);
  68. else if (leaf<=930) iterate(2,color);
  69. else iterate(3,color);
  70. }
  71. /* clean up */
  72. getch();
  73. end_gr();
  74. return 0;
  75. }
//////////////////////////////////////////////////////////////////////////////
//
// Leafs drawing
// (c) Johna Smith, 1996
//
// Method description:
// We take a closed area, which will be main leaf. We want to generate
// four fractal leafs from this one. To make it we need to scale, move and
// rotate main leaf. These transformations described by the following
// equations:
// x'=ax+by+c, y'=dx+ey+f
// So we need 6 coefficients for each leaf (24 coefficients at all).
// These coefficients are calculated with special mathematical method
// (here are already calculated coefficients)
// To draw whole picture we need an iterational process:
// 1) Take any point within main leaf borders
// 2) Call function iterate for one of 4 leafs (its nuber we select
// randomly but than the larger is leaf's area the higher is probability
// that we call 'iterate' for this leaf
// 3) Then we take transformed point as (x,y) and repeat iterations
//
// ! This program is NOT optimized for best performance !
// To do so don't use putpixel function - change bytes in video memory directly.
//
//////////////////////////////////////////////////////////////////////////////

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