日常の進捗

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

3次元の結び目

f:id:takawo:20171225173941g:plain

結び目理論のコードがいまいち理解できてなかったので,もう少しやってみることにして,リファレンスのページにあったCのコードをProcessingにポーティングしてみた.座標算出のコードぽかったのでグラフィカルになるようにアレンジした.

コード

float n_segment = 1000;
float degree = 0;
float radius = 30;
float x, y, z, px, py, pz;
ArrayList<PVector> points = new ArrayList<PVector>();

void setup() {
  size(960, 540, P3D);
  colorMode(HSB, 360, 100, 100);
  noFill();
  stroke(0, 0, 100);
}

void draw() {
  background(0, 0, 0);
  translate(width/2, height/2, -500);
  rotateY(frameCount*0.01);
  float mu = degree * TWO_PI / n_segment;
  x = 10 * (cos(mu) + cos(3 * mu)) + cos(2 * mu) * cos(4 * mu);
  y = 6 * sin(mu) + 10 * sin(3 * mu);
  z = 4 * sin(3 * mu) * sin(5 * mu / 2) + 4 * sin(4 * mu) - 2 * sin(6 * mu);
  PVector p = new PVector(x, y, z);
  p.mult(radius);
  points.add(p);

  beginShape();
  int i = 0;
  for (PVector point : points) {
    stroke(i%360, 80, 100);
    strokeWeight(sin(i*0.1)*20+10);
    vertex(point.x, point.y, point.z);
    i++;
  }
  endShape();
  degree += 3;
}

リファレンス

http://paulbourke.net/geometry/knots/

結び目理論 - Wikipedia