0

I'm building an application where you can zoom in on the mandelbrot by clicking at a point in the mandelbrot. When clicked, the position of the click will be the center of the canvas and the scale will be multiplied by 2 (so you zoom in). But it doesn't work. It does not zoom in at the point where you click.

        canvas = new Canvas(GRID_WIDTH, GRID_HEIGHT);
        canvas.setOnMouseClicked(e -> {
            if(!e.isShiftDown()) {
                scale = scale * 2;
                xCoordinate = e.getX()/GRID_WIDTH;
                yCoordinate = e.getY()/GRID_HEIGHT;
                areaFiller.fill2( canvas , xCoordinate, yCoordinate, scale); // creates the mandelbrot with coordinates from the center
            }
            else {
               if(e.isShiftDown()) {
                  scale = scale / 2;
                  xCoordinate = e.getX()/GRID_WIDTH;
                  yCoordinate = e.getY()/GRID_HEIGHT;
                  areaFiller.fill2( canvas , xCoordinate, yCoordinate, scale);
               }
            }   
        });
fabian
  • 80,457
  • 12
  • 86
  • 114
jrip
  • 140
  • 1
  • 10
  • *"If I call this method something goes wrong"* is not enough information to find the issue, if there is no sufficient description (or better code) for the method at question. However most likely the x and y coordinates are wrong, since the calculation of those values is independent of current scale and "viewport". – fabian May 13 '16 at 14:27
  • The current `scale` should figure in the calculation that gets the coordinate in the complex plane where mouse was clicked, and an offset from the current central coordinate too. – Weather Vane May 13 '16 at 16:57
  • See related question: [JavaFX correct scaling](http://stackoverflow.com/questions/16680295/javafx-correct-scaling) which provides some background information on such tasks and might help you to derive a solution to your problem. – jewelsea May 13 '16 at 19:36
  • ...and you should adjust `scale` *after* you have worked out where the mouse was clicked. For some reason you have worked that out twice - you only need to do it once: after ascertaining the mouse was clicked, and before testing the shift key for zoom direction in/out. – Weather Vane May 16 '16 at 15:53

0 Answers0