Usage:
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 Radius_A_Regular_Polygon_By_Side_And_N(A,N)= A/(2*sin/(180/N));
function Area_Of_A_Isosceles_Prism_By_Base_Heights(A,h,H)=
2*((1/2)*A*h)+ A*sqrt((a/2)*(a/2)+h*h)*H;
function Tennis_ball_Curve(i)=let(
b=360*i-sin(360*i*4)*25 ,
a=sin(360*i*2)*65)
[cos(a)*sin(b),cos(a)*cos(b),sin(a)] ;
for(i=[0:1/180:1]){
translate(Tennis_ball_Curve(i))sphere(0.02,$fn=30);
}
function L_Spiral (a,radius = 50,heigth,turns = 4,xpnt=0.685,start_angle=45)
= let( fx = xpnt<0?
pow(max(0,1-a),1/abs(min(-1,xpnt-1) )): pow(max(0,1-a),max(1,xpnt+1)) )
[(radius * fx ) * cos(start_angle+ a* turns*360*sign(turns)),
(radius * fx ) * sin(start_angle+ a* turns*360*sign(turns)),a*heigth];
for(j=[0:1/60:1]){hull(){
translate(L_Spiral (j, 50,-150, 4, 1.685, 0 ))sphere();
translate(L_Spiral (j+1/60, 50,-150, 4, 1.685, 0 ))sphere();
}}
function Area_Of_Two_Intersecting_Circles(P1, P2,P1r,P2r) =
let(p1 = P1r * P1r,p2 = P2r * P2r )
(PI*p1+PI*p2)-
(
let(d = max(1e-32,norm(P2 - P1)))
(d >= P1r + P2r) ?0:
d < abs(P2r - P1r)?PI * min(p1, p2):
let( x = (p1 - p2 + d * d) / (2 * d),
z = x * x, y = sqrt(p1 - z))
p1 * asin(y / P1r)/57.2958 + p2 *
asin(y / P2r)/57.2958 - y *
(x + sqrt(z + p2 - p1)) );
////////////////////////////////////////////////
p1=rands(0,40,2);
p2=rands(0,40,2);
r1=rands(1,20,1)[0];
r2=rands(1,20,1)[0];
echo(str(" Area 1 : ",PI*r1*r1," "));
echo(str(" Area 2 : ",PI*r2*r2," "));
echo(str(" Area ∑ : ",PI*r1*r1+PI*r2*r2) );
echo(str(" Area I : ",Area_Of_Two_Intersecting_Circles(p1,p2,r1,r2)));
linear_extrude(1) union(){
translate(p1)circle(r1,$fn=50);
translate(p2)circle(r2,$fn=50);
}
if( norm(p2 - p1)< r1 + r2 )color("red")linear_extrude(1.01)
intersection(){
translate(p1)circle(r1,$fn=50);
translate(p2)circle(r2,$fn=50);
}
function random_points_inside_ellipsoid(rx,ry,rz,n)=
[for(i=[1:n])ellipsoid_point(rx,ry,rz)];
function ellipsoid_point(rx,ry,rz)=
let (
r=[rx,ry,rz],
p=[
rands(-rx,rx,1)[0],
rands(-ry,ry,1)[0],
rands(-rz,rz,1)[0]
] ,
d= ((
sq2(p.x)/sq2(r.x)+
sq2(p.y)/sq2(r.y)+
sq2(p.z)/sq2(r.z)) - 1) * min(r.x,r.y,r.z) )
(d)>0?ellipsoid_point(rx,ry,rz):p;
function sq2(v)=pow(v,2);
p=random_points_inside_ellipsoid(50,50,100,1000);
for(t=p)translate(t)sphere(4);
echo(p);
Deck=["Ace of Spades","Two of Spades","Three of Spades",
"Four of Spades","Five of Spades","Six of Spades","Seven of Spades",
"Eight of Spades","Nine of Spades","Ten of Spades","Jack of Spades",
"Queen of Spades","King of Spades","Ace of Hearts","Two of Hearts",
"Three of Hearts","Four of Hearts","Five of Hearts","Six of Hearts",
"Seven of Hearts","Eight of Hearts","Nine of Hearts","Ten of Hearts",
"Jack of Hearts","Queen of Hearts","King of Hearts","Ace of Clubs",
"Two of Clubs","Three of Clubs","Four of Clubs","Five of Clubs",
"Six of Clubs","Seven of Clubs","Eight of Clubs","Nine of Clubs",
"Ten of Clubs","Jack of Clubs","Queen of Clubs","King of Clubs",
"Ace of Diamonds","Two of Diamonds","Three of Diamonds",
"Four of Diamonds","Five of Diamonds","Six of Diamonds",
"Seven of Diamonds","Eight of Diamonds","Nine of Diamonds",
"Ten of Diamonds","Jack of Diamonds","Queen of Diamonds",
"King of Diamonds"];
echo( shuffle(Deck));
function shuffle(il,repeat=2)=len(il)<=1?il:
let(l=repeat>0?shuffle(il,repeat-1):il, p=split(l))
round(rands(0,2,1)[0])==0?
concat( shuffle(p[0]),shuffle(p[1])):
concat( shuffle(p[1]),shuffle(p[0]));
function split(l)=[
[for(i=[0:2:len(l)-1])l[i]],
[for(i=[1:2:len(l)-1])l[i]]] ;
echo(rfor (50,10,-10,[0,1]));
function rfor(i,to,stepsize=1,state)=
abs(i-to)<abs(stepsize)?/* are there space left to take next step */
state : /* if done return state or what ever value you like*/
let(newstate=myFunction(state,i ))
rfor(i+stepsize,to,stepsize,newstate) // call next recursive step
;
/*your code here, reassign state as you like */
function myFunction (state,i)= [state[0]+sqrt(i),state[1]];
function dice()=round(rands(1,2,1)[0])==1?false:true;
function Volume_Of_A_Cone_By_Radius_And_Height(r,h )=
PI*r*r*h*(1/3);
for(i=[-0:1/160:1])translate([i,0])square([1/160, (arc1( (i)))]);
function arc1 (x,n=0) = let(a= n>0? arc1(x,(n-1)):x) clamp(sqrt(1-(1-a)*(1-a)));
function clamp(a, b = 0, c = 1) = min(max(a, b), c);
function random_points_inside_cylinder(r,h,n)=
[for(i=[1:n])cylinder_point(r,h)];
function cylinder_point(r,h)=
let (p=[
rands(-r,r,1)[0],
rands(-r,r,1)[0],
rands(0,h,1)[0] ],
d=(pow(p.x,2) + pow(p.y,2) ))
d > pow(r,2)?cylinder_point(r,h):p;
p=random_points_inside_cylinder(50,100,1000);
for(t=p)translate(t)sphere(4);
echo(p);
function random_points_inside_tube(r,r2,h,n)=
[for(i=[1:n])tube_point(r,r2,h)];
function tube_point(r,r2,h)=
let (p=[
rands(-r,r,1)[0],
rands(-r,r,1)[0],
rands(0,h,1)[0] ],
d=(pow(p.x,2) + pow(p.y,2) ))
d > pow(r,2)||d < pow(r2,2)?tube_point(r,r2,h):p;
p=random_points_inside_tube(50,30,100,1000);
for(t=p)translate(t) sphere(4);
echo(p);
function random_points_inside_cone(r,h,n)=
[for(i=[1:n])cone_point(r,h)];
function cone_point(r,h)=
let (p=[
rands(-r,r,1)[0],
rands(-r,r,1)[0],
rands(0,h,1)[0] ],
d=sqrt(pow(p.x,2) + pow(p.y,2) )
)
d > r-r*(p.z/h)?cone_point(r,h):p;
p=random_points_inside_cone(50,150,2000);
for(t=p)translate(t)rotate(rands(0,360,3))cube(5,center=true);
echo(p);
function Volume_Of_A_Cuboid_By_Sides(A,B,C)=A*B*C;