Thread: Help With Mandelbrot Code (I'm in over my head)

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    2

    Help With Mandelbrot Code (I'm in over my head)

    I have some code that generates pixel values for a mandelbrot image. I haven't added the code to output to the file yet; right now I'm trying to figure out why it loops infinitely. I want it to generate a 512x512 pixel image, but as I mentioned, it just keeps on going. Any help would be appreciated. Forgive me for programming ignorance. Here's what I've got so far:

    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    const int maxiters = 255;
    const int xpixels = 512;
    const int ypixels = 512;
    
    
    int getGray(int c) { return ((c)%8)*32; }
    int getRed(int c)  { return c;          }
    int getGreen(int c){ return 0;          }
    int getBlue(int c) { return 255-((c)%16)*32; }
    
    
    int getMbrotCount(double a0, double b0){
        int count=0;
        double a=a0;
        double b=b0;
        double anext,bnext;
    
    
        while(a*a+b*b<4.0 && count < maxiters ){
            anext = a*a - b*b + a0;
            bnext = 2.0*a*b +b0;
            a=anext;
            b=bnext;
            count++;
        }
    
    
        return count;
    }
    
    
    int main(){
        double x0=-2.0;
        double y0=1.5;
        double x1=1.0;
        double y1=-1.5;
        double x,y;
        int c;
        int i,j;
    
    
        cout << "P3\n" << xpixels << " " << ypixels << "\n255\n";
    
    
        for(i=0;i<ypixels;++i){
            y=y0+(y1-y0)/ypixels*i;
    
    
            for(j=0;j<xpixels;++j){
                x=x0+(x1-x0)/xpixels*j;
    
    
                c=getMbrotCount(x,y);
                if(c==maxiters) cout << "0 0 0 ";
                else{
                    cout << getRed(c) << " ";
                    cout << getGreen(c) << " ";
                    cout << getBlue(c) << " ";
                }
            }
            cout << endl;
        }
    
    
    
    
        return 0;
    
    
    }

  2. #2
    Registered User
    Join Date
    Oct 2012
    Posts
    2
    Okay, I output it to a file and it's not infinite looping anymore.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head with character frequency code
    By steadyhanded in forum C Programming
    Replies: 2
    Last Post: 02-08-2012, 05:06 PM
  2. MandelBrot
    By Chems in forum C Programming
    Replies: 16
    Last Post: 10-26-2010, 02:43 PM
  3. Mandelbrot tutorial?
    By PseudoSane in forum C Programming
    Replies: 7
    Last Post: 11-07-2006, 02:02 PM
  4. I want to draw a mandelbrot set ..
    By mad_muppet in forum C Programming
    Replies: 16
    Last Post: 08-18-2006, 12:43 AM
  5. ugly mandelbrot set :(
    By Horse22 in forum C++ Programming
    Replies: 3
    Last Post: 02-26-2005, 03:55 PM