前回のエントリ

circle.png square.png hexagon.png decagon.png

という画像を使用しましたが、 この画像の作り方についてメモしておきます。

imagemagick を利用して画像を描く際に使うのは convert -draw です。

まず、白画像を作成します。

convert -size 128x128 xc:white base.png

白画像をキャンバスにしてcircle.pngを作成

convert base.png -draw "circle 64,64 64,8" circle.png

次に、多角形の頂点を求めるプログラム(D言語)を作成し

import std;

void main(string[] args) {
    const int n = to!int(args[1]);
    const int cx = 64;
    const int cy = 64;
    const int r = 64-8;
    const double delta = (360.0 / to!double(n)) * (PI / 180.0);

    for (int i = 0; i < n; i++) {
        const int x = to!int(cos(-PI/2.0 + delta * i) * r) + cx;
        const int y = to!int(sin(-PI/2.0 + delta * i) * r) + cy;
        writef("%d,%d ", x, y);
    }
}

コンパイルします。

dmd coordinates.d

そして、残りの画像を作ります。

convert base.png -draw "polygon $(./coordinates 4)" square.png
convert base.png -draw "polygon $(./coordinates 6)" hexagon.png
convert base.png -draw "polygon $(./coordinates 10)" decagon.png

import std; で標準パッケージが全部インポートできるの、素敵ですね。
(※ その分、バイナリサイズは大きくなるようです。)

参考