Rotate Cube

What

正六面体を回転させています。実際に描画しているのは、頂点と線のみです。

How


var vertexes = [
  [1,1,1], [1,1,-1], [1,-1,1], [1,-1,-1],
  [-1,1,1], [-1,1,-1], [-1,-1,1], [-1,-1,-1]
]
var lines = [[1,2,4], [3,5], [3,6], [7], [5,6], [7], [7]]
var rate = 100
        
function setup() {
  createCanvas(500, 500)
  fill(0)
}
        
function draw() {
  background(255);
  theta = frameCount * 0.01
  new_vertexes = []
  for(i in vertexes){
    new_vertexes.push(rotate_vertex(vertexes[i], theta))
  }
  push()
  translate(250,250)
  for(i in new_vertexes){
    v = new_vertexes[i]
    ellipse(v[0] * rate, v[1] * rate, 10);
    if(lines[i] != undefined){
      for(j in lines[i]){
        u = new_vertexes[lines[i][j]]
        line(v[0] * rate, v[1] * rate, u[0] * rate, u[1] * rate)
      }

    }
  }
  pop()
}
        
function rotate_vertex(v, s){
  a2 = cos(s) * v[0] + sin(s) * v[2];
  b2 = sin(s) * sin(s) * v[0] + cos(s) * v[1] - sin(s) * cos(s) * v[2];
  c2 = -cos(s) * sin(s) * v[0] + sin(s) * v[1] + cos(s) * cos(s) * v[2];
  return [a2, b2, c2]
}