1
0
mirror of https://github.com/JustinSDK/dotSCAD.git synced 2025-08-13 18:24:28 +02:00

refactored

This commit is contained in:
Justin Lin
2017-05-14 11:12:47 +08:00
parent 2a504293d7
commit 4aa49793c7

View File

@@ -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;