Wie kann ich Linien rekursiv zeichnen?

bob651

Aktives Mitglied
Hallo. Die Aufgabe klingt sehr einfach aber trotzdem schaff ich es nicht.
Ich muss einfach nur eine vertikale Linie zeichnen (50PxL ca) und am oberen Ende soll ich 2 weitere Linien drankleben. Also es entsteht ein "Y".
Ich habe jetzt das "Y" hinbekommen mit den genauen Koordionaten, aber so kann es ja nicht sein.
Code:
public class MyDraw extends JPanel {
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
        

          g.drawLine(260,490, 260, 390); 
          g.drawLine(210,440, 260, 390);
          g.drawLine(310,440, 260, 390);

Wie würdet ihr es lösen?
Später muss ich dann an jeder Kante dasselbe machen, aber ich schaff das glaube ich selbst, wenn ich erst ein Grundgerüst habe.
 
Ok, eine Frage: Ist das ein richtiger Ansatz?
Code:
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+5, tempLine.y1+5, tempLine.x2+10, tempLine.y2+10);
 
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);
    }
   
   
   
   
   
  
   
}
 

Neue Themen


Zurück
Oben