Rekursiv geometrische Figuren darstellen

bob651

Aktives Mitglied
Hallo, ich habe nicht gewusst, dass es ein Unterforum für Aufgaben gibt, deshalb stelle ich mal meine Frage hier.
Ich habe schon jede Google Seite dazu angeschaut, aber die sind alle so kompliziert gelöst, wobei meine Aufgabe recht simpel ist.

-->Was ich will:
x Eine vertikale Linie zeichnen (Länge ca. 100 pixel).
x Am oberen Ende eine Line mit halber Länge 45° nach links und eine gleichlange Linie 45° nach rechts zeichnen.
x Nach sieben Rekursionen abbrechen.


Habe jetzt diesen Code soweit geschafft;
Code:
 public class MyDraw extends JPanel {
 public void paintComponent(Graphics g) {
 super.paintComponent(g);
        

            Line2D.Double tempLine = line;

            for (int q = 0; q < count; q++){

                g.drawLine((int)tempLine.x1, (int)tempLine.y1, (int)tempLine.x2, (int)tempLine.y2);
               
                tempLine = new Line2D.Double(tempLine.x1+260, tempLine.y1+490, tempLine.x2+260, tempLine.y2+390);

 

bob651

Aktives Mitglied
Code:
public class binarbaum {
   
  
    public static void main(String[] args) {
       
        setup();
    
       ;
       while(true){
            draw();
            StdDraw.show();
        }
    }
    static int anzahl = 7;
    static double scale = 3;
    static double linkerWinkel = 45;
    static double rechterWinkel = 45;
  
   
    // alles initialisieren
    public static void setup() {
        StdDraw.setXscale(0,100);
        StdDraw.setYscale(0,100);
    }
   
    // animation
    public static void draw() {
        malen(50, 0, 90, anzahl);
    }

   
    static void malen(int x, int y, double angle, int zoom) {
        // base case
        if (zoom == 0) return;
       
        //ende von linie berechnen
        double angleRadians = Math.toRadians(angle);
        int x2 = x + (int) (Math.cos(angleRadians) * zoom * scale);
        int y2 = y + (int) (Math.sin(angleRadians) * zoom * scale);
       
     
       
        // baum rekursiv malen
        StdDraw.line(x, y, x2, y2);
        malen(x2, y2, angle - linkerWinkel, zoom - 1);
       malen(x2, y2, angle + rechterWinkel, zoom - 1);
    }
   
   
   
   
   
  
   
}


biddeschön.
wird zwar nicht um die hälfte kleiner wie ich es wollte, aber naja
 

Oben