diff options
author | Camil Staps | 2015-04-30 17:18:11 +0200 |
---|---|---|
committer | Camil Staps | 2015-04-30 17:18:11 +0200 |
commit | 3b4d9eab5128a79bc7f464cbf105798d7af44962 (patch) | |
tree | 506ac0167d83da1961c8e5d91adbee6d055eeebf /Week11 Mandelbrot/src/com | |
parent | cleanup (diff) |
Cleaned up; javadoc
Diffstat (limited to 'Week11 Mandelbrot/src/com')
3 files changed, 196 insertions, 43 deletions
diff --git a/Week11 Mandelbrot/src/com/camilstaps/mandelbrot/Mandelbrot.java b/Week11 Mandelbrot/src/com/camilstaps/mandelbrot/Mandelbrot.java new file mode 100644 index 0000000..7adb93f --- /dev/null +++ b/Week11 Mandelbrot/src/com/camilstaps/mandelbrot/Mandelbrot.java @@ -0,0 +1,40 @@ +package com.camilstaps.mandelbrot;
+
+import fractals.MainWindow;
+
+/**
+ * Solutions to week 11
+ * @author Camil Staps
+ */
+public class Mandelbrot {
+
+ /**
+ * MainWindow does the hard work
+ * @param args
+ */
+ public static void main(String args[]) {
+ MainWindow fractal_win = new MainWindow ();
+ }
+
+ /**
+ * Calculate the mandel number up to a certain amount of iterations of the function
+ * @param x
+ * @param y
+ * @param repetitions
+ * @return
+ */
+ public static int mandelNumber(double x, double y, int repetitions) {
+ double x_n = x, y_n = y;
+ int n = 0;
+
+ while (x_n * x_n + y_n * y_n <= 4 && n <= repetitions) {
+ double new_x_n = x_n * x_n - y_n * y_n + x;
+ y_n = 2 * x_n * y_n + y;
+ x_n = new_x_n;
+ n++;
+ }
+
+ return n;
+ }
+
+}
\ No newline at end of file diff --git a/Week11 Mandelbrot/src/com/camilstaps/mandelbrot/MandelbrotView.java b/Week11 Mandelbrot/src/com/camilstaps/mandelbrot/MandelbrotView.java index 4cb489a..fe6a69e 100644 --- a/Week11 Mandelbrot/src/com/camilstaps/mandelbrot/MandelbrotView.java +++ b/Week11 Mandelbrot/src/com/camilstaps/mandelbrot/MandelbrotView.java @@ -4,10 +4,9 @@ package com.camilstaps.mandelbrot; import fractals.Grid; -import fractals.Mandelbrot; /** - * + * Showing the Mandelbrot fractal on the screen * @author camilstaps */ public class MandelbrotView { @@ -15,11 +14,21 @@ 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(); @@ -30,6 +39,11 @@ public class MandelbrotView { 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(); @@ -40,6 +54,9 @@ public class MandelbrotView { return min_y + (((float) y_on_screen) * (max_y - min_y) / grid_h); } + /** + * (Re)draw the fractal + */ public void redraw() { int repetitions = mandelbrotProvider.getRepetitions(); @@ -47,21 +64,46 @@ public class MandelbrotView { 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; - int[] color = { + // 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, color); + 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(); } diff --git a/Week11 Mandelbrot/src/com/camilstaps/mandelbrot/ZoomFrame.java b/Week11 Mandelbrot/src/com/camilstaps/mandelbrot/ZoomFrame.java index 1dfa2a7..3c15d5b 100644 --- a/Week11 Mandelbrot/src/com/camilstaps/mandelbrot/ZoomFrame.java +++ b/Week11 Mandelbrot/src/com/camilstaps/mandelbrot/ZoomFrame.java @@ -12,8 +12,8 @@ import java.awt.event.MouseMotionListener; import javax.swing.JFrame; /** - * - * @author camilstaps + * A frame in which zooming is possible + * @author Camil Staps, s4498062 */ public class ZoomFrame extends JFrame implements MouseListener, MouseMotionListener { @@ -40,65 +40,103 @@ public class ZoomFrame extends JFrame implements MouseListener, MouseMotionListe } @Override - public void mouseClicked(MouseEvent me) { - } + public void mouseClicked(MouseEvent me) {} + /** + * Save the coordinates + * @param me + */ @Override public void mousePressed(MouseEvent me) { old_x = start_x = me.getX(); old_y = start_y = me.getY(); } + /** + * On shift-click, zoom out. + * On click, zoom in. + * On drag, zoom the box. + * @param me + */ @Override public void mouseReleased(MouseEvent me) { if (me.getX() == start_x && me.getY() == start_y) { - if (mandelbrotView == null || mandelbrotTextFields == null) - return; - - mandelbrotTextFields.setCenterX(mandelbrotView.getX(me.getX())); - mandelbrotTextFields.setCenterY(mandelbrotView.getY(me.getY())); - if ((me.getModifiers() & InputEvent.SHIFT_MASK) != 0) { - mandelbrotTextFields.setScale(mandelbrotTextFields.getScale() / 2); + zoomOut(me); } else { - mandelbrotTextFields.setScale(mandelbrotTextFields.getScale() * 2); + zoomIn(me); } } else { - double center_x = (mandelbrotView.getX(me.getX()) - - mandelbrotView.getX(start_x)) / 2 - + mandelbrotView.getX(start_x); - double center_y = (mandelbrotView.getY(me.getY()) - - mandelbrotView.getY(start_y)) / 2 - + mandelbrotView.getY(start_y); - - mandelbrotTextFields.setCenterX(center_x); - mandelbrotTextFields.setCenterY(center_y); - - double scale = mandelbrotTextFields.getScale() - / (me.getX() - start_x) - * getWidth(); - - System.err.println(mandelbrotTextFields.getScale()); - System.err.println(me.getX() - start_x); - System.err.println(getWidth()); - - mandelbrotTextFields.setScale(scale); + zoomBox(me); } mandelbrotView.redraw(); dragging = false; } + + /** + * Zoom in with factor 2 + * @param me + */ + private void zoomIn(MouseEvent me) { + if (mandelbrotView == null || mandelbrotTextFields == null) + return; - @Override - public void mouseEntered(MouseEvent me) { + mandelbrotTextFields.setCenterX(mandelbrotView.getX(me.getX())); + mandelbrotTextFields.setCenterY(mandelbrotView.getY(me.getY())); + + mandelbrotTextFields.setScale(mandelbrotTextFields.getScale() * 2); } + + /** + * Zoom out with factor 2 + * @param me + */ + private void zoomOut(MouseEvent me) { + if (mandelbrotView == null || mandelbrotTextFields == null) + return; - @Override - public void mouseExited(MouseEvent me) { + mandelbrotTextFields.setCenterX(mandelbrotView.getX(me.getX())); + mandelbrotTextFields.setCenterY(mandelbrotView.getY(me.getY())); + + mandelbrotTextFields.setScale(mandelbrotTextFields.getScale() / 2); } + + /** + * Zoom to the selected box. + * Intentionally chose to let the user only select squares, otherwise it's too easy to mess up the scale + * @param me + */ + private void zoomBox(MouseEvent me) { + double center_x = (mandelbrotView.getX(me.getX()) + - mandelbrotView.getX(start_x)) / 2 + + mandelbrotView.getX(start_x); + double center_y = (mandelbrotView.getY(me.getY()) + - mandelbrotView.getY(start_y)) / 2 + + mandelbrotView.getY(start_y); + + mandelbrotTextFields.setCenterX(center_x); + mandelbrotTextFields.setCenterY(center_y); + + double scale = mandelbrotTextFields.getScale() + / (me.getX() - start_x) + * getWidth(); + + mandelbrotTextFields.setScale(scale); + } + + @Override + public void mouseEntered(MouseEvent me) {} @Override + public void mouseExited(MouseEvent me) {} + + /** + * Erase the old box, draw the new box + * @param me + */ + @Override public void mouseDragged(MouseEvent me) { Graphics g = getSafeGraphics(); if (g != null) { @@ -111,6 +149,10 @@ public class ZoomFrame extends JFrame implements MouseListener, MouseMotionListe } } + /** + * Semi-singleton construction for graphics + * @return + */ private Graphics getSafeGraphics() { if (graphics == null) { graphics = getGraphics(); @@ -119,6 +161,9 @@ public class ZoomFrame extends JFrame implements MouseListener, MouseMotionListe return graphics; } + /** + * Erase the old zoombox if it exists + */ private void eraseZoombox() { if (start_x < 0 || start_y < 0 || old_x < 0 || old_y < 0) return; @@ -128,15 +173,41 @@ public class ZoomFrame extends JFrame implements MouseListener, MouseMotionListe } @Override - public void mouseMoved(MouseEvent me) { - //System.err.println(me.toString()); - } + public void mouseMoved(MouseEvent me) {} + /** + * Interface to get and set user input + */ public interface MandelbrotTextFields { + + /** + * Set the center X + * @param x + */ public void setCenterX(double x); + + /** + * Set the center Y + * @param y + */ public void setCenterY(double y); + + /** + * Get the scale factor + * @return + */ public double getScale(); + + /** + * Set the scale factor + * @param scale + */ public void setScale(double scale); + + /** + * Set the max amount of iterations for the mandel function + * @param repetitions + */ public void setRepetitions(int repetitions); } |