Java program to generate Sierpinski Triangle (Fractal) of specified resolution using Recursion Algorithm, even in high resolutions – tested for 40K with increased Java VM  heap size – -Xmx8g option.

Sierpinski Triangle 1000x1000px Level Of Recursion: 10
Sierpinski Triangle 1000x1000px Level Of Recursion: 10

Main.java

import javax.imageio.ImageIO;
import java.io.File;

// 2018 TheFlyingKeyboard and released under MIT License
// theflyingkeyboard.net
public class Main {
    public static void main(String[] args) {
        SierpinskiTriangle sierpinskiTriangle = new SierpinskiTriangle();

        try {
            ImageIO.write(sierpinskiTriangle.generate(1000, 10), "png", new File("SierpinskiTriangle.png"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

SierpinskiTriangle.java

import java.awt.*;
import java.awt.image.BufferedImage;

// 2018 TheFlyingKeyboard and released under MIT License
// theflyingkeyboard.net
public class SierpinskiTriangle {
    private final Color foregroundColor;
    private final Color backgroundColor;
    private Graphics2D graphics2D;
    private final int lineSize;

    public SierpinskiTriangle(){
        this.foregroundColor = Color.BLACK;
        this.backgroundColor = Color.WHITE;
        lineSize = 1;
    }

    public SierpinskiTriangle(final Color foregroundColor, final Color backgroundColor, final int lineSize){
        this.foregroundColor = foregroundColor;
        this.backgroundColor = backgroundColor;
        this.lineSize = lineSize;
    }

    public BufferedImage generate(final int triangleSize, final int recursionLevel){
        BufferedImage bufferedImage = new BufferedImage(triangleSize, triangleSize, BufferedImage.TYPE_INT_RGB);
        graphics2D = bufferedImage.createGraphics();

        graphics2D.setColor(backgroundColor);
        graphics2D.fillRect(0, 0, triangleSize, triangleSize);

        graphics2D.setColor(foregroundColor);
        graphics2D.setStroke(new BasicStroke(lineSize));

        int triangleHeight = (int) Math.round(triangleSize * Math.sqrt(3.0d) / 2.0d);
        Point pointA = new Point(0, triangleHeight);
        Point pointB = new Point(triangleSize / 2, 0);
        Point pointC = new Point(triangleSize, triangleHeight);

        drawTriangle(pointA, pointB, pointC, recursionLevel);

        return bufferedImage;
    }

    private void drawTriangle(Point pointA, Point pointB, Point pointC, final int recursionLevel){
        if(recursionLevel == 1){
            Polygon polygon = new Polygon();
            polygon.addPoint(pointA.x, pointA.y);
            polygon.addPoint(pointB.x, pointB.y);
            polygon.addPoint(pointC.x, pointC.y);

            graphics2D.draw(polygon);
            return;
        }

        Point pointAB = middlePoint(pointA, pointB);
        Point pointBC = middlePoint(pointB, pointC);
        Point pointAC = middlePoint(pointA, pointC);

        drawTriangle(pointA, pointAB, pointAC, recursionLevel - 1);
        drawTriangle(pointAB, pointB, pointBC, recursionLevel - 1);
        drawTriangle(pointAC, pointBC, pointC, recursionLevel - 1);
    }

    private Point middlePoint(Point pointA, Point pointB){
        return new Point((pointA.x + pointB.x) / 2, (pointA.y + pointB.y) / 2);
    }
}

 



Java Sierpinski Triangle Recursion
Tagged on:             

Leave a Reply

Your email address will not be published. Required fields are marked *

By continuing to use the site, you agree to the use of cookies. You can read more about it the Cookies&Privacy Policy Section Above. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this. You can read more about it the Cookies&Privacy Policy Section.

Close