From cbd2ae7df814c9cccf8b04684673aa93dbd6b0bd Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Fri, 20 Jan 2023 23:41:31 +0000 Subject: [PATCH] copilot rotate: reapply origin offset --- rotate.js | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/rotate.js b/rotate.js index 19deea7..f2c82fc 100755 --- a/rotate.js +++ b/rotate.js @@ -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 -))); \ No newline at end of file + 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 +); \ No newline at end of file