日常の進捗

主に自分のための,行為とその習慣化の記録

Mod:Coding Challenge #55: Mathematical Rose Patterns

数学的薔薇.今回のコードで言うと変数dとnの関係でパターンが以下のように変化する.

Rose-rhodonea-curve-7x9-chart-improved.svg
By Jason Davies, CC BY-SA 3.0, Link

15行版書けるかなと思って取り組んだが,書けた.

こういう良い感じのパターン見るとタイリングで作ってみたくなるのでまた後で書くかもしれない.

追記:標本化した.

コード

float d = 8;
float n = 5;

void setup() {
  size(400, 400);
}

void draw() {
  float k = n / d;
  background(51);
  translate(width / 2, height / 2);

  beginShape();
  stroke(255);
  noFill();
  strokeWeight(1);
  for (float a = 0; a < TWO_PI * d; a += 0.02) {
    float r = 200 * cos(k * a);
    float x = r * cos(a);
    float y = r * sin(a);
    vertex(x, y);
  }
  endShape(CLOSE);
}

コード(15行版)

float k = 5.0 / 8.0;
fullScreen();
colorMode(HSB, 360, 100, 100);
background(140, 20, 40);
translate(width/2, height/2);
stroke(0, 0, 100);
strokeWeight(5);
noFill();
beginShape();
for (float angle = 0; angle < 360 * 8.0; angle += 1) {
  float theta = radians(angle);
  float r = (width+height)/6 * cos(k * theta);
  vertex(r * cos(theta), r * sin(theta));
}
endShape(CLOSE);

コード(標本版)

float offset;
float xStep, yStep;
int num = 15;

void setup() {
  size(800, 800);
  colorMode(HSB, 360, 100, 100, 100);
  offset = 50;
  xStep = (width - offset * 2)/(num-1);
  yStep = (height - offset * 2)/(num-1);
}

void draw() {
  background(210, 45, 31);
  noFill();
  float d = 1;
  for (float y = offset; y <= height - offset; y += xStep) {
    float n = 1;
    for (float x = offset; x <= width - offset; x += xStep) {
      float k = n/d;
      stroke(358, 73, 99);
      pushMatrix();
      translate(x, y);
      beginShape();
      for (float a = 0; a < TWO_PI * d; a += TWO_PI/360f) {
        float r = (xStep+yStep)/4 * cos(k *a);
        float rx = r * cos(a);
        float ry = r * sin(a);
        vertex(rx, ry);
      }
      endShape(CLOSE);
      popMatrix();
      n++;
    }
    d++;
  }
  noLoop();
}

リファレンス