xxxxxxxxxx
71
let P1, P2, P3;
const sc = 100;
const dtheta = 0.1;
let MYZ, MZY, MXZ, MZX;
function setup() {
createCanvas(400, 400);
P1 = createVector(1, 0, 0);
P2 = createVector(0, 1, 0);
P3 = p5.Vector.cross(P1, P2);
MYZ = math.matrix([
[1, 0, 0],
[0 , cos(dtheta), -sin(dtheta)],
[0 , sin(dtheta), cos(dtheta)]
]);
MZY = math.matrix([
[1, 0, 0],
[0 , cos(dtheta), sin(dtheta)],
[0 , -sin(dtheta), cos(dtheta)]
]);
MXZ = math.matrix([
[cos(dtheta), 0, -sin(dtheta)],
[0, 1, 0],
[sin(dtheta), 0, cos(dtheta)]
]);
MZX = math.matrix([
[cos(dtheta), 0, sin(dtheta)],
[0, 1, 0],
[-sin(dtheta), 0, cos(dtheta)]
]);
}
function draw() {
background(220);
translate(width / 2, height / 2);
stroke(255, 0, 0);
line(0, 0, P1.x * sc, P1.y * sc);
stroke(0, 255, 0);
line(0, 0, P2.x * sc, P2.y * sc);
stroke(0, 0, 255);
line(0, 0, P3.x * sc, P3.y * sc);
if (keyIsDown(UP_ARROW)) rotVecs(MYZ);
if (keyIsDown(DOWN_ARROW)) rotVecs(MZY);
if (keyIsDown(RIGHT_ARROW)) rotVecs(MXZ);
if (keyIsDown(LEFT_ARROW)) rotVecs(MZX);
findOrthVec();
}
function rotVecs(M) {
let result = math.multiply(M, [P1.x, P1.y, P1.z]);
P1 = createVector(result.subset(math.index(0)), result.subset(math.index(1)), result.subset(math.index(2)));
result = math.multiply(M, [P2.x, P2.y, P2.z]);
P2 = createVector(result.subset(math.index(0)), result.subset(math.index(1)), result.subset(math.index(2)));
P3 = p5.Vector.cross(P1, P2);
}
function findOrthVec() {
const max_its = 100;
let p1 = math.matrix([[P1.x],[P1.y],[P1.z]]);
let p2 = math.matrix([[P2.x],[P2.y],[P2.z]]);
let p3 = math.matrix([[random()],[random()],[random()]]);
let v = math.matrix([[0],[0],[-1]]);
for (let ni = 0; ni<max_its; ni++){
G = math.transpose(math.concat(p1,p2,p3,1));
S = math.matrix([[1,0,0],[0,1,0],[0,0,0.5]]);
p3 = math.subtract(p3,math.multiply(math.multiply(math.inv(G),S),math.add(math.multiply(G,p3),v)));
}
}