import java.awt.Graphics; public class c07_lsystems extends java.applet.Applet { static final int left = 30; static final int h = 300; double x, y; double dx, dy; java.util.Stack stack = new java.util.Stack(); class State { double x, y, dx, dy; State() { this.x = c07_lsystems.this.x; this.y = c07_lsystems.this.y; this.dx = c07_lsystems.this.dx; this.dy = c07_lsystems.this.dy; } void restore() { c07_lsystems.this.x = this.x; c07_lsystems.this.y = this.y; c07_lsystems.this.dx = this.dx; c07_lsystems.this.dy = this.dy; } } public void paint(Graphics g) { for (int stage = 7; stage <= 7; stage++) { x = left+h/2; y = left+h; dx = 0; dy = -10*(stage+1); forward(g, stage); } } void forward(Graphics g, int stage) { if (stage > 0) { forward(g, stage-1); save(); left(); forward(g, stage-1); restore(); save(); right(); forward(g, stage-1); restore(); } else { g.drawLine((int)x, (int)y, (int)(x+dx), (int)(y+dy)); x += dx; y += dy; } } void left() { double dx1 = dx; dx = 0.707 * dx1 - 0.707 * dy; dy = 0.707 * dx1 + 0.707 * dy; } void right() { double dx1 = dx; dx = 0.866 * dx1 + 0.500 * dy; dy = -0.500 * dx1 + 0.866 * dy; } void save() { stack.push(new State()); dx *= 0.7; dy *= 0.7; } void restore() { State state = (State)stack.pop(); state.restore(); } }