copilot rotate: reapply origin offset

This commit is contained in:
Starbeamrainbowlabs 2023-01-20 23:41:31 +00:00
parent 3e123ddc56
commit cbd2ae7df8
No known key found for this signature in database
GPG Key ID: 1BE5172E637709C2

@ -16,6 +16,7 @@
*/
function rotate_point_3d(origin, point, x, y, z)
{
point = [...point];
point[0] = point[0] - origin[0];
point[1] = point[1] - origin[1];
point[2] = point[2] - origin[2];
@ -34,8 +35,8 @@ function rotate_point_3d(origin, point, x, y, z)
var x3 = x2 * Math.cos(z) - y2 * Math.sin(z);
var y3 = x2 * Math.sin(z) + y2 * Math.cos(z);
var z3 = z2;
return [x3, y3, z3];
return [x3+origin[0], y3+origin[1], z3+origin[2]];
}
function deg_to_rad(deg)
@ -48,13 +49,29 @@ function point_to_string(point, decimal_places=3)
return "(x " + point[0].toFixed(decimal_places) + ", y " + point[1].toFixed(decimal_places) + ", z " + point[2].toFixed(decimal_places) + ")";
}
function do_test(origin, point, ...args) {
const result = rotate_point_3d(origin, point, ...args);
console.log(`ORIGIN`, point_to_string(origin), `\tPOINT`, point_to_string(point), `\tRESULT`, point_to_string(result));
}
console.log(point_to_string(rotate_point_3d(
[ 0, 0, 0 ],
[ 5, 0, 0 ],
do_test(
[1, 0, 0],
[5, 0, 0],
deg_to_rad(0), // rotate around x axis = yz plane
deg_to_rad(90), // rotate around y axis = xz plane
deg_to_rad(0) // rotate around z axis = xy plane
)));
deg_to_rad(0), // rotate around y axis = xz plane
deg_to_rad(180) // rotate around z axis = xy plane
);
do_test(
[1, 0, 0],
[0, 1, 0],
deg_to_rad(0), // rotate around x axis = yz plane
deg_to_rad(0), // rotate around y axis = xz plane
deg_to_rad(180) // rotate around z axis = xy plane
);
do_test(
[10, 100, 0],
[0, 0, 0],
deg_to_rad(0), // rotate around x axis = yz plane
deg_to_rad(0), // rotate around y axis = xz plane
deg_to_rad(180) // rotate around z axis = xy plane
);