コエンザイムのブログ

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

ModulatedEllipseクラスに機能を追加しました

ModulatedEllipseクラスに機能を追加しました。setVertexNum()で頂点数を設定可能です。

頂点でパーリンノイズの効果が出ますので、頂点数でも効果を制御できるようになりました。また、頂点数を3や4など比較的小さく設定することにより楕円を多角形型に変形させることもできます。

setVertexNum()を利用した図形の描画例
setVertexNum()を利用した図形の描画例

以下はソースコードです。開発環境は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;
  e.setNoiseSmoothRatio(1.0);
  e.setNoiseEffectRatio(0.0);
  e.setNoiseSymmetry(false);
  for (i = 0; i < 6; i++)
  {
    e.setPosition(x, y);
    e.setVertexNum(i + 3);
    e.draw();
    fill(0);
    text("SmoothRatio:1.0", x - cellWidth / 2, y - cellHeight / 2 + 10);
    text("EffectRatio:0.0", x - cellWidth / 2, y - cellHeight / 2 + 25);
    text("Symmetry:false", x - cellWidth / 2, y - cellHeight / 2 + 40);
    text("VertexNum:" + (i + 3), x - cellWidth / 2, y - cellHeight / 2 + 55);
    fill(255);

    x += cellWidth;
  }
  
  x = cellWidth / 2;
  y += cellHeight;
  e.setNoiseSmoothRatio(0.7);
  e.setNoiseEffectRatio(0.3);
  e.setNoiseSymmetry(false);
  for (i = 0; i < 6; i++)
  {
    e.setPosition(x, y);
    e.setVertexNum((i + 1) * 100);
    e.draw();
    fill(0);
    text("SmoothRatio:0.7", x - cellWidth / 2, y - cellHeight / 2 + 10);
    text("EffectRatio:0.3", x - cellWidth / 2, y - cellHeight / 2 + 25);
    text("Symmetry:false", x - cellWidth / 2, y - cellHeight / 2 + 40);
    text("VertexNum:" + ((i + 1) * 100), x - cellWidth / 2, y - cellHeight / 2 + 55);
    fill(255);

    x += cellWidth;
  }

  x = cellWidth / 2;
  y += cellHeight;
  e.setNoiseSmoothRatio(0.5);
  e.setNoiseEffectRatio(0.5);
  e.setVertexNum(700);
  for (i = 0; i < 2; i++)
  {
    e.setNoiseSymmetry(!((i % 2) == 0));
    e.setPosition(x, y);
    e.draw();
    fill(0);
    text("SmoothRatio:0.5", x - cellWidth / 2, y - cellHeight / 2 + 10);
    text("EffectRatio:0.5", x - cellWidth / 2, y - cellHeight / 2 + 25);
    text("Symmetry:" + !((i % 2) == 0), x - cellWidth / 2, y - cellHeight / 2 + 40);
    text("VertexNum:700", x - cellWidth / 2, y - cellHeight / 2 + 55);
    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;
  int mVertexNum = 628;

  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 setVertexNum(int n)
  {
    mVertexNum = n;
  }
  
  void draw()
  {
    float h = mDiameterH / 2.0;
    float v = mDiameterV / 2.0;
    
    int xCoordinate = 1;
    int cnt = 0;
    float theta = 0.0;
    beginShape();
    for (cnt = -1; cnt <= mVertexNum + 1; cnt++)
    {
      theta = TWO_PI * cnt / mVertexNum;

      if (mNoiseSymmetryFlg)
      {
        if (cnt < 0)
        {
          xCoordinate = 2;
        }
        else if (cnt >= mVertexNum)
        {
          xCoordinate = 1 + cnt % mVertexNum;
        }
        else
        {
          if (cnt > int(mVertexNum / 2.0))
          {
            xCoordinate = int(mVertexNum / 2.0) - (cnt - int(mVertexNum / 2.0));
          }
          else
          {
            xCoordinate = cnt + 1;
          }
        }
      }
      else
      {
        if (cnt < 0)
        {
          xCoordinate = mVertexNum;
        }
        else if (cnt >= mVertexNum)
        {
          xCoordinate = 1 + cnt % mVertexNum;
        }
        else
        {
          xCoordinate = cnt + 1;
        }
      }

      curveVertex(
        mCenterX + cos(-HALF_PI + theta) * h * (1.0 - noise(xCoordinate * mNoiseRoughnessCoefficient) * mNoiseEffectRatio), 
        mCenterY + sin(-HALF_PI + theta) * v * (1.0 - noise(xCoordinate * mNoiseRoughnessCoefficient) * mNoiseEffectRatio));
    }
    endShape(CLOSE);
  }
}