This commit is contained in:
Dyana Wu 2020-10-09 12:53:00 +08:00
commit 3557ecb1ac

View file

@ -3,21 +3,23 @@
* Copyright (C) 2012 Peter Uithoven * Copyright (C) 2012 Peter Uithoven
* *
* License: LGPL 2.1 or later * License: LGPL 2.1 or later
* 2D Shapes
* ngon(sides, radius, center=false);
* complexRoundSquare(size,rads1=[0,0], rads2=[0,0], rads3=[0,0], rads4=[0,0], center=true)
* roundedSquare(pos=[10,10],r=2)
* ellipsePart(width,height,numQuarters)
* donutSlice(innerSize,outerSize, start_angle, end_angle)
* pieSlice(size, start_angle, end_angle) //size in radius(es)
* ellipse(width, height) {
*/ */
// 2D Shapes // Examples - (layouts.scad is required for exapmles)
//ngon(sides, radius, center=false); // example2DShapes(); use <layouts.scad>;
//complexRoundSquare(size,rads1=[0,0], rads2=[0,0], rads3=[0,0], rads4=[0,0], center=true)
//roundedSquare(pos=[10,10],r=2)
//ellipsePart(width,height,numQuarters)
//donutSlice(innerSize,outerSize, start_angle, end_angle)
//pieSlice(size, start_angle, end_angle) //size in radius(es)
//ellipse(width, height) {
// Examples module example2DShapes(){
/*use <layouts.scad>; grid(105,105,true,4)
grid(105,105,true,4) {
{
// ellipse // ellipse
ellipse(50,75); ellipse(50,75);
@ -44,15 +46,21 @@ grid(105,105,true,4)
donutSlice([40,22],[50,30],180,270); donutSlice([40,22],[50,30],180,270);
donutSlice([50,20],50,180,270); donutSlice([50,20],50,180,270);
donutSlice([20,30],[50,40],0,270); donutSlice([20,30],[50,40],0,270);
}*/ }
//---------------------- }
// end examples ----------------------
// size, top left radius, top right radius, bottom right radius, bottom left radius, center module complexRoundSquare(
module complexRoundSquare(size,rads1=[0,0], rads2=[0,0], rads3=[0,0], rads4=[0,0], center=true) size, // Size
{ rads1=[0,0], // Top left radius
rads2=[0,0], // Top right radius
rads3=[0,0], // Bottom right radius
rads4=[0,0], // Bottom left radius
center=true // center
) {
width = size[0]; width = size[0];
height = size[1]; height = size[1];
//%square(size=[width, height],center=true); // %square(size=[width, height],center=true);
x1 = 0-width/2+rads1[0]; x1 = 0-width/2+rads1[0];
y1 = 0-height/2+rads1[1]; y1 = 0-height/2+rads1[1];
x2 = width/2-rads2[0]; x2 = width/2-rads2[0];
@ -62,78 +70,101 @@ module complexRoundSquare(size,rads1=[0,0], rads2=[0,0], rads3=[0,0], rads4=[0,0
x4 = 0-width/2+rads4[0]; x4 = 0-width/2+rads4[0];
y4 = height/2-rads4[1]; y4 = height/2-rads4[1];
scs = 0.1; //straight corner size scs = 0.1; // straight corner size
x = (center)? 0: width/2; x = (center)? 0: width/2;
y = (center)? 0: height/2; y = (center)? 0: height/2;
translate([x,y,0]) translate([x,y,0])
hull()
{ {
hull() {
// top left // top left
if(rads1[0] > 0 && rads1[1] > 0) if(rads1[0] > 0 && rads1[1] > 0)
translate([x1,y1]) mirror([1,0]) ellipsePart(rads1[0]*2,rads1[1]*2,1); translate([x1,y1])
mirror([1,0])
ellipsePart(rads1[0]*2,rads1[1]*2,1);
else else
translate([x1,y1]) square(size=[scs, scs]); translate([x1,y1])
square(size=[scs, scs]);
// top right // top right
if(rads2[0] > 0 && rads2[1] > 0) if(rads2[0] > 0 && rads2[1] > 0)
translate([x2,y2]) ellipsePart(rads2[0]*2,rads2[1]*2,1); translate([x2,y2])
ellipsePart(rads2[0]*2,rads2[1]*2,1);
else else
translate([width/2-scs,0-height/2]) square(size=[scs, scs]); translate([width/2-scs,0-height/2])
square(size=[scs, scs]);
// bottom right // bottom right
if(rads3[0] > 0 && rads3[1] > 0) if(rads3[0] > 0 && rads3[1] > 0)
translate([x3,y3]) mirror([0,1]) ellipsePart(rads3[0]*2,rads3[1]*2,1); translate([x3,y3])
mirror([0,1])
ellipsePart(rads3[0]*2,rads3[1]*2,1);
else else
translate([width/2-scs,height/2-scs]) square(size=[scs, scs]); translate([width/2-scs,height/2-scs])
square(size=[scs, scs]);
// bottom left // bottom left
if(rads4[0] > 0 && rads4[1] > 0) if(rads4[0] > 0 && rads4[1] > 0)
translate([x4,y4]) rotate([0,0,-180]) ellipsePart(rads4[0]*2,rads4[1]*2,1); translate([x4,y4])
rotate([0,0,-180])
ellipsePart(rads4[0]*2,rads4[1]*2,1);
else else
#translate([x4,height/2-scs]) square(size=[scs, scs]); #translate([x4,height/2-scs])
} square(size=[scs, scs]);
} }
} }
module roundedSquare(pos=[10,10],r=2) { module roundedSquare(pos=[10,10],r=2) {
minkowski() { minkowski()
{
square([pos[0]-r*2,pos[1]-r*2],center=true); square([pos[0]-r*2,pos[1]-r*2],center=true);
circle(r=r); circle(r=r);
} }
} }
// round shapes // round shapes
// The orientation might change with the implementation of circle... // The orientation might change with the implementation of circle...
module ngon(sides, radius, center=false){ module ngon(sides, radius, center=false) {
rotate([0, 0, 360/sides/2]) circle(r=radius, $fn=sides, center=center); rotate([0, 0, 360/sides/2])
circle(r=radius, $fn=sides, center=center);
} }
module ellipsePart(width,height,numQuarters)
{ module ellipsePart(width,height,numQuarters) {
o = 1; //slight overlap to fix a bug o = 1; //slight overlap to fix a bug
difference() difference()
{ {
ellipse(width,height); ellipse(width,height);
if(numQuarters <= 3) if(numQuarters <= 3)
translate([0-width/2-o,0-height/2-o,0]) square([width/2+o,height/2+o]); translate([0-width/2-o,0-height/2-o,0])
square([width/2+o,height/2+o]);
if(numQuarters <= 2) if(numQuarters <= 2)
translate([0-width/2-o,-o,0]) square([width/2+o,height/2+o*2]); translate([0-width/2-o,-o,0])
square([width/2+o,height/2+o*2]);
if(numQuarters < 2) if(numQuarters < 2)
translate([-o,0,0]) square([width/2+o*2,height/2+o]); translate([-o,0,0])
square([width/2+o*2,height/2+o]);
} }
} }
module donutSlice(innerSize,outerSize, start_angle, end_angle)
{ module donutSlice(innerSize,outerSize, start_angle, end_angle) {
difference() difference()
{ {
pieSlice(outerSize, start_angle, end_angle); pieSlice(outerSize, start_angle, end_angle);
if(len(innerSize) > 1) ellipse(innerSize[0]*2,innerSize[1]*2);
else circle(innerSize); if(len(innerSize) > 1)
ellipse(innerSize[0]*2,innerSize[1]*2);
else
circle(innerSize);
} }
} }
module pieSlice(size, start_angle, end_angle) //size in radius(es)
{ module pieSlice(size, start_angle, end_angle) { //size in radius(es)
rx = ((len(size) > 1)? size[0] : size); rx = len(size) > 1? size[0] : size;
ry = ((len(size) > 1)? size[1] : size); ry = len(size) > 1? size[1] : size;
trx = rx* sqrt(2) + 1; trx = rx* sqrt(2) + 1;
try = ry* sqrt(2) + 1; try = ry* sqrt(2) + 1;
a0 = (4 * start_angle + 0 * end_angle) / 4; a0 = (4 * start_angle + 0 * end_angle) / 4;
@ -141,6 +172,7 @@ module pieSlice(size, start_angle, end_angle) //size in radius(es)
a2 = (2 * start_angle + 2 * end_angle) / 4; a2 = (2 * start_angle + 2 * end_angle) / 4;
a3 = (1 * start_angle + 3 * end_angle) / 4; a3 = (1 * start_angle + 3 * end_angle) / 4;
a4 = (0 * start_angle + 4 * end_angle) / 4; a4 = (0 * start_angle + 4 * end_angle) / 4;
if(end_angle > start_angle) if(end_angle > start_angle)
intersection() { intersection() {
if(len(size) > 1) if(len(size) > 1)
@ -158,6 +190,8 @@ module pieSlice(size, start_angle, end_angle) //size in radius(es)
]); ]);
} }
} }
module ellipse(width, height) { module ellipse(width, height) {
scale([1, height/width, 1]) circle(r=width/2); scale([1, height/width, 1])
circle(r=width/2);
} }