11
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

「プログラムでシダを描画する」をJavaで描画する(PNG生成)

Last updated at Posted at 2014-06-19

空前のシダ描画ブーム到来!?(^^;)
あなたも得意なプログラミング言語でシダを描画してみよう!

Java (AWT)バージョンに続いて、Java で PNG を生成するバージョンを書いてみました。
実行すると、カレントディレクトリ(カレントフォルダ)に sida.png というファイルが生成されます。
(バグとかご意見ありましたら https://twitter.com/akmiyoshi までお願いします)

sida-2014-0620-0130.png

Sida.java
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Random;
import javax.imageio.ImageIO;

public class Sida {

    static final int WIDTH = 500;
    static final int HEIGHT = 500;
    static final Random rnd = new Random();
    static final BufferedImage buffImage = new BufferedImage(WIDTH, HEIGHT,
            BufferedImage.TYPE_INT_BGR);
    static final Graphics2D graphics = buffImage.createGraphics();

    public static void main(String[] args) {
        graphics.setBackground(Color.DARK_GRAY);
        graphics.clearRect(0, 0, WIDTH, HEIGHT);
        f(20, 0, 0);
        try {
            ImageIO.write(buffImage, "png", new File("sida.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    static double W1x(double x, double y) {
        return 0.836 * x + 0.044 * y;
    }

    static double W1y(double x, double y) {
        return -0.044 * x + 0.836 * y + 0.169;
    }

    static double W2x(double x, double y) {
        return -0.141 * x + 0.302 * y;
    }

    static double W2y(double x, double y) {
        return 0.302 * x + 0.141 * y + 0.127;
    }

    static double W3x(double x, double y) {
        return 0.141 * x - 0.302 * y;
    }

    static double W3y(double x, double y) {
        return 0.302 * x + 0.141 * y + 0.169;
    }

    static double W4x(double x, double y) {
        return 0;
    }

    static double W4y(double x, double y) {
        return 0.175337 * y;
    }

    static void f(int k, double x, double y) {
        if (0 < k) {
            f(k - 1, W1x(x, y), W1y(x, y));
            if (rnd.nextDouble() < 0.3) {
                f(k - 1, W2x(x, y), W2y(x, y));
            }
            if (rnd.nextDouble() < 0.3) {
                f(k - 1, W3x(x, y), W3y(x, y));
            }
            if (rnd.nextDouble() < 0.3) {
                f(k - 1, W4x(x, y), W4y(x, y));
            }
        } else {
            plot((x * WIDTH * 0.98) + (WIDTH * 0.5), HEIGHT - (y * HEIGHT * 0.98));
        }
    }

    static void plot(double xd, double yd) {
        int x = (int) xd;
        int y = (int) yd;
        graphics.setPaint(Color.green);
        graphics.drawLine(x, y, x, y);
    }
}
11
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?