media @ VU
applet-math-fractal-logistic-it.jva
applet-math-fractal-logistic-it.jva
/ applet-math-fractal-logistic-it
// Interactive X = R X(1-X) Iterations, Evgeny Demidov, 18 Nov 2005
import java.awt.*;
import java.awt.event.*;
public class applet-math-fractal-logistic-it extends java.applet.Applet
implements MouseMotionListener, KeyListener {
Image buffImage; Graphics buffGraphics;
int maxIt = 10, N = 1, numIt, w,h,h2, lbSize = 30,
xFun[],yFun[], xGist[],yGist[], xIt[],yIt[];
double R = 2, maxIYI = 2., Step;
Label lbIt, lbN, lbR;
TextField tfIt, tfN, tfR;
public void init() {
h = Integer.parseInt(getParameter("height")) - lbSize; h2 = h/2;
w = Integer.parseInt(getParameter("width"));
String s=getParameter("MaxIt"); if (s != null) maxIt=Integer.parseInt(s);
s=getParameter("N"); if (s != null) N=Integer.parseInt(s);
s = getParameter("R"); if (s != null) R = Double.valueOf(s).doubleValue();
xFun = new int[h]; for (int i = h-1; i >= 0; i--) xFun[i] = i;
yFun = new int[h];
buffImage = createImage(w, h);
buffGraphics = buffImage.getGraphics();
lbR = new Label("R"); add(lbR);
tfR = new TextField( "" + R, 9); add(tfR);
lbN = new Label("N"); add(lbN);
tfN = new TextField( "" + N, 3); add(tfN);
lbIt = new Label("It"); add(lbIt);
tfIt = new TextField( "" + maxIt, 3); add(tfIt);
tfR.addKeyListener(this); tfN.addKeyListener(this);
tfIt.addKeyListener(this);
addMouseMotionListener(this);
Step = 1. / h;
draw();
}
public void destroy() { removeMouseMotionListener(this); }
public void keyTyped(KeyEvent e) {}
public void keyPressed(KeyEvent e) {}
public void keyReleased(KeyEvent e) {
final int keyEnter = 10;
if (e.getKeyCode() == keyEnter) {
try{
N = Integer.parseInt( tfN.getText() );
maxIt = Integer.parseInt( tfIt.getText() );
R = Double.valueOf(tfR.getText()).doubleValue();
}catch ( NumberFormatException ne) {}
draw(); repaint();
}
e.consume();
}
public void mouseMoved(MouseEvent e) {}
public void mouseDragged(MouseEvent e) {
R = 4*Step*(h - (e.getY() - lbSize));
tfR.setText( Float.toString((float)R) );
draw(); repaint();
e.consume();
}
public void draw() {
xGist = new int[maxIt<<1 + 1]; yGist = new int[maxIt<<1 + 1];
xIt = new int[maxIt+1]; yIt = new int[maxIt+1];
for (int i = 0; i <= maxIt; i++)
xIt[i] = h + (int)((w-h-1)*(double)i/(maxIt));
generateFun(); iterations();
buffGraphics.setColor(Color.white);
buffGraphics.fillRect(0, 0, w - 1, h - 1);
buffGraphics.setColor(Color.black);
buffGraphics.drawRect(0, 0, w - 1, h - 1);
buffGraphics.drawLine(h, 0, h, h - 1);
buffGraphics.setColor(Color.green);
buffGraphics.drawLine(0, h, h, 0);
buffGraphics.setColor(Color.blue);
buffGraphics.drawPolyline( xFun, yFun, h );
buffGraphics.setColor(Color.red);
buffGraphics.drawPolyline( xGist, yGist, numIt<<1 );
buffGraphics.drawPolyline( xIt, yIt, numIt+1 );
}
public void paint(Graphics g) {
g.drawImage(buffImage, 0, lbSize, this);
showStatus( "It=" + numIt);
}
public void iterations() {
double X = .5, Y; int bakX, bakY,iY;
xGist[0] = bakX = h2;
yGist[0] = h; yIt[0] = h2;
for (int i = 1; i <= maxIt; i++) {
Y = X;
for (int n = N; n > 0; n--) Y = R*Y*(1-Y);
iY = (int)(Y/Step);
int i2 = i<<1;
xGist[i2 - 1] = bakX;
yGist[i2 - 1] = bakY = h - iY;
xGist[i2] = bakX = iY;
yGist[i2] = yIt[i] = bakY;
X = Y; numIt = i;
if (Math.abs(Y) > maxIYI) break;
}
}
public void generateFun() {
for (int i = h - 1; i >= 0; i--) {
double X = i*Step;
for (int n = N; n > 0; n--) X = R*X*(1.-X);
yFun[i] = h - (int)(X/Step);
}
}
public void update(Graphics g) { paint(g); }
}
(C) A. Eliëns
2/9/2007
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.