Advertisement
Guest User

Untitled

a guest
May 27th, 2015
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. /* This is a very simple program to create the mandelbrot set */
  2. /* compile is g++ mand.cpp -o mand */
  3.  
  4. /* sample paths to try are */
  5. /* -2 -1 1 -1 10000 */
  6. /* -1.3 -1.2 0.1 -0.1 256 */
  7.  
  8. #include <iostream>
  9. #include <fstream>
  10. #include <math.h>
  11. using namespace std;
  12.  
  13. const int width = 1024;
  14. const int height = 768;
  15.  
  16. int computeData(int xstart, int xend, int ystart, int yend, int iter, char (&pic)[height][width][3]);
  17. bool outputFile(char (&pic)[height][width][3]);
  18.  
  19. int main()
  20. {
  21. double xstart, xend, ystart, yend;
  22. int iter;
  23.  
  24. char pic[height][width][3]; //actual pic
  25.  
  26. // get data
  27. cout << "Enter xstart, xend, ystart, yend, iterations: " << endl;
  28. cin >> xstart >> xend >> ystart >> yend >> iter;
  29.  
  30. computeData(xstart, xend, ystart, yend, iter, pic);
  31. // create an output stream for the file
  32.  
  33.  
  34. if (outputFile(pic) == false)
  35. {
  36. cout << "Error Opening File" << endl;
  37. return 1;
  38. }
  39.  
  40.  
  41. return 0;
  42. }
  43.  
  44. int computeData(int xstart, int xend, int ystart, int yend, int iter, char (&pic)[height][width][3])
  45. {
  46. double x, y, xstep, ystep, z, zi, newz, newzi, colour;
  47. int i, j, k, inset, fd;
  48. long col;
  49.  
  50. // compute the step relative to the resolution of the image
  51. xstep = (xend-xstart)/width;
  52. ystep = (yend-ystart)/height;
  53.  
  54. // do the calculation
  55. x = xstart;
  56. y = ystart;
  57. for (i=0; i<height; i++)
  58. {
  59. for (j=0; j<width; j++)
  60. {
  61. z = 0;
  62. zi = 0;
  63. inset = 1;
  64. for (k=0; k<iter; k++)
  65. {
  66. newz = (z*z)-(zi*zi) + x;
  67. newzi = 2*z*zi + y;
  68. z = newz;
  69. zi = newzi;
  70. if(((z*z)+(zi*zi)) > 4)
  71. {
  72. inset = 0;
  73. colour = k;
  74. k = iter;
  75. }
  76. }
  77. if (inset)
  78. {
  79. pic[i][j][0] = 0;
  80. pic[i][j][1] = 0;
  81. pic[i][j][2] = 0;
  82. }
  83. else
  84. {
  85. pic[i][j][0] = (char) (colour / iter * 255);
  86. pic[i][j][1] = (char) (colour / iter * 255 / 2);
  87. pic[i][j][2] = (char) (colour / iter * 255 / 2);
  88. }
  89. x += xstep;
  90. }
  91. y += ystep;
  92. x = xstart;
  93. }
  94.  
  95. }
  96.  
  97. bool outputFile(char (&pic)[height][width][3]) // stuff
  98. {
  99. ofstream ofs;
  100. ofs.open("image.tga", ios::out);
  101.  
  102. if(ofs.good())
  103. {
  104. char buffer[100];
  105.  
  106. // prepare the buffer for the TGA format.
  107.  
  108. buffer[0] = 0;
  109. buffer[1] = 0;
  110. buffer[2] = 2;
  111. buffer[8] = 0; buffer[9] = 0;
  112. buffer[10] = 0; buffer[11] = 0;
  113. buffer[12] = (width & 0x00FF); buffer[13] = (width & 0xFF00) >> 8;
  114. buffer[14] = (height & 0x00FF); buffer[15] = (height & 0xFF00) >> 8;
  115. buffer[16] = 24;
  116. buffer[17] = 0;
  117.  
  118. // write the entire buffer out i.e. tga header
  119.  
  120. ofs.write((char*)buffer, 18);
  121.  
  122. // now write the pic out which is stored in pic
  123.  
  124. ofs.write((char*)pic, width*height*3);
  125. ofs.close();
  126.  
  127. return true;
  128. }
  129. else
  130. {
  131. return false;
  132. }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement