FireAlpaca用ブラシスクリプト「五芒星形散布ブラシ」

ブラシスクリプト仕様書で公開されている、ペン軌道の接線方向を向いた二等辺三角形を連続的に描画するスクリプトを改造して、五芒星形(星形)を散布するスクリプトを書いた。

記事下部のリンク先よりブラシファイル(.bsファイル)のDownloadができます

 スクリプト

function point(X , Y , K , D ,W)
   local a = X + math.cos( ( D + K )*math.pi / 180 ) * W
   local b = Y + math.sin( ( D + K )*math.pi / 180 ) * W
   return a , b
end


function main( x, y, p )
   local sw = 400
   local q = 0.7
   local r , si = math.random() * sw , math.random(-90 , 90) 
   local x0 , y0 = x + r *math.cos(si) , y + r * math.sin(si) 
   local k = math.random(0,72)
   local k2_min = 35
   local k2 = math.random(k2_min , 100)
   local d = {54 , 126 ,198 , 270 , 342}
   local px = {0,0,0,0,0}
   local py = {0,0,0,0,0}

   local w = s_width()

      if w < 5 then
        w = 5
      end

   if not firstDraw then
      local distance = bs_distance( lastDrawX - x, lastDrawY - y )
      if distance < q*w then
         return 0
      end
   end

   

   w = w * k2 * 0.01

 

   for i , value in ipairs(d) do
      px[i] , py[i] = point(x0 , y0 , k , d[i] , w)
   end


   --S1
      bs_polygon(x0 , y0)
      bs_polygon(px[1] , py[1])
      bs_polygon(px[3] , py[3])
      local r,g,b = bs_fore()
      bs_fill( r,g,b, 255 )
   --S2
      bs_polygon(x0 , y0)
      bs_polygon(px[1] , py[1])
      bs_polygon(px[4] , py[4])
      local r,g,b = bs_fore()
      bs_fill( r,g,b, 255 )
   --S3
      bs_polygon(x0 , y0)
      bs_polygon(px[2] , py[2])
      bs_polygon(px[4] , py[4])
      local r,g,b = bs_fore()
      bs_fill( r,g,b, 255 )
   --S4
      bs_polygon(x0 , y0)
      bs_polygon(px[2] , py[2])
      bs_polygon(px[5] , py[5])
      local r,g,b = bs_fore()
      bs_fill( r,g,b, 255 )
   --S5
      bs_polygon(x0 , y0)
      bs_polygon(px[3] , py[3])
      bs_polygon(px[5] , py[5])
      local r,g,b = bs_fore()
      bs_fill( r,g,b, 255 )


   lastDrawX = x
   lastDrawY = y
   firstDraw = false

   return 1
end


lastDrawX = 0
lastDrawY = 0
firstDraw = true 

※ star_s150406b.bs

スクリプト解説

描画中心座標

描画の中心座標は、ペン軌道から(r * cos(si) , r * sin(si) )離れた位置で、r,siの範囲はそれぞれ

  • 0 <= r <= 1 * sw
  • -90°<= si <= 90°

である。

   local sw = 400
   ~~
   local r , si = math.random() * sw , math.random(-90 , 90) 
   local x0 , y0 = x + r *math.cos(si) , y + r * math.sin(si) 

描画の大きさ

描画中心座標から外側の5つの頂点までの距離はwで決定される。wはブラシの幅で、5pixelより小さくならないよう定義されている。また、描画の大きさはブラシ幅のk2_min%から100%の範囲で変動する(描画の大きさの最小を決定するのがk2_minである)。

   local k2_min = 35
   local k2 = math.random(k2_min , 100)
   ~~

   local w = s_width()

      if w < 5 then 
        w = 5 
      end 
   ~~   

   w = w * k2 * 0.01

散布量

描画は、最後に描画した座標から現在のペンの座標までの距離が q * w より小さくなったら実行される

    local q = 0.7

   ~~
   if not firstDraw then 
      local distance = bs_distance( lastDrawX - x, lastDrawY - y )
      if distance < q*w then 
         return 0 
      end 
   end

 頂点

※以下、頂点とは五芒星形の外側の頂点のことを言う。

k は描画をランダムに回転させるための変数で、dは各頂点の基本位置を示す変数である。また、px , py は d  に対応して頂点の座標をを格納するための変数である。function point内で頂点の座標を計算し、forループ内で px , py に格納している。

function point(X , Y , K , D ,W)
   local a = X + math.cos( ( D + K )*math.pi / 180 ) * W
   local b = Y + math.sin( ( D + K )*math.pi / 180 ) * W
   return a , b
end


function main( x, y, p )
   ~~
   local k = math.random(0,72)
   ~~
   local d = {54 , 126 ,198 , 270 , 342}
   local px = {0,0,0,0,0}
   local py = {0,0,0,0,0}

   ~~

   for i , value in ipairs(d) do
      px[i] , py[i] = point(x0 , y0 , k , d[i] , w)
   end

 描画

描画は--S1から--S2で行なわれている。

五芒星形を、一つ飛ばしの頂点2個と描画中心座標で作られる三角形5つの重ね合わせと見做し、それぞれの三角形の頂点をbs_polygonで指定、bs_filで塗りつぶすという処理を行っている。

 

実行結果

f:id:Inari777:20150407095436p:plain

時間があったら各パラメータを調整できるようにします。

 

Download

Downloadは以下のリンク先から行ってください。

star_s150406a.bs - Google ドライブ

star_s150406b.bs - Google ドライブ