0

so I've been trying to program the mandelbrot set in java, I know the code isn't very optimized but i've just started out doing this.

The idea is that when i click a pixel it will put that pixel in the center (that represents a certain complex number) and calculate the set around it. This works at the beginning but if i zoom in it will start to behave weird. I'm guessing it's either my midX, midY or the display function that's weird but i've been looking at it for a long time and can't figure it out, would appreciate some help.

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));
                }

            }

        }
    }

}
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
poffsan
  • 3
  • 1
  • Do you mind showing what kind of misbehavior you receive, as a screenshot preferably? – Vesper Mar 28 '13 at 10:19
  • 2
    CTRL+SHIFT+F in Eclipse helps :) Adding JAVA tag on SO helps *a lot* ;) – Andrea Ligios Mar 28 '13 at 10:20
  • Thanks :) Well a screenshot doesn't really explain it since it looks right, but when you move it it doesn't move correctly. If I zoom in a few times it wont put the pixel i clicked on in the center anymore. Seems it only works as long as the magnification isn't too large. – poffsan Mar 28 '13 at 10:35

1 Answers1

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();

You've already zoomed in, say to 10x, but you change central coordinates not regarding the zoom value.

Vesper
  • 18,599
  • 6
  • 39
  • 61