D
Drizzit
Gast
Also wir haben gestern in der Programmierenvorlesung folgendes Programm als Beispiel für Rekursion behandelt:
Den unteren Teil verstehe ich ja. Also das i auf 0 gesetzt wird und dann für i = 0, 1, 2 durchläuft etc.
aber den code von "schneeflocke" kapiere ich nicht wirklich. Kann mir da jemand etwas weiterhelfen?
achja. der code für "turtle" sollte dafür nicht so wichtig sein, aber ich poste ihn hier trotzdem mal:
danke schonmal für jegliche hilfe.
bin hier ziemlich am verzweifeln.
Code:
import java.applet.Applet;
import java.awt.*;
public class Schneeflocken extends Applet {
Turtle t = new Turtle(100, 100);
public void schneeflocke(int n, double laenge, Graphics g) {
if (n == 1)
t.move(g, laenge);
else {
schneeflocke(n - 1, laenge / 3, g);
t.turn(-60);
schneeflocke(n - 1, laenge / 3, g);
t.turn(120);
schneeflocke(n - 1, laenge / 3, g);
t.turn(-60);
schneeflocke(n - 1, laenge / 3, g);
}
}
public void paint(Graphics g) {
t.start(0, 200);
for (int i = 0; i <= 2; i++) {
schneeflocke(5, 400, g);
t.turn(120);
}
}
}
Den unteren Teil verstehe ich ja. Also das i auf 0 gesetzt wird und dann für i = 0, 1, 2 durchläuft etc.
aber den code von "schneeflocke" kapiere ich nicht wirklich. Kann mir da jemand etwas weiterhelfen?
achja. der code für "turtle" sollte dafür nicht so wichtig sein, aber ich poste ihn hier trotzdem mal:
Code:
import static java.lang.Math.IEEEremainder;
import static java.lang.Math.cos;
import static java.lang.Math.round;
import static java.lang.Math.sin;
import java.awt.Graphics;
// Die Turtle-Graphik
public class Turtle {
protected double x, y, angle;
private boolean visible = true;
private Graphics g;
public Turtle() {
init(0, 0);
}
private void init(int x, int y) {
angle = 0;
visible = true;
g = null;
start(x, y, angle);
}
public Turtle(int x, int y) {
init(x, y);
}
// Bewegen und dabei Zeichnen auf absolute Position
public void moveTo(double x, double y) {
if (visible && g != null)
g.drawLine((int) round(this.x), (int) round(this.y), (int) round(x), (int) round(y));
this.x = x;
this.y = y;
}
// Setze nur die Turtle neu
public void start(double x, double y, double angle) {
this.x = x;
this.y = y;
this.angle = angle;
}
// Setze nur die Turtle neu
public void start(double x, double y) {
start (x, y, 0);
}
public void turn(double angle) {
this.angle += angle;
this.angle = IEEEremainder(this.angle, 360.0);
}
public void move(Graphics g, double length) {
double xn, yn;
if (angle == 0) {
xn = x + length;
yn = y;
} else if (angle == 90) {
xn = x;
yn = y + length;
} else if (angle == 180) {
xn = x - length;
yn = y;
} else if (angle == 270) {
xn = x;
yn = y - length;
} else {
xn = x + length * cos(angle / 180.0 * Math.PI);
yn = y + length * sin(angle / 180.0 * Math.PI);
}
if (visible && g != null)
g.drawLine((int) round(x), (int) round(y), (int) round(xn), (int) round(yn));
x = xn;
y = yn;
}
public void move(double length) {
move(g, length);
}
public boolean isVisible() {
return visible;
}
public void setVisible(boolean visible) {
this.visible = visible;
}
public void setGraphics(Graphics g) {
this.g = g;
}
}
danke schonmal für jegliche hilfe.