aboutsummaryrefslogtreecommitdiff
path: root/Week11 Mandelbrot/src/com/camilstaps/mandelbrot/MandelbrotView.java
diff options
context:
space:
mode:
authorCamil Staps2015-04-29 22:37:13 +0200
committerCamil Staps2015-04-29 22:37:13 +0200
commit57cc1163708260f3e4f5b56cc1de2ad2ee625018 (patch)
treec865e7c5ad8db5bf4e3846395e55046a9bc2263f /Week11 Mandelbrot/src/com/camilstaps/mandelbrot/MandelbrotView.java
parentBugfix drawing zoom rectangle (diff)
cleanup
Diffstat (limited to 'Week11 Mandelbrot/src/com/camilstaps/mandelbrot/MandelbrotView.java')
-rw-r--r--Week11 Mandelbrot/src/com/camilstaps/mandelbrot/MandelbrotView.java68
1 files changed, 68 insertions, 0 deletions
diff --git a/Week11 Mandelbrot/src/com/camilstaps/mandelbrot/MandelbrotView.java b/Week11 Mandelbrot/src/com/camilstaps/mandelbrot/MandelbrotView.java
new file mode 100644
index 0000000..4cb489a
--- /dev/null
+++ b/Week11 Mandelbrot/src/com/camilstaps/mandelbrot/MandelbrotView.java
@@ -0,0 +1,68 @@
+/*
+ * Copyright (c) 2015 Camil Staps
+ */
+package com.camilstaps.mandelbrot;
+
+import fractals.Grid;
+import fractals.Mandelbrot;
+
+/**
+ *
+ * @author camilstaps
+ */
+public class MandelbrotView {
+
+ private final MandelbrotProvider mandelbrotProvider;
+ private final Grid grid;
+
+ public MandelbrotView(MandelbrotProvider mp, Grid grid) {
+ mandelbrotProvider = mp;
+ this.grid = grid;
+ }
+
+ 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;
+ }
+
+ 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);
+ }
+
+ 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++) {
+ double mandel = ((double) Mandelbrot.mandelNumber(getX(i), getY(j), repetitions) * Math.PI) / repetitions;
+ int[] color = {
+ (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))};
+ //int[] color = {(int) (255 * mandel), (int) (255 * mandel), (int) (255 * mandel)};
+ grid.setPixel(i, j, color);
+ }
+ }
+ }
+
+ public interface MandelbrotProvider {
+ public double getCenterX();
+ public double getCenterY();
+ public double getScale();
+ public int getRepetitions();
+ }
+
+}