6
8

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.

「プログラムでシダを描画する」をJulia + Cairoで描画する

Last updated at Posted at 2014-06-19

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

シダ表示プログラムの Julia 版を作ってみました。
グラフィックの描画のために Cairo パッケージを追加しています。
実行すると、カレントディレクトリ(カレントフォルダ)に sida.png というファイルが生成されます。
(尚、バグとかご意見ありましたら https://twitter.com/akmiyoshi までお願いします)

sida.png

とりあえず、ソースだけ…

sida.jl
using Cairo

WIDTH=500;
HEIGHT=500;

W1x(x, y) = 0.836  * x + 0.044 * y;
W1y(x, y) = -0.044 * x + 0.836 * y + 0.169;
W2x(x, y) = -0.141 * x + 0.302 * y;
W2y(x, y) = 0.302  * x + 0.141 * y + 0.127;
W3x(x, y) = 0.141  * x - 0.302 * y;
W3y(x, y) = 0.302  * x + 0.141 * y + 0.169;
W4x(x, y) = 0;
W4y(x, y) = 0.175337 * y;

function plot(x,y)
  x = trunc(x);
  y = trunc(y);
  set_source_rgb(cr,0.0,1.0,0.0); # green
  rectangle(cr,x,y,1.0,1.0);      # plot
  fill(cr);
end

function f(k, x, y)
    if 0 < k
        f(k - 1, W1x(x, y), W1y(x, y));
        if rand() < 0.3
            f(k - 1, W2x(x, y), W2y(x, y));
        end
        if rand() < 0.3
            f(k - 1, W3x(x, y), W3y(x, y));
        end
        if rand() < 0.3
            f(k - 1, W4x(x, y), W4y(x, y));
        end
    else
        plot((x * WIDTH * 0.98) + (WIDTH * 0.5), HEIGHT - (y * HEIGHT * 0.98));
    end
end

# main()

c = CairoRGBSurface(WIDTH,HEIGHT);
cr = CairoContext(c);

set_font_size(cr,WIDTH*0.06);

set_source_rgb(cr,0.2,0.2,0.2);     # gray
rectangle(cr,0.0,0.0,WIDTH,HEIGHT); # background
fill(cr);

move_to(cr,0.0,WIDTH*0.06);
set_source_rgb(cr,1,1,1);       # white
show_text(cr,strftime(time())); # text

f(20, 0, 0);

move_to(cr,0.0,WIDTH*0.06*2);
set_source_rgb(cr,1,1,1);       # whilte
show_text(cr,strftime(time())); # text

write_to_png(c,"sida.png");
6
8
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
6
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?