Skip to content

Instantly share code, notes, and snippets.

@boredzo
Created January 5, 2016 06:02
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boredzo/fde487c724a40a26fa9c to your computer and use it in GitHub Desktop.
Save boredzo/fde487c724a40a26fa9c to your computer and use it in GitHub Desktop.
Skew operator for OpenSCAD
/*skew takes an array of six angles:
*x along y
*x along z
*y along x
*y along z
*z along x
*z along y
*/
module skew(dims) {
matrix = [
[ 1, dims[0]/45, dims[1]/45, 0 ],
[ dims[2]/45, 1, dims[4]/45, 0 ],
[ dims[5]/45, dims[3]/45, 1, 0 ],
[ 0, 0, 0, 1 ]
];
multmatrix(matrix)
children();
}
skew([45, 0, 0, 0, 0, 0])
cube([10,10,10]);
@dguerizec
Copy link

It should be tan(dims[0]) instead of dims[0]/45, and so on for all the others.

@ziggy90127
Copy link

/*skew takes an array of six angles:
 *x along y
 *x along z
 *y along x
 *y along z
 *z along x
 *z along y
 */
module skew(dims) {
matrix = [
	[ 1, tan(dims[0]), tan(dims[1]), 0 ],
	[ tan(dims[2]), 1, tan(dims[3]), 0 ],
	[ tan(dims[4]), tan(dims[5]), 1, 0 ],
	[ 0, 0, 0, 1 ]
];
multmatrix(matrix)
children();
}

@nash8114
Copy link

Thanks for this module. Saved me the hassle of figuring out the correct Matrix.
Thanks to dguerizec and ziggy90127 for correcting the matrix formula and posting the complete solution.

@ygoe
Copy link

ygoe commented Aug 7, 2022

Thank you, works wonderfully (only tried the corrected version from the comment). I've changed the interface a bit to make it easier to use:

// Skews the child geometry.
// xy: Angle towards X along Y axis.
// xz: Angle towards X along Z axis.
// yx: Angle towards Y along X axis.
// yz: Angle towards Y along Z axis.
// zx: Angle towards Z along X axis.
// zy: Angle towards Z along Y axis.
module skew(xy = 0, xz = 0, yx = 0, yz = 0, zx = 0, zy = 0) {
	matrix = [
		[ 1, tan(xy), tan(xz), 0 ],
		[ tan(yx), 1, tan(yz), 0 ],
		[ tan(zx), tan(zy), 1, 0 ],
		[ 0, 0, 0, 1 ]
	];
	multmatrix(matrix)
	children();
}

Example:

skew(xz = 20, yx = 40)
cube([5, 5, 10]);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment