Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* This is a very simple program to create the mandelbrot set */
- /* compile is g++ mand.cpp -o mand */
- /* sample paths to try are */
- /* -2 -1 1 -1 10000 */
- /* -1.3 -1.2 0.1 -0.1 256 */
- #include <iostream>
- #include <fstream>
- #include <math.h>
- using namespace std;
- const int width = 1024;
- const int height = 768;
- int computeData(int xstart, int xend, int ystart, int yend, int iter, char (&pic)[height][width][3]);
- bool outputFile(char (&pic)[height][width][3]);
- int main()
- {
- double xstart, xend, ystart, yend;
- int iter;
- char pic[height][width][3]; //actual pic
- // get data
- cout << "Enter xstart, xend, ystart, yend, iterations: " << endl;
- cin >> xstart >> xend >> ystart >> yend >> iter;
- computeData(xstart, xend, ystart, yend, iter, pic);
- // create an output stream for the file
- if (outputFile(pic) == false)
- {
- cout << "Error Opening File" << endl;
- return 1;
- }
- return 0;
- }
- int computeData(int xstart, int xend, int ystart, int yend, int iter, char (&pic)[height][width][3])
- {
- double x, y, xstep, ystep, z, zi, newz, newzi, colour;
- int i, j, k, inset, fd;
- long col;
- // compute the step relative to the resolution of the image
- xstep = (xend-xstart)/width;
- ystep = (yend-ystart)/height;
- // do the calculation
- x = xstart;
- y = ystart;
- for (i=0; i<height; i++)
- {
- for (j=0; j<width; j++)
- {
- z = 0;
- zi = 0;
- inset = 1;
- for (k=0; k<iter; k++)
- {
- newz = (z*z)-(zi*zi) + x;
- newzi = 2*z*zi + y;
- z = newz;
- zi = newzi;
- if(((z*z)+(zi*zi)) > 4)
- {
- inset = 0;
- colour = k;
- k = iter;
- }
- }
- if (inset)
- {
- pic[i][j][0] = 0;
- pic[i][j][1] = 0;
- pic[i][j][2] = 0;
- }
- else
- {
- pic[i][j][0] = (char) (colour / iter * 255);
- pic[i][j][1] = (char) (colour / iter * 255 / 2);
- pic[i][j][2] = (char) (colour / iter * 255 / 2);
- }
- x += xstep;
- }
- y += ystep;
- x = xstart;
- }
- }
- bool outputFile(char (&pic)[height][width][3]) // stuff
- {
- ofstream ofs;
- ofs.open("image.tga", ios::out);
- if(ofs.good())
- {
- char buffer[100];
- // prepare the buffer for the TGA format.
- buffer[0] = 0;
- buffer[1] = 0;
- buffer[2] = 2;
- buffer[8] = 0; buffer[9] = 0;
- buffer[10] = 0; buffer[11] = 0;
- buffer[12] = (width & 0x00FF); buffer[13] = (width & 0xFF00) >> 8;
- buffer[14] = (height & 0x00FF); buffer[15] = (height & 0xFF00) >> 8;
- buffer[16] = 24;
- buffer[17] = 0;
- // write the entire buffer out i.e. tga header
- ofs.write((char*)buffer, 18);
- // now write the pic out which is stored in pic
- ofs.write((char*)pic, width*height*3);
- ofs.close();
- return true;
- }
- else
- {
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement