1
0
mirror of https://github.com/Irev-Dev/Round-Anything.git synced 2025-08-15 12:23:59 +02:00

Major restructuring unifying 2 space indentation and putting each child on new line to be better for git, still need to decise what to do with for loops inside arrays and turnerary opperators

This commit is contained in:
charlie
2018-02-18 21:46:25 +00:00
committed by Kurt Charlie Hutten
parent 4e08261f37
commit 41cfc7c322
3 changed files with 615 additions and 477 deletions

7
LICENSE Normal file
View File

@@ -0,0 +1,7 @@
Copyright 2020 Kurt Hutten
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@@ -1,8 +1,8 @@
// Library: MinkowskiRound.scad // Library: MinkowskiRound.scad
// Version: 1.0 // Version: 1.0
// Author: IrevDev // Author: IrevDev
// Copyright: 2017 // Copyright: 2020
// License: GPL 3 // License: MIT
/* /*
---Modules ---Modules
@@ -38,10 +38,14 @@ Both this modules do the same thing as minkowskiRound() but focus on either insi
//}//--I rendered this out with a $fn=25 and it took more than 12 hours on my computer //}//--I rendered this out with a $fn=25 and it took more than 12 hours on my computer
module round2d(OR=3,IR=1){ module round2d(OR=3,IR=1){
offset(OR)offset(-IR-OR)offset(IR)children(); offset(OR){
offset(-IR-OR){
offset(IR){
children();
}
}
}
} }
module minkowskiRound(OR=1,IR=1,enable=1,cubeSize=[500,500,500]){ module minkowskiRound(OR=1,IR=1,enable=1,cubeSize=[500,500,500]){

View File

@@ -2,8 +2,8 @@
// Version: 1.0 // Version: 1.0
// Author: IrevDev // Author: IrevDev
// Contributors: TLC123 // Contributors: TLC123
// Copyright: 2017 // Copyright: 2020
// License: GPL 3 // License: MIT
@@ -15,15 +15,40 @@ module examples(){
slotPosition=8; slotPosition=8;
minR=1.5; farcornerR=6; minR=1.5; farcornerR=6;
internalR=3; internalR=3;
points=[[0,0,farcornerR],[0,height,minR],[slotPosition,height,minR],[slotPosition,height-slotH,internalR], points=[
[slotPosition+slotW,height-slotH,internalR],[slotPosition+slotW,height,minR],[width,height,minR],[width,0,minR]]; [0, 0, farcornerR],
points2=[[0,0,farcornerR],["l",height,minR],[slotPosition,"l",minR],["l",height-slotH,internalR], [0, height, minR],
[slotPosition+slotW,"l",internalR],["l",height,minR],[width,"l",minR],["l",height*0.2,minR],[45,0,minR+5,"ayra"]];//,["l",0,minR]]; [slotPosition, height, minR],
[slotPosition, height-slotH, internalR],
[slotPosition+slotW, height-slotH, internalR],
[slotPosition+slotW, height, minR],
[width, height, minR],
[width, 0, minR]
];
points2=[
[0, 0, farcornerR],
["l", height, minR],
[slotPosition, "l", minR],
["l", height-slotH, internalR],
[slotPosition+slotW, "l", internalR],
["l", height, minR],
[width, "l", minR],
["l", height*0.2, minR],
[45, 0, minR+5, "ayra"]
];//,["l",0,minR]];
echo(processRadiiPoints(points2)); echo(processRadiiPoints(points2));
translate([-25,0,0])polygon(polyRound(points,5)); translate([-25,0,0]){
%translate([-25,0,0.2])polygon(getpoints(points));//transparent copy of the polgon without rounding polygon(polyRound(points,5));
translate([-50,0,0])polygon(polyRound(points2,5)); }
%translate([-50,0,0.2])polygon(getpoints(processRadiiPoints(points2)));//transparent copy of the polgon without rounding %translate([-25,0,0.2]){
polygon(getpoints(points));//transparent copy of the polgon without rounding
}
translate([-50,0,0]){
polygon(polyRound(points2,5));
}
%translate([-50,0,0.2]){
polygon(getpoints(processRadiiPoints(points2)));//transparent copy of the polgon without rounding
}
//Example of features 2 //Example of features 2
// 1 2 3 4 5 6 // 1 2 3 4 5 6
b=[[-4,0,1],[5,3,1.5],[0,7,0.1],[8,7,10],[20,20,0.8],[10,0,10]]; //points b=[[-4,0,1],[5,3,1.5],[0,7,0.1],[8,7,10],[20,20,0.8],[10,0,10]]; //points
@@ -34,44 +59,65 @@ module examples(){
// 1 2 3 4 5 6 // 1 2 3 4 5 6
p=[[0,0,1.2],[0,20,1],[15,15,1],[3,10,3],[15,0,1],[6,2,10]];//points p=[[0,0,1.2],[0,20,1],[15,15,1],[3,10,3],[15,0,1],[6,2,10]];//points
a=polyRound(p,5); a=polyRound(p,5);
translate([25,0,0])polygon(a); translate([25,0,0]){
%translate([25,0,0.2])polygon(getpoints(p));//transparent copy of the polgon without rounding polygon(a);
}
%translate([25,0,0.2]){
polygon(getpoints(p));//transparent copy of the polgon without rounding
}
//example of radii conflict handling and debuging feature //example of radii conflict handling and debuging feature
r1a=10; r1b=10; r1a=10; r1b=10;
r2a=30; r2b=30; r2a=30; r2b=30;
r3a=10; r3b=40; r3a=10; r3b=40;
r4a=15; r4b=20; r4a=15; r4b=20;
c1=[[0,0,0],[0,20,r1a],[20,20,r1b],[20,0,0]];//both radii fit and don't need to be changed c1=[[0,0,0],[0,20,r1a],[20,20,r1b],[20,0,0]];//both radii fit and don't need to be changed
translate([-25,-30,0])polygon(polyRound(c1,8)); translate([-25,-30,0]){
polygon(polyRound(c1,8));
}
echo(str("c1 debug= ",polyRound(c1,8,mode=1)," all zeros indicates none of the radii were reduced")); echo(str("c1 debug= ",polyRound(c1,8,mode=1)," all zeros indicates none of the radii were reduced"));
c2=[[0,0,0],[0,20,r2a],[20,20,r2b],[20,0,0]];//radii are too large and are reduced to fit c2=[[0,0,0],[0,20,r2a],[20,20,r2b],[20,0,0]];//radii are too large and are reduced to fit
translate([0,-30,0])polygon(polyRound(c2,8)); translate([0,-30,0]){
polygon(polyRound(c2,8));
}
echo(str("c2 debug= ",polyRound(c2,8,mode=1)," 2nd and 3rd radii reduced by 20mm i.e. from 30 to 10mm radius")); echo(str("c2 debug= ",polyRound(c2,8,mode=1)," 2nd and 3rd radii reduced by 20mm i.e. from 30 to 10mm radius"));
c3=[[0,0,0],[0,20,r3a],[20,20,r3b],[20,0,0]];//radii are too large again and are reduced to fit, but keep their ratios c3=[[0,0,0],[0,20,r3a],[20,20,r3b],[20,0,0]];//radii are too large again and are reduced to fit, but keep their ratios
translate([25,-30,0])polygon(polyRound(c3,8)); translate([25,-30,0]){
polygon(polyRound(c3,8));
}
echo(str("c3 debug= ",polyRound(c3,8,mode=1)," 2nd and 3rd radii reduced by 6 and 24mm respectively")); echo(str("c3 debug= ",polyRound(c3,8,mode=1)," 2nd and 3rd radii reduced by 6 and 24mm respectively"));
//resulting in radii of 4 and 16mm, //resulting in radii of 4 and 16mm,
//notice the ratio from the orginal radii stays the same r3a/r3b = 10/40 = 4/16 //notice the ratio from the orginal radii stays the same r3a/r3b = 10/40 = 4/16
c4=[[0,0,0],[0,20,r4a],[20,20,r4b],[20,0,0]];//radii are too large again but not corrected this time c4=[[0,0,0],[0,20,r4a],[20,20,r4b],[20,0,0]];//radii are too large again but not corrected this time
translate([50,-30,0])polygon(polyRound(c4,8,mode=2));//mode 2 = no radii limiting translate([50,-30,0]){
polygon(polyRound(c4,8,mode=2));//mode 2 = no radii limiting
}
//example of rounding random points, this has no current use but is a good demonstration //example of rounding random points, this has no current use but is a good demonstration
random=[for(i=[0:20])[rnd(0,50),rnd(0,50),/*rnd(0,30)*/1000]]; random=[for(i=[0:20])[rnd(0,50),rnd(0,50),/*rnd(0,30)*/1000]];
R =polyRound(random,7); R =polyRound(random,7);
translate([-25,25,0])polyline(R); translate([-25,25,0]){
polyline(R);
}
//example of different modes of the CentreN2PointsArc() function 0=shortest arc, 1=longest arc, 2=CW, 3=CCW //example of different modes of the CentreN2PointsArc() function 0=shortest arc, 1=longest arc, 2=CW, 3=CCW
p1=[0,5];p2=[10,5];centre=[5,0]; p1=[0,5];p2=[10,5];centre=[5,0];
translate([60,0,0]){ translate([60,0,0]){
color("green")polygon(CentreN2PointsArc(p1,p2,centre,0,20));//draws the shortest arc color("green"){
color("cyan")polygon(CentreN2PointsArc(p1,p2,centre,1,20));//draws the longest arc polygon(CentreN2PointsArc(p1,p2,centre,0,20));//draws the shortest arc
}
color("cyan"){
polygon(CentreN2PointsArc(p1,p2,centre,1,20));//draws the longest arc
}
} }
translate([75,0,0]){ translate([75,0,0]){
color("purple")polygon(CentreN2PointsArc(p1,p2,centre,2,20));//draws the arc CW (which happens to be the short arc) color("purple"){
color("red")polygon(CentreN2PointsArc(p2,p1,centre,2,20));//draws the arc CW but p1 and p2 swapped order resulting in the long arc being drawn polygon(CentreN2PointsArc(p1,p2,centre,2,20));//draws the arc CW (which happens to be the short arc)
}
color("red"){
polygon(CentreN2PointsArc(p2,p1,centre,2,20));//draws the arc CW but p1 and p2 swapped order resulting in the long arc being drawn
}
} }
radius=6; radius=6;
@@ -146,10 +192,10 @@ module examples(){
translate([-30,-90,0]) translate([-30,-90,0])
shell2d(-0.5,0,0)polygon(polyRound(b,30)); shell2d(-0.5,0,0)polygon(polyRound(b,30));
} }
{function polyRound(radiipoints,fn=5,mode=0)=
/* Takes a list of radii points of the format [x,y,radius] and rounds each point function polyRound(radiipoints,fn=5,mode=0)=
/*Takes a list of radii points of the format [x,y,radius] and rounds each point
with fn resolution with fn resolution
mode=0 - automatic radius limiting - DEFAULT mode=0 - automatic radius limiting - DEFAULT
mode=1 - Debug, output radius reduction for automatic radius limiting mode=1 - Debug, output radius reduction for automatic radius limiting
@@ -159,19 +205,24 @@ module examples(){
p=getpoints(radiipoints), //make list of coordinates without radii p=getpoints(radiipoints), //make list of coordinates without radii
Lp=len(p), Lp=len(p),
//remove the middle point of any three colinear points //remove the middle point of any three colinear points
newrp=[for(i=[0:len(p)-1]) if(isColinear(p[wrap(i-1,Lp)],p[wrap(i+0,Lp)],p[wrap(i+1,Lp)])==0||p[wrap(i+0,Lp)].z!=0)radiipoints[wrap(i+0,Lp)] ], newrp=[
for(i=[0:len(p)-1]) if(isColinear(p[wrap(i-1,Lp)],p[wrap(i+0,Lp)],p[wrap(i+1,Lp)])==0||p[wrap(i+0,Lp)].z!=0)radiipoints[wrap(i+0,Lp)]
],
newrp2=processRadiiPoints(newrp), newrp2=processRadiiPoints(newrp),
temp=[for(i=[0:len(newrp2)-1]) //for each point in the radii array temp=[
for(i=[0:len(newrp2)-1]) //for each point in the radii array
let( let(
thepoints=[for(j=[-getpoints:getpoints])newrp2[wrap(i+j,len(newrp2))]],//collect 5 radii points thepoints=[for(j=[-getpoints:getpoints])newrp2[wrap(i+j,len(newrp2))]],//collect 5 radii points
temp2=mode==2?round3points(thepoints,fn):round5points(thepoints,fn,mode) temp2=mode==2?round3points(thepoints,fn):round5points(thepoints,fn,mode)
) )
mode==1?temp2:newrp2[i][2]==0?[[newrp2[i][0],newrp2[i][1]]]: //return the original point if the radius is 0 mode==1?temp2:newrp2[i][2]==0?
[[newrp2[i][0],newrp2[i][1]]]: //return the original point if the radius is 0
CentreN2PointsArc(temp2[0],temp2[1],temp2[2],0,fn) //return the arc if everything is normal CentreN2PointsArc(temp2[0],temp2[1],temp2[2],0,fn) //return the arc if everything is normal
] ]
) )
[for (a = temp) for (b = a) b];}//flattern and return the array [for (a = temp) for (b = a) b];//flattern and return the array
{function round5points(rp,fn,debug=0)=
function round5points(rp,fn,debug=0)=
rp[2][2]==0&&debug==0?[[rp[2][0],rp[2][1]]]://return the middle point if the radius is 0 rp[2][2]==0&&debug==0?[[rp[2][0],rp[2][1]]]://return the middle point if the radius is 0
rp[2][2]==0&&debug==1?0://if debug is enabled and the radius is 0 return 0 rp[2][2]==0&&debug==1?0://if debug is enabled and the radius is 0 return 0
let( let(
@@ -207,8 +258,9 @@ module examples(){
//determine the direction of rotation //determine the direction of rotation
debug==1?//if debug in disabled return arc (default) debug==1?//if debug in disabled return arc (default)
(newR-r[1]): (newR-r[1]):
[t23,t34,cen];} [t23,t34,cen];
{function round3points(rp,fn)=
function round3points(rp,fn)=
rp[1][2]==0?[[rp[1][0],rp[1][1]]]://return the middle point if the radius is 0 rp[1][2]==0?[[rp[1][0],rp[1][1]]]://return the middle point if the radius is 0
let( let(
p=getpoints(rp), //get list of points p=getpoints(rp), //get list of points
@@ -228,8 +280,9 @@ module examples(){
angCen=getAngle(tmid,p[1]),//angle from point 2 to circle centre angCen=getAngle(tmid,p[1]),//angle from point 2 to circle centre
cen=[p[1][0]-cos(angCen)*circD,p[1][1]-sin(angCen)*circD] //circle center by offseting from point 2 cen=[p[1][0]-cos(angCen)*circD,p[1][1]-sin(angCen)*circD] //circle center by offseting from point 2
) )
[t12,t23,cen];} [t12,t23,cen];
{function parallelFollow(rp,thick=4,minR=1,mode=1)=
function parallelFollow(rp,thick=4,minR=1,mode=1)=
//rp[1][2]==0?[rp[1][0],rp[1][1],0]://return the middle point if the radius is 0 //rp[1][2]==0?[rp[1][0],rp[1][1],0]://return the middle point if the radius is 0
thick==0?[rp[1][0],rp[1][1],0]://return the middle point if the radius is 0 thick==0?[rp[1][0],rp[1][1],0]://return the middle point if the radius is 0
let( let(
@@ -252,17 +305,20 @@ module examples(){
cen=[p[1][0]-cos(angCen)*circD,p[1][1]-sin(angCen)*circD],//circle center by offseting from point 2 cen=[p[1][0]-cos(angCen)*circD,p[1][1]-sin(angCen)*circD],//circle center by offseting from point 2
outR=max(minR,rp[1][2]-thick*sgn*mode) //ensures radii are never too small. outR=max(minR,rp[1][2]-thick*sgn*mode) //ensures radii are never too small.
) )
concat(cen,outR);} concat(cen,outR);
{function findPoint(ang1,refpoint1,ang2,refpoint2,r=0)=
function findPoint(ang1,refpoint1,ang2,refpoint2,r=0)=
let( let(
m1=tan(ang1),c1=refpoint1.y-m1*refpoint1.x, m1=tan(ang1),
m2=tan(ang2),c2=refpoint2.y-m2*refpoint2.x, c1=refpoint1.y-m1*refpoint1.x,
m2=tan(ang2),
c2=refpoint2.y-m2*refpoint2.x,
outputX=(c2-c1)/(m1-m2), outputX=(c2-c1)/(m1-m2),
outputY=m1*outputX+c1 outputY=m1*outputX+c1
) )
[outputX,outputY,r]; [outputX,outputY,r];
}
{function RailCustomiser(rp,o1=0,o2,mode=0,minR=0,a1,a2)= function RailCustomiser(rp,o1=0,o2,mode=0,minR=0,a1,a2)=
/*This function takes a series of radii points and plots points to run along side at a constanit distance, think of it as offset but for line instead of a polygon /*This function takes a series of radii points and plots points to run along side at a constanit distance, think of it as offset but for line instead of a polygon
rp=radii points, o1&o2=offset 1&2,minR=min radius, a1&2=angle 1&2 rp=radii points, o1&o2=offset 1&2,minR=min radius, a1&2=angle 1&2
mode=1 - include endpoints a1&2 are relative to the angle of the last two points and equal 90deg if not defined mode=1 - include endpoints a1&2 are relative to the angle of the last two points and equal 90deg if not defined
@@ -298,16 +354,16 @@ module examples(){
OffLn2=[for(i=[0:Lrp3]) o2==0?rp[i+1]:parallelFollow([rp[i],rp[i+1],rp[i+2]],o2b,minR,mode=CWorCCW2)], OffLn2=[for(i=[0:Lrp3]) o2==0?rp[i+1]:parallelFollow([rp[i],rp[i+1],rp[i+2]],o2b,minR,mode=CWorCCW2)],
Rp1=abs(rp[0].z), Rp1=abs(rp[0].z),
Rp2=abs(rp[Lrp-1].z), Rp2=abs(rp[Lrp-1].z),
endP1a=findPoint(getAngle(rp[0],rp[1]),OffLn1[0],a1,rp[0],Rp1), endP1a=findPoint(getAngle(rp[0],rp[1]), OffLn1[0], a1,rp[0], Rp1),
endP1b=findPoint(getAngle(rp[Lrp-1],rp[Lrp-2]),OffLn1[len(OffLn1)-1],a2,rp[Lrp-1],Rp2), endP1b=findPoint(getAngle(rp[Lrp-1],rp[Lrp-2]), OffLn1[len(OffLn1)-1], a2,rp[Lrp-1], Rp2),
endP2a=findPoint(getAngle(rp[0],rp[1]),OffLn2[0],a1,rp[0],Rp1), endP2a=findPoint(getAngle(rp[0],rp[1]), OffLn2[0], a1,rp[0], Rp1),
endP2b=findPoint(getAngle(rp[Lrp-1],rp[Lrp-2]),OffLn2[len(OffLn1)-1],a2,rp[Lrp-1],Rp2), endP2b=findPoint(getAngle(rp[Lrp-1],rp[Lrp-2]), OffLn2[len(OffLn1)-1], a2,rp[Lrp-1], Rp2),
absEnda=getAngle(endP1a,endP2a), absEnda=getAngle(endP1a,endP2a),
absEndb=getAngle(endP1b,endP2b), absEndb=getAngle(endP1b,endP2b),
negRP1a=[cos(absEnda)*rp[0].z*10+endP1a.x,sin(absEnda)*rp[0].z*10+endP1a.y,0.0], negRP1a=[cos(absEnda)*rp[0].z*10+endP1a.x, sin(absEnda)*rp[0].z*10+endP1a.y, 0.0],
negRP2a=[cos(absEnda)*-rp[0].z*10+endP2a.x,sin(absEnda)*-rp[0].z*10+endP2a.y,0.0], negRP2a=[cos(absEnda)*-rp[0].z*10+endP2a.x, sin(absEnda)*-rp[0].z*10+endP2a.y, 0.0],
negRP1b=[cos(absEndb)*rp[Lrp-1].z*10+endP1b.x,sin(absEndb)*rp[Lrp-1].z*10+endP1b.y,0.0], negRP1b=[cos(absEndb)*rp[Lrp-1].z*10+endP1b.x, sin(absEndb)*rp[Lrp-1].z*10+endP1b.y, 0.0],
negRP2b=[cos(absEndb)*-rp[Lrp-1].z*10+endP2b.x,sin(absEndb)*-rp[Lrp-1].z*10+endP2b.y,0.0], negRP2b=[cos(absEndb)*-rp[Lrp-1].z*10+endP2b.x, sin(absEndb)*-rp[Lrp-1].z*10+endP2b.y, 0.0],
OffLn1b=(mode==0||mode==2)&&rp[0].z<0&&rp[Lrp-1].z<0? OffLn1b=(mode==0||mode==2)&&rp[0].z<0&&rp[Lrp-1].z<0?
concat([negRP1a],[endP1a],OffLn1,[endP1b],[negRP1b]) concat([negRP1a],[endP1a],OffLn1,[endP1b],[negRP1b])
:(mode==0||mode==2)&&rp[0].z<0? :(mode==0||mode==2)&&rp[0].z<0?
@@ -317,8 +373,7 @@ module examples(){
:mode==0||mode==2? :mode==0||mode==2?
concat([endP1a],OffLn1,[endP1b]) concat([endP1a],OffLn1,[endP1b])
: :
OffLn1 OffLn1,
,
OffLn2b=(mode==0||mode==2)&&rp[0].z<0&&rp[Lrp-1].z<0? OffLn2b=(mode==0||mode==2)&&rp[0].z<0&&rp[Lrp-1].z<0?
concat([negRP2a],[endP2a],OffLn2,[endP2b],[negRP2b]) concat([negRP2a],[endP2a],OffLn2,[endP2b],[negRP2b])
:(mode==0||mode==2)&&rp[0].z<0? :(mode==0||mode==2)&&rp[0].z<0?
@@ -330,18 +385,22 @@ module examples(){
: :
OffLn2 OffLn2
)//end of let() )//end of let()
o2undef==1?OffLn1b:concat(OffLn2b,revList(OffLn1b));} o2undef==1?OffLn1b:concat(OffLn2b,revList(OffLn1b));
{function revList(list)=//reverse list
function revList(list)=//reverse list
let(Llist=len(list)-1) let(Llist=len(list)-1)
[for(i=[0:Llist]) list[Llist-i]]; [for(i=[0:Llist]) list[Llist-i]];
}
{function CWorCCW(p)= function CWorCCW(p)=
let( let(
Lp=len(p), Lp=len(p),
e=[for(i=[0:Lp-1]) (p[wrap(i+0,Lp)].x-p[wrap(i+1,Lp)].x)*(p[wrap(i+0,Lp)].y+p[wrap(i+1,Lp)].y)] e=[for(i=[0:Lp-1])
(p[wrap(i+0,Lp)].x-p[wrap(i+1,Lp)].x)*(p[wrap(i+0,Lp)].y+p[wrap(i+1,Lp)].y)
]
) )
sign(sum(e));} sign(sum(e));
{function CentreN2PointsArc(p1,p2,cen,mode=0,fn)=
function CentreN2PointsArc(p1,p2,cen,mode=0,fn)=
/* This function plots an arc from p1 to p2 with fn increments using the cen as the centre of the arc. /* This function plots an arc from p1 to p2 with fn increments using the cen as the centre of the arc.
the mode determines how the arc is plotted the mode determines how the arc is plotted
mode==0, shortest arc possible mode==0, shortest arc possible
@@ -365,58 +424,108 @@ module examples(){
r=pointDist(p1,cen),//determine the radius r=pointDist(p1,cen),//determine the radius
p1Angle=getAngle(cen,p1) //angle of line 1 p1Angle=getAngle(cen,p1) //angle of line 1
) )
[for(i=[0:fn]) [cos(p1Angle+(arcAngle/fn)*i*CWorCCW)*r+cen[0],sin(p1Angle+(arcAngle/fn)*i*CWorCCW)*r+cen[1]]];} [for(i=[0:fn]) [cos(p1Angle+(arcAngle/fn)*i*CWorCCW)*r+cen[0],sin(p1Angle+(arcAngle/fn)*i*CWorCCW)*r+cen[1]]];
{function moveRadiiPoints(rp,tran=[0,0],rot=0)=
function moveRadiiPoints(rp,tran=[0,0],rot=0)=
[for(i=rp) [for(i=rp)
let( let(
a=getAngle([0,0],[i.x,i.y]),//get the angle of the this point a=getAngle([0,0],[i.x,i.y]),//get the angle of the this point
h=pointDist([0,0],[i.x,i.y]) //get the hypotenuse/radius h=pointDist([0,0],[i.x,i.y]) //get the hypotenuse/radius
) )
[h*cos(a+rot)+tran.x,h*sin(a+rot)+tran.y,i.z]//calculate the point's new position [h*cos(a+rot)+tran.x,h*sin(a+rot)+tran.y,i.z]//calculate the point's new position
];} ];
module round2d(OR=3,IR=1){ module round2d(OR=3,IR=1){
offset(OR)offset(-IR-OR)offset(IR)children(); offset(OR){
offset(-IR-OR){
offset(IR){
children();
}
}
}
} }
module shell2d(o1,OR=0,IR=0,o2=0){ module shell2d(o1,OR=0,IR=0,o2=0){
difference(){ difference(){
round2d(OR,IR)offset(max(o1,o2))children(0);//original 1st child forms the outside of the shell round2d(OR,IR){
round2d(IR,OR)difference(){//round the inside cutout offset(max(o1,o2)){
offset(min(o1,o2))children(0);//shrink the 1st child to form the inside of the shell children(0);//original 1st child forms the outside of the shell
if($children>1)for(i=[1:$children-1])children(i);//second child and onwards is used to add material to inside of the shell }
}
round2d(IR,OR){
difference(){//round the inside cutout
offset(min(o1,o2)){
children(0);//shrink the 1st child to form the inside of the shell
}
if($children>1){
for(i=[1:$children-1]){
children(i);//second child and onwards is used to add material to inside of the shell
}
}
}
} }
} }
} }
module internalSq(size,r,center=0){ module internalSq(size,r,center=0){
tran=center==1?[0,0]:size/2; tran=center==1?[0,0]:size/2;
translate(tran){ translate(tran){
square(size,true); square(size,true);
offs=sin(45)*r; offs=sin(45)*r;
for(i=[-1,1],j=[-1,1])translate([(size.x/2-offs)*i,(size.y/2-offs)*j])circle(r); for(i=[-1,1],j=[-1,1]){
translate([(size.x/2-offs)*i,(size.y/2-offs)*j])circle(r);
}
} }
} }
module r_extrude(ln,r1=0,r2=0,fn=30){ module r_extrude(ln,r1=0,r2=0,fn=30){
n1=sign(r1);n2=sign(r2); n1=sign(r1);n2=sign(r2);
r1=abs(r1);r2=abs(r2); r1=abs(r1);r2=abs(r2);
translate([0,0,r1])linear_extrude(ln-r1-r2)children(); translate([0,0,r1]){
linear_extrude(ln-r1-r2){
children();
}
}
for(i=[0:1/fn:1]){ for(i=[0:1/fn:1]){
translate([0,0,i*r1])linear_extrude(r1/fn)offset(n1*sqrt(sq(r1)-sq(r1-i*r1))-n1*r1)children(); translate([0,0,i*r1]){
translate([0,0,ln-r2+i*r2])linear_extrude(r2/fn)offset(n2*sqrt(sq(r2)-sq(i*r2))-n2*r2)children(); linear_extrude(r1/fn){
offset(n1*sqrt(sq(r1)-sq(r1-i*r1))-n1*r1){
children();
}
}
}
translate([0,0,ln-r2+i*r2]){
linear_extrude(r2/fn){
offset(n2*sqrt(sq(r2)-sq(i*r2))-n2*r2){
children();
}
}
}
} }
} }
{function mirrorPoints(b,rot=0,atten=[0,0])= //mirrors a list of points about Y, ignoring the first and last points and returning them in reverse order for use with polygon or polyRound
function mirrorPoints(b,rot=0,atten=[0,0])= //mirrors a list of points about Y, ignoring the first and last points and returning them in reverse order for use with polygon or polyRound
let( let(
a=moveRadiiPoints(b,[0,0],-rot), a=moveRadiiPoints(b,[0,0],-rot),
temp3=[for(i=[0+atten[0]:len(a)-1-atten[1]]) [a[i][0],-a[i][1],a[i][2]]], temp3=[for(i=[0+atten[0]:len(a)-1-atten[1]])
[a[i][0],-a[i][1],a[i][2]]
],
temp=moveRadiiPoints(temp3,[0,0],rot), temp=moveRadiiPoints(temp3,[0,0],rot),
temp2=revList(temp3) temp2=revList(temp3)
) )
concat(b,temp2);} concat(b,temp2);
function processRadiiPoints(rp)= function processRadiiPoints(rp)=
[for(i=[0:len(rp)-1]) processRadiiPoints2(rp,i)]; [for(i=[0:len(rp)-1])
processRadiiPoints2(rp,i)
];
function processRadiiPoints2(list,end=0,idx=0,result=0)= function processRadiiPoints2(list,end=0,idx=0,result=0)=
idx>=end+1?result:processRadiiPoints2(list,end,idx+1,relationalRadiiPoints(result,list[idx])); idx>=end+1?result:
processRadiiPoints2(list,end,idx+1,relationalRadiiPoints(result,list[idx]));
function cosineRuleBside(a,c,C)=c*cos(C)-sqrt(sq(a)+sq(c)+sq(cos(C))-sq(c)); function cosineRuleBside(a,c,C)=c*cos(C)-sqrt(sq(a)+sq(c)+sq(cos(C))-sq(c));
function absArelR(po,pn)= function absArelR(po,pn)=
let( let(
th2=atan(po[1]/po[0]), th2=atan(po[1]/po[0]),
@@ -424,6 +533,7 @@ function absArelR(po,pn)=
r3=cosineRuleBside(r2,pn[1],th2-pn[0]) r3=cosineRuleBside(r2,pn[1],th2-pn[0])
) )
[cos(pn[0])*r3,sin(pn[0])*r3,pn[2]]; [cos(pn[0])*r3,sin(pn[0])*r3,pn[2]];
function relationalRadiiPoints(po,pi)= function relationalRadiiPoints(po,pi)=
let( let(
p0=pi[0], p0=pi[0],
@@ -500,37 +610,54 @@ function relationalRadiiPoints(po,pi)=
:[po[0]+pn[0],pn[1],pn[2]]//rel x, abs y :[po[0]+pn[0],pn[1],pn[2]]//rel x, abs y
) )
temp; temp;
{function invtan(run,rise)=
function invtan(run,rise)=
let(a=abs(atan(rise/run))) let(a=abs(atan(rise/run)))
rise==0&&run>0?0:rise>0&&run>0?a:rise>0&&run==0?90:rise>0&&run<0?180-a:rise==0&&run<0?180:rise<0&&run<0?a+180:rise<0&&run==0?270:rise<0&&run>0?360-a:"error";} rise==0&&run>0?
{function cosineRuleAngle(p1,p2,p3)= 0:rise>0&&run>0?
a:rise>0&&run==0?
90:rise>0&&run<0?
180-a:rise==0&&run<0?
180:rise<0&&run<0?
a+180:rise<0&&run==0?
270:rise<0&&run>0?
360-a:"error";
function cosineRuleAngle(p1,p2,p3)=
let( let(
p12=abs(pointDist(p1,p2)), p12=abs(pointDist(p1,p2)),
p13=abs(pointDist(p1,p3)), p13=abs(pointDist(p1,p3)),
p23=abs(pointDist(p2,p3)) p23=abs(pointDist(p2,p3))
) )
acos((sq(p23)+sq(p12)-sq(p13))/(2*p23*p12));} acos((sq(p23)+sq(p12)-sq(p13))/(2*p23*p12));
{function sum(list, idx = 0, result = 0) =
idx >= len(list) ? result : sum(list, idx + 1, result + list[idx]);} function sum(list, idx = 0, result = 0) =
idx >= len(list) ? result : sum(list, idx + 1, result + list[idx]);
function sq(x)=x*x; function sq(x)=x*x;
function getGradient(p1,p2)=(p2.y-p1.y)/(p2.x-p1.x); function getGradient(p1,p2)=(p2.y-p1.y)/(p2.x-p1.x);
function getAngle(p1,p2)=p1==p2?0:invtan(p2[0]-p1[0],p2[1]-p1[1]); function getAngle(p1,p2)=p1==p2?0:invtan(p2[0]-p1[0],p2[1]-p1[1]);
function getMidpoint(p1,p2)=[(p1[0]+p2[0])/2,(p1[1]+p2[1])/2]; //returns the midpoint of two points function getMidpoint(p1,p2)=[(p1[0]+p2[0])/2,(p1[1]+p2[1])/2]; //returns the midpoint of two points
function pointDist(p1,p2)=sqrt(abs(sq(p1[0]-p2[0])+sq(p1[1]-p2[1]))); //returns the distance between two points function pointDist(p1,p2)=sqrt(abs(sq(p1[0]-p2[0])+sq(p1[1]-p2[1]))); //returns the distance between two points
function isColinear(p1,p2,p3)=getGradient(p1,p2)==getGradient(p2,p3)?1:0;//return 1 if 3 points are colinear function isColinear(p1,p2,p3)=getGradient(p1,p2)==getGradient(p2,p3)?1:0;//return 1 if 3 points are colinear
module polyline(p) {for(i=[0:max(0,len(p)-1)])line(p[i],p[wrap(i+1,len(p) )]); module polyline(p) {
for(i=[0:max(0,len(p)-1)]){
line(p[i],p[wrap(i+1,len(p) )]);
}
} // polyline plotter } // polyline plotter
module line(p1, p2 ,width=0.3) { // single line plotter module line(p1, p2 ,width=0.3) { // single line plotter
hull() { hull() {
translate(p1) circle(width); translate(p1){
translate(p2) circle(width); circle(width);
}
translate(p2){
circle(width);
}
} }
} }
function getpoints(p)=[for(i=[0:len(p)-1])[p[i].x,p[i].y]];// gets [x,y]list of[x,y,r]list function getpoints(p)=[for(i=[0:len(p)-1])[p[i].x,p[i].y]];// gets [x,y]list of[x,y,r]list
function wrap(x,x_max=1,x_min=0) = (((x - x_min) % (x_max - x_min)) + (x_max - x_min)) % (x_max - x_min) + x_min; // wraps numbers inside boundaries function wrap(x,x_max=1,x_min=0) = (((x - x_min) % (x_max - x_min)) + (x_max - x_min)) % (x_max - x_min) + x_min; // wraps numbers inside boundaries
{function rnd(a = 1, b = 0, s = []) = function rnd(a = 1, b = 0, s = []) =
s == [] ? s == [] ?
(rands(min(a, b), max( a, b), 1)[0]) (rands(min(a, b), max( a, b), 1)[0]):(rands(min(a, b), max(a, b), 1, s)[0]); // nice rands wrapper
:
(rands(min(a, b), max(a, b), 1, s)[0])
;} // nice rands wrapper