0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

コッホ曲線の拡張

Last updated at Posted at 2025-06-04

代表的なコッホ曲線

image.png

void setup() {
  size(800, 400);
  background(255);
  stroke(0);

  drawKoch(new PVector(50, 300), new PVector(750, 300), 6);
}

void drawKoch(PVector a, PVector b, int n) {
  if (n == 0) {
    line(a.x, a.y, b.x, b.y);
    return;
  }
  // 3等分点
  PVector v = PVector.sub(b, a).div(3);
  PVector p1 = PVector.add(a, v);         // 1/3点
  PVector p2 = PVector.add(a, v.copy().mult(2)); // 2/3点

  // 正三角形の頂点
  float th = atan2(b.y - a.y, b.x - a.x) - PI/3;
  float len = v.mag();
  PVector peak = new PVector(
    p1.x + cos(th)*len,
    p1.y + sin(th)*len
  );

  drawKoch(a, p1, n-1);
  drawKoch(p1, peak, n-1);
  drawKoch(peak, p2, n-1);
  drawKoch(p2, b, n-1);
}

正三角形を正方形へ変更

image.png

void setup() {
  size(800, 400);
  background(255);
  stroke(0);

  drawKochSquare(new PVector(50, 360), new PVector(750, 360), 6);
}

void drawKochSquare(PVector a, PVector b, int n) {
  if (n == 0) {
    line(a.x, a.y, b.x, b.y);
    return;
  }
  PVector v = PVector.sub(b, a).div(3);
  PVector p = PVector.add(a, v);
  PVector q = PVector.add(a, v.copy().mult(2));
  float px = p.x, py = p.y, qx = q.x, qy = q.y;
  float dx = qx - px, dy = qy - py;
  float ox = dy, oy = -dx;
  PVector up = new PVector(px + ox, py + oy);
  PVector right = new PVector(qx + ox, qy + oy);
  drawKochSquare(a, p, n-1);
  drawKochSquare(p, up, n-1);
  drawKochSquare(up, right, n-1);
  drawKochSquare(right, q, n-1);
  drawKochSquare(q, b, n-1);
}

平面から立体へ

image.png

//import nervoussystem.obj.*;

float size = 600;
int depth = 4;

void setup() {
  size(600, 500, P3D);
  fill(255);
  stroke(50,20);
}

void draw() {
  background(255);
  lights();
  //beginRecord("nervoussystem.obj.OBJExport", "FractalSquare.obj");
  translate(width/2, height/2, -300);
  rotateX(PI/3);
  rotateZ(PI/3);
  drawFractalSquare(size, depth);
  //endRecord();exit();
}

void drawFractalSquare(float size, int depth) {
  if (depth == 0) {
    // xy平面に正方形
    beginShape();
    vertex(-size/2, -size/2, 0);
    vertex( size/2, -size/2, 0);
    vertex( size/2,  size/2, 0);
    vertex(-size/2,  size/2, 0);
    endShape(CLOSE);
    return;
  }
  float sub = size / 3;
  for (int i=0; i<3; i++) {
    for (int j=0; j<3; j++) {
      pushMatrix();
      translate((i-1)*sub, (j-1)*sub, 0);
      if (i==1 && j==1) {// 中央:押し出し
        pushMatrix();
        translate(0, 0, size/3);
        drawFractalSquare(sub, depth-1);
        popMatrix();
        for (int k=0; k<4; k++) {// 側面(4方向, ズレ補正あり)
          pushMatrix();
          rotateZ(HALF_PI*k);
          translate(0, -sub/2, sub/2); // 中心合わせる
          rotateX(HALF_PI);
          drawFractalSquare(sub, depth-1);
          popMatrix();
        }
      } else {
        drawFractalSquare(sub, depth-1);
      }
      popMatrix();
    }
  }
}

立方体6面に配置し3Dプリント

IMG_9948.jpeg

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?