Hey everyone, I have been working on some c++ recently.
I wanted to make some Mandelbrot images in .ppm format so far I have the outlines of the code; file loading, rewriting etc, I just don't know (even after using google to read up on mandelbrots) how the equation works to make this image.
I'm using a 512x512 .ppm file and can't figure it out, if anybody will point me in the right direction that would be swell!

Code:

#include <iostream>
#include <fstream>
using namespace std;

int r = 0;
int g = 0;
int b = 0;

int rows = 512;
int columns =512;
int main () {
ofstream fout;
fout.open("mandelbrot.ppm");
fout << "P3 ";
fout << rows << " ";
fout << columns << " ";
fout << "255 " << endl;
for (int y = 0; y<rows; y++) {
    for (int x = 0; x<columns; x++) {
        fout << r << " " << g << " " << b << " " << endl;
    }
}
fout.close();
return 0;
}
https://github.com/ComputerNerd/Mandelbrot-Casio-Prizm-Explorer/blob/master/src/main.c
If this is supposed to run on the PC only you may want to consider rewritting it to use float math and see if it is faster. If this will go on a system without a FPU then keep the fixed point math. Also it says casio prizm but the code to draw the mandelbrot is portable. It is designed to generate a rgb565 image but that is very easy to change. If you are going to be using rgb888 data I will assume this is on the PC I would remove the LUT array and just do the division at the end.
Alright, so I got a decent mandlebrot generator worked out:

Code:

#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
const int ROWS=512;
const int COLS=512;
const int MAXITERS=1000;
const float c1r=-1.5;
const float c1i=1;
const float c2r=0.5;
const float c2i=-1;
int pallete = 1;
struct Complex {
   float real;
   float imag;
};
int r;
int g;
int b;
struct point{
   float x;
   float y;
};
void PrintComplex(Complex c){
   cout<<c.real<<"+"<<c.imag<<"i";
   return;
}
Complex AddComplex(Complex c1, Complex c2){
   Complex sum;
   sum.real = c1.real + c2.real;
   sum.imag = c1.imag + c2.imag;
   return sum;
}
Complex MultiplyComplex(Complex c1, Complex c2){
   Complex sum;
   sum.real = c1.real*c2.real - c1.imag*c2.imag;
   sum.imag = c1.real*c2.imag + c1.imag*c2.real;
   return sum;
}
float getReal(int j, Complex c1, Complex c2){
   float incrementVal;
   float real;
   incrementVal=(c2.real-c1.real)/COLS;
   real=c1.real+(incrementVal*j);
   return real;
}
float getImag(int i, Complex c1, Complex c2){
   float incrementVal;
   float imag;
   incrementVal=(c2.imag-c1.imag)/ROWS;
   imag=c1.imag+(incrementVal*i);
   return imag;
}
Complex getComplex(int i, int j, Complex c1, Complex c2){
   Complex c;
   c.real=getReal(j,c1,c2);
   c.imag=getImag(i,c1,c2);
   return c;
}
float getx(int j, point p1, point p2){
   float incrementVal;
   float x;
   incrementVal=(p2.x-p1.x)/COLS;
   x=p1.x+(incrementVal*j);
   return x;
}
float gety(int i, point p1, point p2){
   float incrementVal;
   float y;
   incrementVal=(p2.y-p1.y)/ROWS;
   y=p1.y+(incrementVal*i);
   return y;
}
point getPoint(int i, int j, point p1, point p2){
   point p;
   p.x=getx(j,p1,p2);
   p.y=gety(i,p1,p2);
   return p;
}
bool isInsideUnitCircle(point p){
   if((p.x*p.x+p.y*p.y)<=1)return true;
   return false;
}
int getdistSQ(point p){
   float dist;
   dist=p.x*p.x+p.y*p.y;
   return dist;
}
int setRGB(int A, int B, int C) {
    r = A;
    g = B;
    b = C;
return 0;
}
int setPallette(int P, int I) {
if (P==1) {
    //standard
    switch(I) {
case 1:
    //main body
    setRGB(66,30,15);
    break;
case 2:
    //farthest out
    setRGB(25,7,26);
    break;
case 3:
    setRGB(9,1,47);
    break;
case 4:
    setRGB(4,4,73);
    break;
case 5:
    setRGB(0,7,100);
    break;
case 6:
    setRGB(12,44,138);
    break;
case 7:
    setRGB(24,82,177);
    break;
case 8:
    setRGB(57,125,209);
    break;
case 9:
    setRGB(134,181,229);
    break;
case 10:
    setRGB(211,236,248);
    break;
case 11:
    setRGB(241,233,191);
    break;
case 12:
    setRGB(248,201,95);
    break;
case 13:
    setRGB(255,170,0);
    break;
case 14:
    setRGB(204,128,0);
    break;
case 15:
    setRGB(153,87,0);
    break;
case 16:
    setRGB(106,52,3);
    break;
default:
    //main arms
    setRGB(0,0,0);
}
} else  if (P==2) {
    //red
    setRGB(I*10,0,0);
} else if (P==3) {
    //green
    setRGB(0,I*10,0);
} else if (P==4) {
    //blue
    setRGB(0,0,I*10);
}
return 0;
}
int getColor(unsigned int i){
i%=MAXITERS/16;
i++;
setPallette(pallete, i);
return 0;
}
int getMbrotIndex(Complex c){
   int i=0;
   Complex z;
   z=c;
   while(z.real*z.real+z.imag*z.imag<4 && i<MAXITERS){
      z=MultiplyComplex(z,z);
      z=AddComplex(z,c);
      i++;
   }
   return i;
}

int main(){
    /*
    ifstream fin;
    fin.open("mandelinput.txt");
    if (fin.is_open()) {
        while ( getline (fin,line) ) {
            if (line == 1) {

            } else if (line ==2 ) {

            } else if (line ==3 )
        }
        myfile.close();
    } else {
        cout << "unable to open file";
    }
*/
   ofstream fout;
   fout.open("mandeloutput.ppm");
   Complex c1,c2;
   c1.real=c1r;
   c1.imag=c1i;
   c2.real=c2r;
   c2.imag=c2i;
   Complex c;
   int index;
   //top matter
   fout<<"P3 ";
   fout<<COLS<< " ";
   fout<<ROWS<< " ";
   fout<<"255 ";
   fout<<endl;
   cout << "Color Pallete?" << endl;
   cout << "1: Normal\n2: Red\n3: Green\n4: Blue\n";
    cin >> pallete;
    if (pallete<1 || pallete>4) pallete=1;
   //find each pixel color
   for(int i=0;i<ROWS;++i){
      for(int j=0;j<COLS;++j){
         // find the point in space for this pixel
         c = getComplex(i,j,c1,c2);
         // get an index for this point
         index = getMbrotIndex(c);
         //get a color for this index
         getColor(index);
         //set color
            fout << r << " ";
                fout << g << " ";
                fout << b << " ";
                fout << endl;
      }
   }
   fout.close();
   return 0;
}

Anybody can use this code; there are 4 different coloring options, using the Red, Green, or Blue options just sets the shading to several values of that color. While normal uses a yellow, white, black and blue shading.
This generator uses the Escape Time Algorithm (ETA), but I might update and use the Continuous Color Rendering Algorithm.
Running this program will make a madlebrot.ppm file. I'm working on txt input for several factors such as image bounds [-2.5,1] which scales it to the 512 pixels, so it functions as a zoom.
Here is one that I generator with iterations set at 5000 and the color scheme as normal.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement