4次元立方体(それぞれの軸に対応する辺の色分け)

4次元立方体とそれぞれの軸に対応する辺を色分け


画像1

画像2

MouseCamera mouseCamera;
float angle = 0;
P4Vector[] points = new P4Vector[16];
float x, y, z, w = 0;
float offset=0;
float red, green, blue = 0;

void setup() {
 size(800, 800, P3D);
 mouseCamera = new MouseCamera(800.0, 0, 0, (height/2.0)/tan(PI*30.0/180.0), 0, 0, 0, 0, 1, 0);

 points[0] =  new P4Vector(-1, -1, -1, 1);
 points[1] =  new P4Vector(1, -1, -1, 1);
 points[2] =  new P4Vector(1, 1, -1, 1);
 points[3] =  new P4Vector(-1, 1, -1, 1);
 points[4] =  new P4Vector(-1, -1, 1, 1);
 points[5] =  new P4Vector(1, -1, 1, 1);
 points[6] =  new P4Vector(1, 1, 1, 1);
 points[7] =  new P4Vector(-1, 1, 1, 1);
 
 points[8] =  new P4Vector(-1, -1, -1, -1);
 points[9] =  new P4Vector(1, -1, -1, -1);
 points[10] = new P4Vector(1, 1, -1, -1);
 points[11] = new P4Vector(-1, 1, -1, -1);
 points[12] = new P4Vector(-1, -1, 1, -1);
 points[13] = new P4Vector(1, -1, 1, -1);
 points[14] = new P4Vector(1, 1, 1, -1);
 points[15] = new P4Vector(-1, 1, 1, -1);
}


void draw() {
 background(255);

 mouseCamera.update();
 PVector[] projected3d = new PVector[16];


 for (int i = 0; i < points.length; i++) {
   P4Vector v = points[i];
   stroke(0);
   strokeWeight(8);

   noFill();

   float[][] rotationXY ={
     {cos(angle), -sin(angle), 0, 0}, 
     {sin(angle), cos(angle), 0, 0}, 
     {0, 0, 1, 0}, 
     {0, 0, 0, 1}
   };

   float[][] rotationXZ ={
     {cos(angle), 0, -sin(angle), 0}, 
     {0, 1, 0, 0}, 
     {sin(angle), 0, cos(angle), 0}, 
     {0, 0, 0, 1}
   };

   float[][] rotationXW ={
     {cos(angle), 0, 0, -sin(angle)}, 
     {0, 1, 0, 0}, 
     {0, 0, 1, 0}, 
     {sin(angle), 0, 0, cos(angle)}, 
   };

   float[][] rotationYZ = {
     {1, 0, 0, 0}, 
     {0, cos(angle), -sin(angle), 0}, 
     {0, sin(angle), cos(angle), 0}, 
     {0, 0, 0, 1}
   };

   float[][] rotationWY = {
     {1, 0, 0, 0}, 
     {0, cos(angle), 0, -sin(angle)}, 
     {0, 0, 1, 0}, 
     {0, sin(angle), 0, cos(angle)}
   };
   float[][] rotationWZ ={
     {1, 0, 0, 0}, 
     {0, 1, 0, 0}, 
     {0, 0, cos(angle), -sin(angle)}, 
     {0, 0, sin(angle), cos(angle)}, 
   };

   P4Vector rotated = matmul(rotationWZ, v, true);
   //rotated = matmul(rotationWY, rotated, true);
   //rotated = matmul(rotationWZ, rotated, true);

   //P4Vector rotated = matmul(rotationXY, v, true);
   //rotated = matmul(rotationYZ, rotated, true);
   //rotated = matmul(rotationXZ, rotated, true);
   //rotated = matmul(rotationXY, rotated, true);


   float distance = 2;
   float w = 1 / (distance - rotated.w);    

   float[][] projection = {
     {w, 0, 0, 0}, 
     {0, w, 0, 0}, 
     {0, 0, w, 0}
   };

   PVector projected = matmul(projection, rotated);
   projected.mult(50);
   projected3d[i] = projected;
   point(projected.x, projected.y, projected.z);
 }


 // Connecting

 //X
 for (int i = 0; i < 8; i+=1) {
   red=111;
   green=209;
   blue=243;
   connect(0, i*2, (i*2)+1, projected3d);
 }

 //Y
 for (int i = 0; i < 4; i+=1) {
   red=56;
   green=142;
   blue=238;
   connect(0, i*4, (i*4)+3, projected3d);
   connect(0, (i*4)+1, (i*4)+2, projected3d);
 }

 //Z
 for (int i = 0; i < 4; i++) {    
   red=214;
   green=238;
   blue=56;
   connect(0, i, i+4, projected3d);
   connect(8, i, i+4, projected3d);
 }

 //W
 for (int i = 0; i < 8; i++) {
   red=238;
   green=56;
   blue=69;
   connect(0, i, i+8, projected3d);
 }

 angle += 0.015;
}

void connect(int offset, int i, int j, PVector[] points) {
 PVector a = points[i+offset];
 PVector b = points[j+offset];
 strokeWeight(4);
 stroke(255);
 stroke(red, green, blue);
 line(a.x, a.y, a.z, b.x, b.y, b.z);
}

void mousePressed() {
 mouseCamera.mousePressed();
}
void mouseDragged() {
 mouseCamera.mouseDragged();
}
void mouseWheel(MouseEvent event) {
 mouseCamera.mouseWheel(event);
}

18歳、学生、クリエイター、未来創造者、過去探索者。