コエンザイムのブログ

ジェネレーティブアートが好きです

ModulatedEllipseクラスを試作しました

パーリンノイズで変形させた楕円を表示するModulatedEllipseクラスを試作しました。

ModulatedEllipseクラスを使った図形の描画例

コンストラクタでは中心のx位置、y位置、幅、高さを指定します。

setNoiseSmoothRatio()でノイズの滑らか度を指定できます。範囲は0.0〜1.0で、値が高いほど滑らかになります。

setNoiseEffectRatio()では効果の度合いを指定できます。範囲は0.0〜1.0で、値が高いほど効果が強くなります。

また、setNoiseSymmetry()で効果を左右対称にできます。trueで左右対称になり、falseで左右非対称になります。

以下はソースコードです。開発環境はMacBook Air、Processingです。

ModulatedEllipseSample.pde

// ModulatedEllipseSample

final int cMyWidth = 1280;
final int cMyHeight = 720;

ModulatedEllipse e;

void settings()
{
  size(cMyWidth, cMyHeight);
}

void setup()
{
  noLoop();
}

void draw()
{
  float cellWidth = width / 6;
  float cellHeight = height / 3;

  e = new ModulatedEllipse(0, 0, cellWidth - 10, cellHeight - 10);

  int i;
  float x = cellWidth / 2;
  float y = cellHeight / 2;
  float ratio = 0.0;
  e.setNoiseEffectRatio(0.3);
  for (i = 0; i < 6; i++)
  {
    e.setPosition(x, y);
    e.setNoiseSmoothRatio(ratio);
    e.draw();
    fill(0);
    text("SmoothRatio:" + ratio, x - cellWidth / 2, y - cellHeight / 2 + 10);
    text("EffectRatio:0.3", x - cellWidth / 2, y - cellHeight / 2 + 10 + 15);
    fill(255);

    x += cellWidth;
    ratio += 0.2;
  }
  
  x = cellWidth / 2;
  y += cellHeight;
  ratio = 0.0;
  e.setNoiseSmoothRatio(0.7);
  for (i = 0; i < 6; i++)
  {
    e.setPosition(x, y);
    e.setNoiseEffectRatio(ratio);
    e.draw();
    fill(0);
    text("SmoothRatio:0.7", x - cellWidth / 2, y - cellHeight / 2 + 10);
    text("EffectRatio:" + ratio, x - cellWidth / 2, y - cellHeight / 2 + 10 + 15);
    fill(255);

    x += cellWidth;
    ratio += 0.2;
  }

  x = cellWidth / 2;
  y += cellHeight;
  ratio = 0.0;
  e.setNoiseSmoothRatio(0.3);
  e.setNoiseEffectRatio(0.7);
  for (i = 0; i < 2; i++)
  {
    e.setNoiseSymmetry(!((i % 2) == 0));
    e.setPosition(x, y);
    e.draw();
    fill(0);
    text("SmoothRatio:0.3", x - cellWidth / 2, y - cellHeight / 2 + 10);
    text("EffectRatio:0.7", x - cellWidth / 2, y - cellHeight / 2 + 10 + 15);
    text("Symmetry:" + !((i % 2) == 0), x - cellWidth / 2, y - cellHeight / 2 + 10 + 15 + 15);
    fill(255);

    x += cellWidth;
  }
}

ModulatedEllipse.pde

// ModulatedEllipse

class ModulatedEllipse
{
  float mCenterX = 0;
  float mCenterY = 0;
  float mDiameterH = 100;
  float mDiameterV = 100;
  float mNoiseSmoothRatio = 1.0;
  float mNoiseRoughnessCoefficient = 0.0;
  float mNoiseEffectRatio = 0.5;
  boolean mNoiseSymmetryFlg = false;

  ModulatedEllipse(float x, float y, float h, float v)
  {
    mCenterX = x;
    mCenterY = y;
    mDiameterH = h;
    mDiameterV = v;
  }

  void setPosition(float x, float y)
  {
    mCenterX = x;
    mCenterY = y;
  }

  void setNoiseSmoothRatio(float r)
  {
    mNoiseSmoothRatio = r;
    mNoiseRoughnessCoefficient = map((1.0 - mNoiseSmoothRatio), 0, 1.0, 0.005, 0.1);
  }
  
  void setNoiseEffectRatio(float r)
  {
    mNoiseEffectRatio = r;
  }
  
  void setNoiseSymmetry(boolean f)
  {
    mNoiseSymmetryFlg = f;
  }
  
  void draw()
  {
    float h = mDiameterH / 2.0;
    float v = mDiameterV / 2.0;
    
    int i = 1;
    beginShape();
    for (float theta = 0.0; theta <= TWO_PI; theta += 0.005)
    {
      curveVertex(
        mCenterX + cos(-HALF_PI + theta) * h * (1.0 - noise(i * mNoiseRoughnessCoefficient) * mNoiseEffectRatio), 
        mCenterY + sin(-HALF_PI + theta) * v * (1.0 - noise(i * mNoiseRoughnessCoefficient) * mNoiseEffectRatio));
      if ((theta <= PI) || (!mNoiseSymmetryFlg))
      {
        i++;
      }
      else
      {
        i--;
      }
  }
    endShape(CLOSE);
  }
}