aboutsummaryrefslogtreecommitdiff
path: root/Week11 Mandelbrot/src/com/camilstaps/mandelbrot/MandelbrotView.java
blob: 6422e274ef57b5def6a3813bc3d250424f002c35 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
 * The MIT License (MIT)
 * 
 * Copyright (c) 2015 Camil Staps <info@camilstaps.nl>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.camilstaps.mandelbrot;

import fractals.Grid;

/**
 * Showing the Mandelbrot fractal on the screen
 * @author camilstaps
 */
public class MandelbrotView {
    
    private final MandelbrotProvider mandelbrotProvider;
    private final Grid grid;
    
    /**
     * Get info from a MandelbrotProvider; work with a Grid
     * @param mp
     * @param grid 
     */
    public MandelbrotView(MandelbrotProvider mp, Grid grid) {
        mandelbrotProvider = mp;
        this.grid = grid;
    }
    
    /**
     * Get the 'mathematical' x value for a certain x coordinate of the screen
     * @param x_on_screen
     * @return 
     */
    public double getX(int x_on_screen) {
        double centerX = mandelbrotProvider.getCenterX();
        double scale = mandelbrotProvider.getScale();
        int grid_w = grid.getWidth();
        double  min_x = centerX - grid_w / scale / 2,
                max_x = centerX + grid_w / scale / 2;
        
        return min_x + ((double) x_on_screen) * (max_x - min_x) / grid_w;
    }
    
    /**
     * Get the 'mathematical' y value for a certain y coordinate of the screen
     * @param y_on_screen
     * @return 
     */
    public double getY(int y_on_screen) {
        double centerY = mandelbrotProvider.getCenterY();
        double scale = mandelbrotProvider.getScale();
        int grid_h = grid.getHeight();
        double  min_y = centerY - grid_h / scale / 2,
                max_y = centerY + grid_h / scale / 2;
        
        return min_y + (((float) y_on_screen) * (max_y - min_y) / grid_h);
    }
    
    /**
     * (Re)draw the fractal
     */
    public void redraw() {
        int repetitions = mandelbrotProvider.getRepetitions();
        
        int grid_w = grid.getWidth(), grid_h = grid.getHeight();
        
        for (int i = 0; i < grid_w; i++) {
            for (int j = 0; j < grid_h; j++) {
                // Convert mandel number to some number in [0,pi] to let the colourise below work nicely
                double mandel = ((double) Mandelbrot.mandelNumber(getX(i), getY(j), repetitions) * Math.PI) / repetitions;
                // Different mandel numbers have different colours
                int[] colour = {
                    (int) (30 + 220 * Math.sin(mandel)), 
                    (int) (30 + 220 * Math.sin(mandel + 2 * Math.PI / 3)), 
                    (int) (30 + 220 * Math.sin(mandel + 4 * Math.PI / 3))};
                // This is a grayscale version:
                //int[] color = {(int) (255 * mandel), (int) (255 * mandel), (int) (255 * mandel)};
                grid.setPixel(i, j, colour);
            }               
        }
    }

    /**
     * Get user input
     */
    public interface MandelbrotProvider {
        /**
         * Get center X value
         * @return 
         */
        public double getCenterX();
        
        /**
         * Get center Y value
         * @return 
         */
        public double getCenterY();
        
        /**
         * Get scale factor
         * @return 
         */
        public double getScale();
        
        /**
         * Get max amount of iterations for the mandel function
         * @return 
         */
        public int getRepetitions();
    }
    
}