I want to know how to zoom in on a mandelbrot drawing with the mouse by right and left clicking
Thank you for your help
private double zoomFactor = 100.0;
if (Greenfoot.mouseClicked(null))
{
double dz += Greenfoot.getMouseInfo().getButton() == 1 ? 0.1 : -0.1;
if (zoomFactor + dz >= 0.5) zoomFactor += dz;
updateImage();
}public void act()
{
if (Greenfoot.mouseClicked(null))
{
double dz += Greenfoot.getMouseInfo().getButton() == 1 ? 0.1 : -0.1;
if (zoomFactor + dz > 0.5) zoomFactor += dz;
updateImage();
}
//GreenfootImage gfim = new GreenfootImage(900,600);
drawMandelbrot();
}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 Mandelbrot5 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 = 30;
private final double ZOOM = 200;
private double zx, zy, cx, cy, temp;
public static final int WIDTH = 900;
public static final int HEIGHT = 600;
private double zoomFactor = 100.0;
public Mandelbrot5()
{
}
public void act()
{
if (Greenfoot.mouseClicked(null))
{
double dz = Greenfoot.getMouseInfo().getButton() == 1 ? 0.1 : -0.1;
if (zoomFactor + dz > 0.5) zoomFactor += dz;
updateImage();
}
}
public void updateImage()
{
GreenfootImage gfim = new GreenfootImage (WIDTH, HEIGHT);
for (int x = 0; x < WIDTH; x++)
{
for (int y = 0; y < HEIGHT; y++)
{
zx = zy = 0;
cx = (x - 400) / ZOOM;
cy = (y - 300) / ZOOM;
int iter = 0;
while (zx * zx + zy * zy < 4 && iter < MAX_ITER)
{
temp = zx;
zx = zx * zx - zy * zy + cx;
zy = 2.0 * zy * temp + cy;
iter++;
}
if (iter > MAX_ITER) gfim.setColorAt(x,y,Color.WHITE);
else if (iter > 9*MAX_ITER/10) gfim.setColorAt(x,y,Color.DARK_GRAY);
else if (iter > 8*MAX_ITER/10) gfim.setColorAt(x,y,Color.GRAY);
else if (iter > 7*MAX_ITER/10) gfim.setColorAt(x,y,Color.MAGENTA);
else if (iter > 6*MAX_ITER/10) gfim.setColorAt(x,y,Color.CYAN);
else if (iter > 5*MAX_ITER/10) gfim.setColorAt(x,y,Color.BLUE);
else if (iter > 4*MAX_ITER/10) gfim.setColorAt(x,y,Color.GREEN);
else if (iter > 3*MAX_ITER/10) gfim.setColorAt(x,y,Color.YELLOW);
else if (iter > 2*MAX_ITER/10) gfim.setColorAt(x,y,Color.ORANGE);
else if (iter > 1*MAX_ITER/10) gfim.setColorAt(x,y,Color.RED);
else gfim.setColorAt(x,y,Color.BLACK);
}
}
gfim.drawImage(gfim,0,0);
setImage(gfim);
}
}