//魔方旋转基础模型
class Point{
constructor(x,y,z){
this.mapData={
0:[1,0],
45:[1,1],
90:[0,1],
135:[-1,1],
180:[-1,0],
225:[-1,-1],
270:[0,-1],
315:[1,-1],
}
this.oriData=[x,y,z,1,1,1]
}
//向量转值
getAngle(x,y){
for(let k in this.mapData){
const arr=this.mapData[k]
if(x==arr[0]&&y==arr[1]){
return Number(k);
}
}
}
//获取向量
getPoint(angle){
while(angle<0){
angle=angle+360
}
return this.mapData[angle];
}
//沿x轴旋转
translateX(angle){
const [x,y,z,xp,yp,zp]=this.oriData
const ag1=this.getAngle(y,z);
const [y2,z2]=this.getPoint(angle+ag1)
const ag2=this.getAngle(yp,zp)
const [yp2,zp2]=this.getPoint(angle+ag2)
this.oriData=[x,y2,z2,xp,yp2,zp2]
}
//沿y轴旋转
translateY(angle){
const [x,y,z,xp,yp,zp]=this.oriData
const ag1=this.getAngle(z,x);
const [z2,x2]=this.getPoint(angle+ag1)
const ag2=this.getAngle(zp,xp)
const [zp2,xp2]=this.getPoint(angle+ag2)
this.oriData=[x2,y,z2,xp2,yp,zp2]
}
//沿z轴旋转
translateZ(angle){
const [x,y,z,xp,yp,zp]=this.oriData
const ag1=this.getAngle(x,y);
const [x2,y2]=this.getPoint(angle+ag1)
const ag2=this.getAngle(xp,yp)
const [xp2,yp2]=this.getPoint(angle+ag2)
this.oriData=[x2,y2,z,xp2,yp2,zp]
}
}
const p1=new Point(1,0,1)
p1.translateX(90)
console.log(p1.oriData)