001 /* 002 * Copyright (C) 2008-2010 by Holger Arndt 003 * 004 * This file is part of the Universal Java Matrix Package (UJMP). 005 * See the NOTICE file distributed with this work for additional 006 * information regarding copyright ownership and licensing. 007 * 008 * UJMP is free software; you can redistribute it and/or modify 009 * it under the terms of the GNU Lesser General Public License as 010 * published by the Free Software Foundation; either version 2 011 * of the License, or (at your option) any later version. 012 * 013 * UJMP is distributed in the hope that it will be useful, 014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 016 * GNU Lesser General Public License for more details. 017 * 018 * You should have received a copy of the GNU Lesser General Public 019 * License along with UJMP; if not, write to the 020 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, 021 * Boston, MA 02110-1301 USA 022 */ 023 024 package org.ujmp.gui.actions; 025 026 import javax.swing.Action; 027 import javax.swing.JComponent; 028 029 import org.ujmp.core.Matrix; 030 import org.ujmp.core.exceptions.MatrixException; 031 import org.ujmp.core.interfaces.GUIObject; 032 import org.ujmp.gui.MatrixGUIObject; 033 034 public class MandelbrotMatrixAction extends AbstractMatrixAction { 035 private static final long serialVersionUID = 8708344146012762782L; 036 037 public MandelbrotMatrixAction(JComponent c, MatrixGUIObject m, GUIObject v) { 038 super(c, m, v); 039 putValue(Action.NAME, "Mandelbrot Matrix"); 040 putValue(Action.SHORT_DESCRIPTION, 041 "creates a matrix from the mandelbrot set"); 042 } 043 044 public Object call() throws MatrixException { 045 double xoffset = -.5; 046 double yoffset = 0; 047 double size = 2; 048 int cells = 500; 049 int iterations = 20; 050 051 Matrix m = Matrix.factory.zeros(cells, cells); 052 for (int column = 0; column < cells; column++) { 053 for (int row = 0; row < cells; row++) { 054 double x0 = xoffset - size / 2 + size * column / cells; 055 double y0 = yoffset - size / 2 + size * row / cells; 056 Complex z0 = new Complex(x0, y0); 057 double gray = iterations - calc(z0, iterations); 058 m.setAsDouble((gray - (iterations / 2)) / (iterations / 2), 059 cells - 1 - row, column); 060 } 061 } 062 m.showGUI(); 063 return m; 064 } 065 066 public int calc(Complex c, int iterations) { 067 Complex z = c; 068 for (int i = 0; i < iterations; i++) { 069 if (z.abs() > 2.0) { 070 return i; 071 } 072 z = z.times(z).plus(c); 073 } 074 return iterations; 075 } 076 077 } 078 079 class Complex { 080 double real = 0; 081 082 double img = 0; 083 084 public Complex(double real, double img) { 085 this.real = real; 086 this.img = img; 087 } 088 089 public double abs() { 090 return Math.sqrt(real * real + img * img); 091 } 092 093 public Complex times(Complex c2) { 094 double real2 = this.real * c2.real - this.img * c2.img; 095 double img2 = this.img * c2.real + this.real * c2.img; 096 return new Complex(real2, img2); 097 } 098 099 public Complex plus(Complex c2) { 100 double real = this.real + c2.real; 101 double img = this.img + c2.img; 102 return new Complex(real, img); 103 } 104 }