java mandelbrot设置移动错误

时间:2013-03-28 10:14:08

标签: java mandelbrot

所以我一直在尝试编写java中的mandelbrot集,我知道代码不是很优化,但我刚刚开始这样做。

我的想法是,当我点击一个像素时,它会将该像素放在中心(代表某个复数)并计算它周围的集合。这在开始时工作但如果我放大它将开始表现奇怪。我猜它是我的midX,midY或显示功能很奇怪,但我已经看了很长时间而无法理解它,会感激一些帮助。

class set {
    DotWindow w;
    int[][] arrayColor;
    int max = 100;
    Grayscale gray;
    double zoom = 1.1;
    double midX = -0.5;
    double midY = 0;

    public static void main(String[] args) {
        new set().run();
    }

    void run() {
        setup();
        runLoop();

    }

    void runLoop() {
        int x;
        int y;
        while (true) {
            GameEvent event = w.getNextEvent();
            switch (event.getKind()) {
            case GameEvent.KEY_PRESSED:
                int key = event.getKey();
                if (key == 43) {
                    zoom = zoom * 1.1;
                } else if (key == 45) {
                    zoom = zoom / 1.1;
                }

                display();
                break;
            case GameEvent.MOUSE_CLICKED:
                midX = midX - (1 - event.getX() / 250.0);
                midY = midY - (1 - event.getY() / 250.0);
                System.out.println(midX);
                display();
                break;
            }

        }
    }

    void setup() {
        w = new DotWindow(500, 500, 1);
        w.checkMouse(true, false, false, false, false);
        w.checkKeys(true, false, false);
        arrayColor = new int[500][500];
        zoom = zoom / 1.1;
        display();

    }

    int calculate(double re, double im) {
        double Zre = 0;
        double Zim = 0;
        double Zim2 = 0;
        double Zre2 = 0;
        int iterations = 0;

        for (int k = 0; k < max; k++) {
            if (Zre2 + Zim2 > 4.0) {
                return k;
            }
            Zim2 = Zim * Zim;
            Zre2 = Zre * Zre;
            Zim = 2.0 * Zre * Zim + im;
            Zre = Zre2 - Zim2 + re;
            iterations = k;
        }
        return iterations;

    }

    void display() {
        for (double y = 0; y < 500; y++) {
            for (double x = 0; x < 500; x++) {
                double value = calculate((midX - (1 - x / 250) / zoom),
                        (midY - (1 - y / 250) / zoom));
                if (value == 99) {
                    w.setDot((int) x, (int) y, Color.BLACK);
                } else {
                    w.setDot((int) x, (int) y, gray = new Grayscale(
                            255 - (int) value * 2));
                }

            }

        }
    }

}

1 个答案:

答案 0 :(得分:0)

        case GameEvent.MOUSE_CLICKED:
            midX = midX - (1 - event.getX() / 250.0)/zoom;
            midY = midY - (1 - event.getY() / 250.0)/zoom;
            System.out.println(midX);
            display();

你已经放大了,比如10倍,但你改变了中心坐标而不是缩放值。