题目描述

如下图所示,先垂直绘制一根线段,然后在线段长度的三分之一处和三分之二处分别
以固定夹角绘制另外两根线段,长度分别为原线段的2/3和1/3,如此反复,直至线段长度
小于某个较小的值。其中,线条颜色以及长度,夹角都可以自行进行微调。

算法思想

递归画

结果

代码

package CourseDesign;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.stage.Stage;

import java.util.Random;


public class A2 extends Application {
   
    private int key = 50;// 长度
    private int angle = 30;// 角度

    public static void main(String[] args) {
   
        Application.launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
   
        Pane pane = new Pane();

        // 第一条线
        Line line = new Line(250, 0, 250, 500);
        line.setStroke(Color.GREEN);
        line.setStrokeWidth(5);
        pane.getChildren().add(line);

        // 新建线程画线
        Thread thread = new Thread(() -> {
   
               try {
   
                   Thread.sleep(600);
               } catch (InterruptedException e) {
   
                   e.printStackTrace();
               }
               // 执行函数
               tree(pane, (int)line.getStartX(), (int)line.getStartY(), (int)line.getEndX(), (int)line.getEndY());
        });
        thread.setDaemon(true);
        thread.start();

        primaryStage.setScene(new Scene(pane, 800, 500));
        primaryStage.show();
    }

    private void tree(Pane pane, int startX, int startY, int endX, int endY) {
   
        // 超过长度结束
        if(Math.sqrt(Math.pow(endX - startX, 2) + Math.pow(endY - startY, 2)) < key)
            return;
        // 初始化颜色
        Random random = new Random();
        int c1 = random.nextInt(256);
        int c2 = random.nextInt(256);
        int c3 = random.nextInt(256);

        // 新建线程画线
            try {
   
                Thread.sleep(300);
            } catch (InterruptedException e) {
   
                e.printStackTrace();
            }
            // 画左边条线
            Runnable runnable = () -> {
   
                int Sx = startX + (endX - startX) / 3;
                int Sy = startY + (endY - startY) / 3;
                int Ex = (int)((startX - Sx) * Math.cos(Math.PI / 180 * (-angle)) - (startY - Sy) * Math.sin(Math.PI / 180 * (-angle)) + Sx);// 计算x的结束坐标
                int Ey = (int)((startX - Sx) * Math.sin(Math.PI / 180 * (-angle)) + (startY - Sy) * Math.cos(Math.PI / 180 * (-angle)) + Sy);// 计算y的结束坐标
                Line line = new Line(Sx, Sy, Ex, Ey);
                line.setStroke(Color.rgb(c1, c2, c3));
                line.setStrokeWidth(5);
                pane.getChildren().add(line);
                tree(pane, (int)line.getEndX(), (int)line.getEndY(), Sx, Sy);
            };
            Platform.runLater(runnable);

            try {
   
                Thread.sleep(300);
            } catch (InterruptedException e) {
   
                e.printStackTrace();
            }
            // 画右边条线
            Runnable runnable1 = () -> {
   
                int Sx = startX + (endX - startX) * 2 / 3;
                int Sy = startY + (endY - startY) * 2 / 3;
                int Ex = (int)((startX - Sx) * Math.cos(Math.PI / 180 * angle) - (startY - Sy) * Math.sin(Math.PI / 180 * angle) + Sx);// 计算x的结束坐标
                int Ey = (int)((startX - Sx) * Math.sin(Math.PI / 180 * angle) + (startY - Sy) * Math.cos(Math.PI / 180 * angle) + Sy);// 计算y的结束坐标
                Line line = new Line(Sx, Sy, Ex, Ey);
                line.setStroke(Color.rgb(c1, c2, c3));
                line.setStrokeWidth(5);
                pane.getChildren().add(line);
                tree(pane, (int)line.getEndX(), (int)line.getEndY(), Sx, Sy);
            };
            Platform.runLater(runnable1);
    }
}