package UE08_051124_Rekursion1;


import javafx.application.Application;
import javafx.geometry.Point2D;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class Sierpinski_Dreieck extends Application {

    static final int canvasWidth = 580;
    static final int canvasHeight = 502;
    static final int minSize = 10;

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

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Sierpinski-Dreieck");
        Canvas canvas = new Canvas(canvasWidth, canvasHeight);
        primaryStage.setResizable(false);
        primaryStage.setScene(new Scene(new BorderPane(canvas)));
        primaryStage.show();
        Point2D p1 = new Point2D(0, canvas.getHeight()), p2 = new Point2D(canvas.getWidth(), canvas.getHeight()), p3 = new Point2D(canvas.getWidth() / 2, 0);
        drawSierpinskiTriangle(canvas.getGraphicsContext2D(), minSize, p1, p2, p3);
    }

    private void drawSierpinskiTriangle(GraphicsContext gc, int size, Point2D p1, Point2D p2, Point2D p3) {
        if (p2.getX() - p1.getX() <= size) return;
        gc.strokeLine(p1.getX(), p1.getY(), p2.getX(), p2.getY());
        gc.strokeLine(p2.getX(), p2.getY(), p3.getX(), p3.getY());
        gc.strokeLine(p1.getX(), p1.getY(), p3.getX(), p3.getY());
        drawSierpinskiTriangle(gc, size, p1, p1.midpoint(p2), p1.midpoint(p3));
        drawSierpinskiTriangle(gc, size, p1.midpoint(p2), p2, p2.midpoint(p3));
        drawSierpinskiTriangle(gc, size, p1.midpoint(p3), p2.midpoint(p3), p3);
    }
}