This site requires JavaScript, please enable it in your browser!
Greenfoot back
ronald
ronald wrote ...

2019/8/26

Mandelbrot

ronald ronald

2019/8/26

#
private final int MAX_ITER = 570;
    private final double ZOOM = 150;
    private BufferedImage I;
    private double zx, zy, cx, cy, temp;
    
    
    public Mandelbrot() 
    {
       I = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
        for (int y = 0; y < getHeight(); y++) 
        {
            for (int x = 0; x < getWidth(); x++) 
            {
                zx = zy = 0;
                cx = (x - 400) / ZOOM;
                cy = (y - 300) / ZOOM;
                int iter = MAX_ITER;
                while (zx * zx + zy * zy < 4 && iter > 0) 
                {
                    temp = zx * zx - zy * zy + cx;
                    zy = 2.0 * zx * zy + cy;
                    zx = temp;
                    iter--;
                }
                I.setRGB(x, y, iter | (iter << 8));
            }
        }
    } 
    
    public void paint(GreenfootImage gfim) 
    {
        
        gfim.drawImage(I, 0, 0, this);
        setImage(gfim);
    }
}
I found a small source code, I have not finished, I am adapting it to greenfoot. Do you have any advice to give me, thank you for your help
danpost danpost

2019/8/26

#
ronald wrote...
<< Code Omitted >> I found a small source code, I have not finished, I am adapting it to greenfoot. Do you have any advice to give me, thank you for your help
Use GreenfootImage instead of BufferedImage, for one thing. You will probably have to write your own setRGB method, though. Not sure if line 33 is even valid. Looks like one too many arguments ("this" shouldn't be there). Not sure what the necessity is in bringing in another image to draw the fractal on (in the paint method); but, so be it.
ronald ronald

2019/8/27

#
import greenfoot.*;

 // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Mandelbrot here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Mandelbrot extends Actor
{
    /**
     * Act - do whatever the Mandelbrot wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    private final int MAX_ITER = 570;
    private final double ZOOM = 150;
    
    private double zx, zy, cx, cy, temp;
    public static final int WIDTH = 900;
    public static final int HEIGHT = 600;
    
    
    public Mandelbrot() 
    {
        GreenfootImage gfim = new GreenfootImage(900,600);
        gfim.drawImage(new GreenfootImage (gfim), 0, 0);
        setImage(gfim); 
        update();
    }
    
    public void update()
    {
       GreenfootImage gfim = new GreenfootImage (WIDTH, HEIGHT);
       gfim.setColor(Color.BLUE);
       gfim.fill();
       
        for (int y = 0; y < HEIGHT; y++) 
        {
            for (int x = 0; x < WIDTH; x++) 
            {
                zx = zy = 0;
                cx = (x - 400) / ZOOM;
                cy = (y - 300) / ZOOM;
                int iter = MAX_ITER;
                while (zx * zx + zy * zy < 4 && iter > 0) 
                {
                    temp = zx * zx - zy * zy + cx;
                    zy = 2.0 * zx * zy + cy;
                    zx = temp;
                    iter--;
                }
                  
                gfim.setColor(new Color(x, y, iter << 12));
                
            }
       
        } 
   
    }
}

I improved it a bit but the line 56 gives me worries, I look good but I do not know how to do Thank you for your help
danpost danpost

2019/8/27

#
"iter << 12" does not make sense. That would be like multiplying a value in the range (0, 570) by 2^12 (two to the power of 12, which equals 4096). Only if iter was equal to zero would the product be in a valid color range (0, 255). Also, you might want to set the new image (gfim) to the actor in the update method.
ronald ronald

2019/8/29

#
            iter--;
                }
                  
                
                if (iter==0)
                {
                 gfim.setColor(new Color(0, 0, 0));
                }                
                else
                {   
               
                gfim.setColor(new Color(255,255,255));
                    
                }
            } 
        }
        setImage(gfim);
    }
}
is it the right code, i'm trying to ask if i should not use getBlue, getRed ... or use color variations with iter, thank you for your help
danpost danpost

2019/8/30

#
It does no good to set a color unless you use it. Setting a color only picks that color for future commands; you still need to use it (using a draw or fill command). However, you are working with individual pixels and probably should be using setColorAt which works independent of the set color.
<< Code Omitted >> is it the right code, i'm trying to ask if i should not use getBlue, getRed ... or use color variations with iter
I would think you would want to vary the color dependent on the value of iter.
ronald ronald

2019/8/30

#
if (iter<2)
                {
                 gfim.setColorAt(x,y,Color.WHITE);
                }                
                else
                {   
                 gfim.setColorAt(x,y,Color.BLACK);   
                }
            } 
        }
        gfim.drawImage(gfim,0,0);
        setImage(gfim);
    }
it works, I will try now with color variations. Do you think I made the code with black and white?
danpost danpost

2019/8/30

#
ronald wrote...
Do you think I made the code with black and white?
Not able to tell as line 11 does nothing to your image (drawing an image onto itself with no offset).
ronald ronald

2019/8/30

#
yes i have even remarked i had put drawImage twice, i deleted one
ronald ronald

2019/8/31

#
I would like to understand the lines of code 40 to 53 according to the mathematics concerning the mandelbrot math formula if possible detailed Thank you for your help
danpost danpost

2019/8/31

#
ronald wrote...
I would like to understand the lines of code 40 to 53 according to the mathematics concerning the mandelbrot math formula if possible detailed
// calculate color for all points in image
for (int y = 0; y < HEIGHT; y++)  // for each row in image
{
    for (int x = 0; x < WIDTH; x++) // for each column in image
    {
        // initialization
        zx = zy = 0; // start at origin of complex plane
        cx = (x - 400) / ZOOM; // apply offset and zoom factor for real part to be adding
        cy = (y - 300) / ZOOM; // apply offset and zoom factor for imaginary part to be adding
        int iter = MAX_ITER; // set count of iterations remaining
        while (zx * zx + zy * zy < 4 && iter > 0) // break out of loop if distance from origin >= 4 or if iterations exhausted
        { // repeatedly apply formula z = z*z+c where z and c are complex numbers (z = zx + i*zy and c = cx + i*cy)
            temp = zx * zx - zy * zy + cx; // new real coefficient
            zy = 2.0 * zx * zy + cy; // new imaginary coefficient
            zx = temp;
            iter--; // one less iteration remaining
        }
        gfim.setColor(new Color(x, y, iter << 12)); // erroneous line of code (should set color of (x, y) in image depending on value of iter)
    }
} 
ronald ronald

2019/9/3

#
thank you
ronald ronald

2019/9/5

#
i have a variant formula of mandelbrot, how to transform it into java f (z, c) = sinh (z) + 1 / c2
danpost danpost

2019/9/5

#
ronald wrote...
i have a variant formula of mandelbrot, how to transform it into java f (z, c) = sinh (z) + 1 / c2
sinh(z), where z = a+i*b, is sinh(a)*cos(b)+i*cosh(a)*sin(b). So, the new real coefficient of z before adding the constant is sinh(a)*cos(b) and the new coefficient of the imaginary part of z before adding the constant is cosh(a)*sin(b).
You need to login to post a reply.