From 4aa49793c7b93941504d52b3387e60e6a09de70f Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Sun, 14 May 2017 11:12:47 +0800 Subject: [PATCH] refactored --- src/shape_square.scad | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/src/shape_square.scad b/src/shape_square.scad index f4a5f455..e83eb596 100644 --- a/src/shape_square.scad +++ b/src/shape_square.scad @@ -14,27 +14,34 @@ include <__private__/__is_vector.scad>; include <__private__/__frags.scad>; +include <__private__/__pie_for_rounding.scad>; function shape_square(size, corner_r = 0) = let( - frags = __frags(corner_r), - remain = frags % 4, - corner_frags = (remain / 4) > 0.5 ? frags - remain + 4 : frags - remain, - step_a = 360 / corner_frags, + corner_frags = __frags(corner_r) / 4, x = __is_vector(size) ? size[0] : size, y = __is_vector(size) ? size[1] : size, half_x = x / 2, half_y = y / 2, half_w = half_x - corner_r, half_h = half_y - corner_r, + right_side = [ + for(pt = __pie_for_rounding(corner_r, -90, 0, corner_frags)) + [pt[0] + half_w - corner_r, pt[1] - half_h + corner_r] + ], + top_side = [ + for(pt = __pie_for_rounding(corner_r, 0, 90, corner_frags)) + [pt[0] + half_w - corner_r, pt[1] + half_h - corner_r] + ], + left_side = [ + for(pt = __pie_for_rounding(corner_r, 90, 180, corner_frags)) + [pt[0] - half_w + corner_r, pt[1] + half_h - corner_r] + ], + bottom_side = [ + for(pt = __pie_for_rounding(corner_r, 180, 270, corner_frags)) + [pt[0] - half_w + corner_r, pt[1] - half_h + corner_r] + ], shape_pts = concat( - [[half_x, -half_h], [half_x, half_h]], - [for(a = [step_a:step_a:90 - step_a]) [corner_r * cos(a) + half_w, corner_r * sin(a) + half_h]], - [[half_w, half_y], [-half_w, half_y]], - [for(a = [90 + step_a:step_a:180 - step_a]) [corner_r * cos(a) - half_w, corner_r * sin(a) + half_h]], - [[-half_x, half_h], [-half_x, -half_h]], - [for(a = [180 + step_a:step_a:270 - step_a]) [corner_r * cos(a) - half_w, corner_r * sin(a) - half_h]], - [[-half_w, -half_y], [half_w, -half_y]], - [for(a = [270 + step_a:step_a:360 - step_a]) [corner_r * cos(a) + half_w, corner_r * sin(a) - half_h]] + right_side, top_side, left_side, bottom_side ) ) shape_pts; \ No newline at end of file