Java program to generate Sierpinski Carpet using Ternary Algorithm.

Sierpinski Carpet Ternary Algorithm 729x729px
Sierpinski Carpet Ternary Algorithm 729x729px

Note that image size must be the power of 3, for example: 729.

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) {
        SierpinskiCarpet sierpinskiTriangle = new SierpinskiCarpet();

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

SierpinskiCarpet.java

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

// 2018 TheFlyingKeyboard and released under MIT License
// theflyingkeyboard.net
public class SierpinskiCarpet {
    private final int foregroundColor;
    private final int backgroundColor;
    private int[] imagePixelData;

    public SierpinskiCarpet() {
        this.foregroundColor = Color.BLACK.getRGB();
        this.backgroundColor = Color.WHITE.getRGB();
    }

    public SierpinskiCarpet(final Color foregroundColor, final Color backgroundColor) {
        this.foregroundColor = foregroundColor.getRGB();
        this.backgroundColor = backgroundColor.getRGB();
    }

    public BufferedImage generate(final int carpetSize) {
        BufferedImage bufferedImage = new BufferedImage(carpetSize, carpetSize, BufferedImage.TYPE_INT_RGB);
        imagePixelData = ((DataBufferInt) bufferedImage.getRaster().getDataBuffer()).getData();

        double counter = 0.0d;

        for (int x = 0; x < carpetSize; ++x) {
            for (int y = 0; y < carpetSize; ++y) {
                if(isTernaryZero(x, y, carpetSize)){
                    imagePixelData[y * carpetSize + x] = backgroundColor;
                }else{
                    imagePixelData[y * carpetSize + x] = foregroundColor;
                }

                ++counter;
            }

            System.out.println((counter / (carpetSize * carpetSize)) * 100.0f + "%");
        }

        return bufferedImage;
    }


    private boolean isTernaryZero(int x, int y, int steps){
        for(int i = 1; i < steps; i *= 3){
            if((x / i) % 3 == 1 && (y / i) % 3 == 1){
                return false;
            }
        }

        return true;
    }
}

 



Java Sierpinski Carpet Ternary
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