Skip to content

Instantly share code, notes, and snippets.

@thehans
thehans / arc_slot.scad
Created May 2, 2022 19:39
arc_slot OpenSCAD example
use <FunctionalOpenSCAD/functional.scad>
// https://github.com/thehans/FunctionalOpenSCAD/blob/master/functional.scad
module arc_slot(R,r,a) {
polygon(concat(
arc(r=R-r, angle=a, offsetAngle=0, c=[0,0], endpoint=false),
arc(r=r, angle=-180, offsetAngle=a-180, c=R*[cos(a),sin(a)], endpoint=false),
arc(r=R+r, angle=-a, offsetAngle=a, c=[0,0], endpoint=false),
arc(r=r, angle=-180, offsetAngle=0, c=[R,0], endpoint=false)
));
@thehans
thehans / alternative_spheres.scad
Last active March 6, 2022 04:02
Alternative sphere implementations in OpenSCAD userspace
translate([-30,0,0]) poly3d(sphere(r=9,$fn=80));
translate([-10,0,0]) poly3d(normalized_cube(r=9,div_count=20));
translate([10,0,0]) poly3d(spherified_cube(9,div_count=20));
translate([30,0,0]) poly3d(icosahedron(9,n=4));
module poly3d(p) {
polyhedron(points=p[0],faces=p[1]);
}
function normalize(v) = v / norm(v); // convert vector to unit vector
@thehans
thehans / BezierHans.scad
Created March 14, 2019 22:57
Bezier curve function for OpenSCAD (any order curve, any dimension points)
BezierExample();
//BezierCircleApprox(10);
// cubic bezier circle approximation example
module BezierCircleApprox(r) {
$fn=1000;
c = 0.551915024494;
points = BezierPath([
@thehans
thehans / tricubic_interp_noise.scad
Created September 12, 2021 03:21
Tricubic interpolation of 3D noise for OpenSCAD
dims = [8,8,3];
min = 0;
max = 2;
seed = 0;
tricubic = create_tricubic_interp(dims, min, max, seed);
step = 1/8;
frames = 64;
z = dims[2] * $t;
@thehans
thehans / bicubic_interp_noise.scad
Created September 11, 2021 00:02
Bicubic interpolation of value noise for OpenSCAD
// Bicubic interpolation of value noise
// by Hans Loeblich
// Reference: https://en.wikipedia.org/wiki/Bicubic_interpolation
dims = [4,4];
min = 0;
max = 2;
seed = 0;
@thehans
thehans / endstop_mount_final.scad
Last active August 28, 2021 18:41
Endstop mount
//use <../Libraries/drawPath.scad>
// Width of base
wBase = 20;
// Length of base
lBase = 25;
// Thickness of base
thBase = 5;
// Fillet radius
rFillet = 1;
@thehans
thehans / gist:ca0ec0516c0b1b4af8ce2f580ef5ffc7
Created June 4, 2021 23:22
Different ways of mirroring in OpenSCAD
independent = false; // Note: Independent mirroring of 2 axes is actually a rotation
fix111 = false; // Only relevant if independent=false
module thing(i,j,k) {
translate([2,2,2]) difference() {
cube(2,center=true);
linear_extrude(convexity=10)
text(text=str("M",i,j,k),size=0.5,valign="center",halign="center");
}
}
@thehans
thehans / nurbs.scad
Created June 2, 2021 19:40
OpenSCAD NURBS quad surface test
function vsum(v,i=0) = len(v)-1 > i ? v[i] + vsum(v, i+1) : v[i];
function N(p, i, U, u) = p == 0 ?
U[i] <= u && u <= U[i+1] ? 1 : 0 :
let(a=u-U[i],b=U[i+p]-U[i],c=U[i+p+1]-u,d=U[i+p+1]-U[i+1])
(b==0?0:a/b*N(p-1,i,U,u)) + (d==0?0:c/d*N(p-1,i+1,U,u));
module nurbs_circle() {
step = 0.0001;
// degree of curve
p = 2;
// Pipe library demonstrating a concept of chained operations
$fn = 72; // define exact number of fragments in curves
// using $fs instead would cause some mismatched # of edges during tapers
echo(str("For best results, all angles should be multiples of ", 360/$fn, " degrees"));
in = 25.4;
// Example of how to use pipe
// Note that all calls are chained (there's only one semicolon, at the end)
@thehans
thehans / radix_sort.scad
Last active January 6, 2021 05:20
OpenSCAD radix sort implementation
// LSD Radix sort (stable)
// base: should be a power of two, otherwise exit condition (d==m) might not be reached.
// key: 3 possible types
// - number (vector index into each element)
// - function (takes element and returns the key value)
// - undef (elements are used directly as the key)
// key values need not be integers, but will be treated as integers via floor()
// e.g. [1.5, 1, 1.2] is considered sorted since they are all same key after floor
function radix_sort(v,base=4,key) = key==undef ?
_rs(v,1,pow(base,-floor(log(max(v))/log(base))),base) :