ruby-processing2D描画構文、begin_shape, end_shape, curve

今回は、processingの描画周りの命令について、試してみましょう。

rp5 create curve_test.rb 800 600

 何時も通りに、雛形作成です。

 では、早速ですが今回は、曲線や多面体など複雑な形状を描画します*1

begin_shape

 begin_shape MODE

本関数を使用することにより、頂点(下記のvertex関数、curve_vertex関数等)を指定しての描画の開始が設定されます。
以降、下記の描画の終了が設定されるまでの間、頂点を設定しての描画が有効です。

MODEを設定することにより、設定した頂点で描画される形状が変化します。

end_shape

end_shape MODE

本関数を使用することにより、頂点を指定しての描画の終了が設定されます。

MODEを設定することにより、設定した頂点で描画される形状が変化します。

begin_shape,end_shape

上記begin_shapeとend_shapeは、2つ1組で使用します。
また、上記2つの関数の間で、座標変換関数(transform, rotate, etc)や、図形描画関数(rect, line, etc)は使用できません。

vertex

 vertex(x_coordinate, y_coordinate)

本関数を使用することにより、頂点が設定されます。

 設定した頂点で描画される図形は、デフォルト*2では、直線です。
設定した順序で頂点間が直線で結ばれます。

curve_vertex

 curve_vertex(x_coordinate, y_coordinate)

本関数を使用することにより、曲線の頂点が設定されます。

 曲線には最低4つの頂点が必要です。また最初と最後の頂点は描画されません。
これは曲線の描画に連続した各頂点の前後の頂点の座標が必要だからです。
最初の頂点は前の頂点が、最後の頂点は後ろの頂点が無いためです。

# Curve Test

class CurveTest < Processing::App

  def setup
    @points = [[50,500],[100,100],
    [200,500],[300,100],
    [450,500],[600,100]]
    
    @count = 0
    @filling = false
  end
  
  def draw
    background 0

    stroke 255
    stroke_weight 5
    @filling ? fill(128) : no_fill
    ary = @points.clone
    case @count
    when 0
      begin_shape
      ary.insert(0,ary.first)
      ary.insert(-1,ary.last)
      ary.each{|point|
        curve_vertex point[0],point[1]             #curve_vertexを使用すことにより曲線を描画
      }
      end_shape
    when 1
      begin_shape               #vertex間が直線で接続され、実線となります
      ary.each{|point|
        vertex point[0],point[1]
      }
      end_shape
    when 2
      begin_shape               #vertex間が直線で接続され、実線となります
      ary.each{|point|
        vertex point[0],point[1]
      }
      end_shape CLOSE           # CLOSEを設定することにより、開始vertexと終端vertexが結ばれて、図形が閉じられます
    when 3
      begin_shape LINES         #2組ずつセットで直線が描画され、破線となります
      ary.each{|point|
        vertex point[0],point[1]
      }
      end_shape
    when 4
      begin_shape POINT         #点が描画されます
      ary.each{|point|
        vertex point[0],point[1]
      }
      end_shape
    end
    
  end
  
  def key_pressed
    case key
    when "n"
      @count += 1
      @count = 4 if @count > 4
    when "p"
      @count -= 1
      @count = 0 if @count < 0
    when "f"
      @filling = !@filling
    else
    end
  end
end

CurveTest.new :title => "Curve Test", :width => 800, :height => 600

*1:line等で、出来なくはないですが

*2:begin_shape MODE 等で変更