Mod: Coding Challenge #87: 3D Knots
ここのところブログにログを残していなかったので(コードは書いてた)、3Dのノット(結び目)に関するチュートリアルをやってみた.球体の面上の点を求める式に似ているのが面白い.コードの中のコメントアウトしている式がそれぞれ,変数betaから半径や角度A,Bを計算して、更にそれを元にしてxyzの座標を計算する式.
関係ないが,OpenProcessingのP3D/WEBGLではstrokeWeightが効かない.p5.js Web Editorで書いてみたけどオートリロードやブラウザベースで完結する良さはなかなか快適だし,p5.jsも良いような気もしてる.
コード
float beta = 0; ArrayList<PVector> points = new ArrayList<PVector>(); // r(beta) = 0.8 + 1.6 * sin(6 * beta) // theta(beta) = 2 * beta // phi(beta) = 0.6 * pi * sin(12 * beta) //x = r * cos(phi) * cos(theta) //y = r * cos(phi) * sin(theta) //z = r * sin(phi) void setup() { size(960, 540, P3D); colorMode(HSB, 360, 100, 100); } void draw() { background(0, 0, 0); translate(width/2, height/2, -500); rotateY(frameCount*0.01); float r = 250 * ( 0.8 + 1.6 * sin(6 * beta)); float theta = 2 * beta; float phi = 0.6 * PI * sin(3 * beta); float x = r * cos(phi) * cos(theta); float y = r * cos(phi) * sin(theta); float z = r * sin(phi); points.add(new PVector(x, y, z)); beta += 0.005; noFill(); strokeWeight(8); beginShape(); for (PVector point : points) { float m = point.mag(); stroke((m*0.5)%360, 80, 100); vertex(point.x, point.y, point.z); } endShape(); }