From e4d93366fa5e9adaf5da6f6a13aa7a160c019aac Mon Sep 17 00:00:00 2001 From: Chris Palmer Date: Mon, 6 Jul 2020 12:43:24 +0100 Subject: [PATCH] Added degrees, radians and rot2_z() to maths.scad. --- readme.md | 4 ++++ utils/maths.scad | 11 ++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 9f7f012..a6d4457 100644 --- a/readme.md +++ b/readme.md @@ -5283,17 +5283,21 @@ Maths utilities for manipulating vectors and matrices. |:--- |:--- | | ```angle_between(v1, v2)``` | Return the angle between two vectors | | ```augment(m)``` | Augment a matrix by adding an identity matrix to the right | +| ```degrees(radians)``` | Convert degrees to radians | | ```euler(R)``` | Convert a rotation matrix to a Euler rotation vector. | | ```identity(n, x = 1)``` | Construct an arbitrary size identity matrix | | ```invert(m)``` | Invert a matrix | | ```nearly_zero(x)``` | True if x is close to zero | +| ```radians(degrees)``` | Convert radians to degrees | | ```reverse(v)``` | Reverse a vector | +| ```rot2_z(a)``` | Generate a 2x2 matrix to rotate around z | | ```rot3_z(a)``` | Generate a 3x3 matrix to rotate around z | | ```rotate(a, v)``` | Generate a 4x4 rotation matrix, ```a``` can be a vector of three angles or a single angle around ```z```, or around axis ```v``` | | ```rowswap(m, i, j)``` | Swap two rows of a matrix | | ```scale(v)``` | Generate a 4x4 matrix that scales by ```v```, which can be a vector of xyz factors or a scalar to scale all axes equally | | ```solve(m, i = 0, j = 0)``` | Solve each row ensuring diagonal is not zero | | ```solve_row(m, i)``` | Make diagonal one by dividing the row by it and subtract from other rows to make column zero | +| ```sqr(x)``` | Square x | | ```transform(v, m)``` | Apply 4x4 transform to a 3 vector by extending it and cropping it again | | ```transform_points(path, m)``` | Apply transform to a path | | ```translate(v)``` | Generate a 4x4 translation matrix, ```v``` can be ```[x, y]```, ```[x, y, z]``` or ```z``` | diff --git a/utils/maths.scad b/utils/maths.scad index e8db429..3a78627 100644 --- a/utils/maths.scad +++ b/utils/maths.scad @@ -20,7 +20,10 @@ // //! Maths utilities for manipulating vectors and matrices. // -function sqr(x) = x * x; +function sqr(x) = x * x; //! Square x +function radians(degrees) = degrees * PI / 180; //! Convert radians to degrees +function degrees(radians) = radians * 180 / PI; //! Convert degrees to radians + function translate(v) = let(u = is_list(v) ? len(v) == 2 ? [v.x, v.y, 0] //! Generate a 4x4 translation matrix, ```v``` can be ```[x, y]```, ```[x, y, z]``` or ```z``` : v @@ -63,6 +66,12 @@ function rot3_z(a) = //! Generate a 3x3 matrix to rotate around z [ s, c, 0], [ 0, 0, 1] ]; +function rot2_z(a) = //! Generate a 2x2 matrix to rotate around z + let(c = cos(a), + s = sin(a)) + [ [ c, -s], + [ s, c] ]; + function scale(v) = let(s = is_list(v) ? v : [v, v, v]) //! Generate a 4x4 matrix that scales by ```v```, which can be a vector of xyz factors or a scalar to scale all axes equally [ [s.x, 0, 0, 0],