ランダムに三角形を描く

■プログラム説明(ソースコード説明)
 三角形を描くには3つの頂点座標を決める必要があります。座標は乱数を求めるMath.random()を使います。求めた座標からselect()を使って範囲を選択します。選択された範囲をfill()を使って塗りつぶします。塗りつぶす色、カラーモード、不透明度を指定することができます。

---------------------------------------------------------------------------------------------------------
■ソースコード
RGBColor = new SolidColor(); ←カラーオブジェクトを作成します
w = activeDocument.width;
h = activeDocument.height;
w = parseInt(w);
h = parseInt(h);
num = 10; ←三角形を描く数を指定します
for (i=0; i<num; i++)
{
RGBColor.red = Math.random() * 255; ←0〜255の範囲の輝度にします(赤)
RGBColor.green = Math.random() * 255; ←0〜255の範囲の輝度にします(緑)
RGBColor.blue = Math.random() * 255; ←0〜255の範囲の輝度にします(青)
x1 = Math.floor(Math.random() * w); ←ドキュメントの縦幅内での乱数値にします
y1 = Math.floor(Math.random() * h); ←ドキュメントの縦幅内での乱数値にします
x2 = Math.floor(Math.random() * w);
y2 = Math.floor(Math.random() * h);
x3 = Math.floor(Math.random() * w);
y3 = Math.floor(Math.random() * h);
selReg = [[x1,y1],[x2,y2],[x3,y3]]; ←選択範囲の座標を指定します
activeDocument.selection.select(selReg); ←範囲を選択します
activeDocument.selection.fill(RGBColor,ColorBlendMode.NORMAL, 20, false); ←不透明度20%で塗りつぶします
}

---------------------------------------------------------------------------------------------------------
■使い方
1:ファイルメニューからスクリプトを実行します。
2:ドキュメントにランダムなサイズと色で三角形が表示されます。

---------------------------------------------------------------------------------------------------------
■ポイント
 描いた三角形を別々のレイヤーにして生成したい場合には以下のようにactiveDocument.artLayers.add()を使います。

RGBColor = new SolidColor();
w = activeDocument.width;
h = activeDocument.height;
w = parseInt(w);
h = parseInt(h);
num = 10;
for (i=0; i<num; i++)
{
RGBColor.red = Math.random() * 255;
RGBColor.green = Math.random() * 255;
RGBColor.blue = Math.random() * 255;
x1 = Math.floor(Math.random() * w);
y1 = Math.floor(Math.random() * h);
x2 = Math.floor(Math.random() * w);
y2 = Math.floor(Math.random() * h);
x3 = Math.floor(Math.random() * w);
y3 = Math.floor(Math.random() * h);
selReg = [[x1,y1],[x2,y2],[x3,y3]];
activeDocument.artLayers.add();
activeDocument.selection.select(selReg);
activeDocument.selection.fill(RGBColor,ColorBlendMode.NORMAL, 20, false);
}

---------------------------------------------------------------------------------------------------------
■注意
 なし

■実際のスクリプトをダウンロード(sample.js.zip)