From bfd9286a5138b96c19eeda74cf5e39f36537d4ca Mon Sep 17 00:00:00 2001 From: Quinn Date: Sun, 9 Jan 2022 15:10:14 -0800 Subject: [PATCH] Add math library --- math.scad | 5 +++++ round_polygon.scad | 33 +++++---------------------------- 2 files changed, 10 insertions(+), 28 deletions(-) create mode 100644 math.scad diff --git a/math.scad b/math.scad new file mode 100644 index 0000000..00b7ee1 --- /dev/null +++ b/math.scad @@ -0,0 +1,5 @@ +function normalize(vec) = vec / norm(vec); + +function lerp(a,b,t) = a + (b - a) * t; + +function scale_xy(shape, xy, allow_zero = false) = let(s = allow_zero ? xy : sign(xy == 0 ? 1 : xy) * max(abs(xy), 0.0001)) [ for (i = shape) [i.x * s, i.y * s]]; diff --git a/round_polygon.scad b/round_polygon.scad index b7cc224..ca30564 100644 --- a/round_polygon.scad +++ b/round_polygon.scad @@ -1,31 +1,10 @@ -$fn=32; - -card = 1; -corn = 1 / sqrt(2); - -oct = [ - [card,0], - [corn,corn], - [0,card], - [-corn,corn], - [-card,0], - [-corn,-corn], - [0,-card], - [corn,-corn] -]; - -function scale_xy(shape, xy, allow_zero = false) = let(s = allow_zero ? xy : sign(xy == 0 ? 1 : xy) * max(abs(xy), 0.0001)) [ for (i = shape) [i.x * s, i.y * s]]; - -function normalize(vec) = vec / norm(vec); - -function grow(shape, mag) = [ for (i = shape) i + normalize(i) * mag]; - -function add_radius(shape, rad) = grow(shape, rad/cos(180/len(shape))); - -function lerp(a,b,t) = a + (b - a) * t; +use; module round_poly(shape, radius, size = 0, sharpness = 0){ - corner_r = lerp(radius, 0, sharpness); + function grow(s, m) = [ for (i = s) i + normalize(i) * m]; + function add_radius(s, r) = grow(s, r/cos(180/len(s))); + +corner_r = lerp(radius, 0, sharpness); corner_offset = lerp(0, radius, sharpness); minkowski(){ @@ -33,5 +12,3 @@ module round_poly(shape, radius, size = 0, sharpness = 0){ circle(max(corner_r, 0.0001)); } } - -round_poly(oct, radius, 4, 0); \ No newline at end of file