コエンザイムのブログ

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

ModulatedEllipseクラスにsetXCoordinateBase()を追加しました

ModulatedEllipseクラスに機能を追加しました。setXCoordinateBase()です。

noise関数は与える引数が同じであれば、同じ値を返します。そのため、ModulatedEllipseクラスを使って図形を作成した場合、ノイズの滑らかさや効き具合等の設定が同じなら、同じ図形が作成されます。

ノイズの滑らかさや効き具合等の設定が同じでも、違う図形も作成することができるように、setXCoordinateBase()を作成しました。

setXCoordinateBase()で渡す値が同じで、他の設定も同じなら、同じ図形が作成されます。

setXCoordinateBase()で渡す値が異なるなら、他の設定が同じでも、違う図形が作成されます。

setSCoordinateBase()を利用して作成した図形の例
setSCoordinateBase()を利用して作成した図形の例

以下はソースコードです。開発環境は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(0.5);
  e.setNoiseEffectRatio(0.5);
  e.setVertexNum(600);
  e.setNoiseSymmetry(false);
  for (i = 0; i < 6; i++)
  {
    e.setPosition(x, y);
    e.setXCoordinateBase((i + 1) * 1000);
    e.draw();
    fill(0);
    text("NoiseSmoothRatio:0.5", x - cellWidth / 2, y - cellHeight / 2 + 10);
    text("NoiseEffectRatio:0.5", x - cellWidth / 2, y - cellHeight / 2 + 25);
    text("Symmetry:false", x - cellWidth / 2, y - cellHeight / 2 + 40);
    text("VertexNum:600", x - cellWidth / 2, y - cellHeight / 2 + 55);
    text("XCoordinateBase:" + ((i + 1) * 1000), x - cellWidth / 2, y - cellHeight / 2 + 70);
    fill(255);

    x += cellWidth;
  }
  
  x = cellWidth / 2;
  y += cellHeight;
  e.setNoiseSmoothRatio(0.5);
  e.setNoiseEffectRatio(0.5);
  e.setVertexNum(600);
  e.setNoiseSymmetry(false);
  for (i = 0; i < 6; i++)
  {
    e.setPosition(x, y);
    e.setXCoordinateBase((i + 1) * 1000);
    e.draw();
    fill(0);
    text("NoiseSmoothRatio:0.5", x - cellWidth / 2, y - cellHeight / 2 + 10);
    text("NoiseEffectRatio:0.5", x - cellWidth / 2, y - cellHeight / 2 + 25);
    text("Symmetry:false", x - cellWidth / 2, y - cellHeight / 2 + 40);
    text("VertexNum:600", x - cellWidth / 2, y - cellHeight / 2 + 55);
    text("XCoordinateBase:" + ((i + 1) * 1000), x - cellWidth / 2, y - cellHeight / 2 + 70);
    fill(255);

    x += cellWidth;
  }

  x = cellWidth / 2;
  y += cellHeight;
  e.setNoiseSmoothRatio(0.5);
  e.setNoiseEffectRatio(0.5);
  e.setNoiseSymmetry(true);
  e.setVertexNum(600);
  for (i = 0; i < 6; i++)
  {
    e.setPosition(x, y);
    e.setXCoordinateBase((i + 1) * 1000);
    e.draw();
    fill(0);
    text("NoiseSmoothRatio:0.5", x - cellWidth / 2, y - cellHeight / 2 + 10);
    text("NoiseEffectRatio:0.5", x - cellWidth / 2, y - cellHeight / 2 + 25);
    text("Symmetry:true", x - cellWidth / 2, y - cellHeight / 2 + 40);
    text("VertexNum:600", x - cellWidth / 2, y - cellHeight / 2 + 55);
    text("XCoordinateBase:" + ((i + 1) * 1000), x - cellWidth / 2, y - cellHeight / 2 + 70);
    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;
  int mXCoordinateBase = 0;

  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 setXCoordinateBase(int c)
  {
    mXCoordinateBase = c;
  }

  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(mXCoordinateBase + xCoordinate * mNoiseRoughnessCoefficient) * mNoiseEffectRatio), 
        mCenterY + sin(-HALF_PI + theta) * v * (1.0 - noise(mXCoordinateBase + xCoordinate * mNoiseRoughnessCoefficient) * mNoiseEffectRatio));
    }
    endShape(CLOSE);
  }
}