media @ VU
applet-math-fractal-poincare.jva
applet-math-fractal-poincare.jva
/ applet-math-fractal-poincare
// Helium Poincare map, Evgeny Demidov, 9 Apr 2003
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.StringTokenizer;
public class applet-math-fractal-poincare extends java.applet.Applet implements MouseListener,
KeyListener, ActionListener {
int w,h,w2,h2, Mx=70, My=50, it = 5;
double Qo=3.5, Po=2.8, dP = .2,
Q1_1, P1_1, Q2_1, P2_1, Q12, Q22,
Pmax = 7, ds=.001, ds2, ds8,t;
Image buffImage; Graphics buffGraphics;
boolean bClear = true;
Label lbDs;
TextField tfDs;
Button btClear;
public void init() {
w = getSize().width; h = getSize().height;
w2 = w/2; h2 = h/2;
buffImage = createImage(w, h);
buffGraphics = buffImage.getGraphics();
String s=getParameter("ds");
if (s != null) ds = Double.valueOf(s).doubleValue();
ds2 = ds*2; ds8 = ds/8;
s=getParameter("dp"); if (s != null){
StringTokenizer st = new StringTokenizer(s);
it = Integer.parseInt(st.nextToken());
Qo = Double.valueOf(st.nextToken()).doubleValue();
Po = Double.valueOf(st.nextToken()).doubleValue();
dP = Double.valueOf(st.nextToken()).doubleValue();}
lbDs = new Label("ds", Label.RIGHT); add(lbDs);
tfDs = new TextField( "" + ds, 5); add(tfDs);
tfDs.addKeyListener(this);
btClear = new Button("Clear"); btClear.addActionListener(this); add(btClear);
addMouseListener(this);
clear();
buffGraphics.setColor(Color.black);
for (int i = 0; i < it; i++){
Po += dP;
integrate();}
}
public void destroy() {
removeMouseListener(this);
}
public void integrate() {
double Q1_0, P1_0, Q2_0, P2_0, Q1_12, Q2_12, R12;
t = 0;
Q1_1 = Qo; P1_1 = Po;
Q22 = Q2_1 = 0.; P2_1 = -4.; Q12 = Q1_1*Q1_1;
cycle: while(t < 300.){
while(Q2_1 <= 0.){
Q1_0 = Q1_1; Q2_0 = Q2_1; P1_0 = P1_1; P2_0 = P2_1;
Q1_12 = Q1_0 + P1_0*Q22*ds8;
Q2_12 = Q2_0 + P2_0*Q12*ds8;
Q12 = Q1_12*Q1_12; Q22 = Q2_12*Q2_12; R12 = Q12 + Q22; R12 *= R12;
P1_1 = P1_0 + Q1_12*(2. - P2_0*P2_0/8. - Q22*(1. + Q22/R12))*ds2;
P2_1 = P2_0 + Q2_12*(2. - P1_0*P1_0/8. - Q12*(1. + Q12/R12))*ds2;
Q1_1 = Q1_12 + P1_1*Q22*ds8;
Q2_1 = Q2_12 + P2_1*Q12*ds8;
Q12 = Q1_1*Q1_1; Q22 = Q2_1*Q2_1;
t += ds;
if( (Math.abs(P1_1) > Pmax) || (Math.abs(P2_1) > Pmax) ) break cycle;
}
int x = (int)(Mx*Math.abs(Q1_1)), y = (int)(My*P1_1);
if (Q1_1 < 0.) y = -y;
buffGraphics.drawLine( x,h2-y, x,h2-y);
Q2_1 = Q2_0 = 0.; P2_1 = P2_0 = -4.;
}
repaint();
}
public void mouseClicked(MouseEvent e){} // event handling
public void mousePressed(MouseEvent e) {
int mx0 = e.getX(), my0 = h2 - e.getY();
Qo = (double)mx0/Mx; Po = (double)my0/My;
buffGraphics.setColor(Color.red);
integrate();
}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void actionPerformed(ActionEvent e){
clear(); repaint();
}
public void keyTyped(KeyEvent e) {}
public void keyPressed(KeyEvent e) {}
public void keyReleased(KeyEvent e) {
final int keyEnter = 10;
if (e.getKeyCode() == keyEnter) {
try{
ds = Double.valueOf(tfDs.getText()).doubleValue();
ds2 = ds*2; ds8 = ds/8;
}catch ( NumberFormatException ne) {}
}
e.consume();
}
public void clear(){
buffGraphics.setColor(Color.white);
buffGraphics.fillRect(0, 0, w, h);
buffGraphics.setColor(Color.lightGray);
buffGraphics.drawLine(0,0,0,h);
buffGraphics.drawLine(0,h2,w,h2);
}
public void paint(Graphics g) {
g.drawImage(buffImage, 0, 0, this);
showStatus("Q=" + (float)Qo + " P=" + (float)Po + " s=" + (int)(t/ds) );
}
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.