Mandelbrot设置缩放和平移

时间:2014-05-09 00:14:06

标签: java fractals mandelbrot

我用Java编写了一个Mandelbrot Set Fractal,并且包含了对分形进行平移和放大一定量的功能。唯一的事情是,当我平移图像并尝试放大时,看起来它似乎试图放大中心并平移一点。平移和缩放不是真正平移或缩放更多实际上是重新计算分形,看起来好像是在平移或缩放。

这是我的代码。

package core;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;

public class Main extends JFrame implements KeyListener
{
    // Constant screen dimensions
    private final int SCREEN_WIDTH = 800;
    private final int SCREEN_HEIGHT = 600;

    // Fractal image
    private BufferedImage fractal;

    // Number of max iterations
    private int maxIter = 500;
    // Zoom factor
    private double zoom = 150;
    // Coordinates
    private double xPos = SCREEN_WIDTH / 2;
    private double yPos = SCREEN_HEIGHT / 2;

    // Algorithm variables
    private double zx, zy, cX, cY, tmp;

    public static void main(String[] args)
    {
        // Create the frame and make it visible
        new Main().setVisible(true);
    }

    public Main()
    {
        super("Mandelbrot Set");
        setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        addKeyListener(this);
        setResizable(false);

        // Compute the fractal to start
        ComputeFractal();
    }

    public void ComputeFractal()
    {
        fractal = new BufferedImage(SCREEN_WIDTH, SCREEN_HEIGHT, BufferedImage.TYPE_INT_RGB);

        for (int y = 0; y < fractal.getHeight(); y++)
        {
            for (int x = 0; x < fractal.getWidth(); x++)
            {
                zx = zy = 0;
                cX = (x - xPos) / zoom;
                cY = (y - yPos) / zoom;

                int iter;

                for (iter = 0; iter < maxIter && zx * zx + zy * zy < 4; iter++)
                {
                    tmp = zx * zx - zy * zy + cX;
                    zy = 2.0 * zx * zy + cY;
                    zx = tmp;
                }

                // If the point is in the set
                if (iter == maxIter)
                {
                    Color color = new Color(0, 0, 0);
                    fractal.setRGB(x, y, color.getRGB());
                }
                // If the point is not in the set
                else
                {   
                    double r = iter | (iter << 2);
                    while (r > 255) { r-=255; }
                    double g = iter | (iter << 4);
                    while (g > 255) { g-=255; }
                    double b = iter | (iter << 8);
                    while (b > 255) { b-=255; }

                    Color color = new Color((int) r, (int) g, (int) b);
                    fractal.setRGB(x, y, color.getRGB());
                }
            }
        }

        repaint();
    }

    public void paint(Graphics g)
    {
        // Draw the fractal
        g.drawImage(fractal, 0, 0, this);
    }

    public void keyReleased(KeyEvent e)
    {
        if (e.getKeyCode() == KeyEvent.VK_SPACE)
        {
            zoom += 100;
            ComputeFractal();
        }
        else if (e.getKeyCode() == KeyEvent.VK_LEFT)
        {
            xPos += 50;
            ComputeFractal();
        }
        else if (e.getKeyCode() == KeyEvent.VK_RIGHT)
        {
            xPos -= 50;
            ComputeFractal();
        }
        else if (e.getKeyCode() == KeyEvent.VK_UP)
        {
            yPos += 50;
            ComputeFractal();
        }
        else if (e.getKeyCode() == KeyEvent.VK_DOWN)
        {
            yPos -= 50;
            ComputeFractal();
        }
    }

    public void keyTyped(KeyEvent e)
    {

    }

    public void keyPressed(KeyEvent e)
    {

    }
}

任何人都可以告诉我,我可以做些什么来使缩放更精确到用户平移相机的位置?

提前谢谢。

编辑:想知道这个程序将呈现什么的人的图片。

enter image description here

1 个答案:

答案 0 :(得分:0)

基本上最新的是,在mandelbrot的中心是xPos 0和yPos 0.你的缩放是xPos和yPos(cX =(x - xPos)/ zoom;)。例如,如果你的xPos是-500并且你增加你的变焦,它将是-500 /变焦,所以它将是 - 4,下次你缩放它将是 - 3,依此类推,直到它达到0。

你想要做的是在x和y上从(-2:2)在mandelbrot上设置一个固定位置(x,y)。因此,该位置将成为您在mandelbrot上渲染图像的中心。缩放将告诉mandelbrod集上每个像素离中心有多远。例如,如果中心像素在mandelbrot上为0,0则基于缩放,中心像素+ 1将为0.1,0或0.00005,0。箭头键将移动固定位置。

cX = xPos/SCREEN_WIDTH + (x - (fractal.getWidth() >> 1)) / zoom;
cY = yPos/SCREEN_HEIGHT + (y - (fractal.getHeight() >> 1)) / zoom;

如果你在代码中粘贴它会起作用,但我建议使用xPos,yPos值大多在范围(-2:2),它会更有意义。另外我建议你改变一下你的方式。为此你需要改变......

private double zoom = 0.01;
private double xPos = 0;
private double yPos = 0;
...

cX = xPos + (x - (fractal.getWidth() >> 1)) * zoom;
cY = yPos + (y - (fractal.getHeight() >> 1)) * zoom;
... 

public void keyReleased(KeyEvent e)
{
    if (e.getKeyCode() == KeyEvent.VK_SPACE)
    {
        zoom *= 0.9;
        ComputeFractal();
    }
    else if (e.getKeyCode() == KeyEvent.VK_LEFT)
    {
        xPos -= 10 * zoom;
        ComputeFractal();
    }
    else if (e.getKeyCode() == KeyEvent.VK_RIGHT)
    {
        xPos += 10 * zoom;
        ComputeFractal();
    }
    else if (e.getKeyCode() == KeyEvent.VK_UP)
    {
        yPos -= 10 * zoom;
        ComputeFractal();
    }
    else if (e.getKeyCode() == KeyEvent.VK_DOWN)
    {
        yPos += 10 * zoom;
        ComputeFractal();
    }
}

我必须做mandelbrot作为我在大学的作业,所以如果你有兴趣,可以在这里查看here