Showing posts with label cube. Show all posts
Showing posts with label cube. Show all posts

Friday, August 4, 2017

Points inside a cone

Generates a list of random points that lie inside a cone

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

Monday, June 26, 2017

Area Of A Cuboid By Sides



function Area_Of_A_Cuboid_By_Sides(A,B,C)=2*(A*B+A*C+C*B);



 

Tuesday, June 20, 2017

Points inside a cube

Generates a list of random points that lie inside a cuboid.

function random_points_inside_cuboid(p1,p2,n)=
    [for(i=[1:n])cuboid_point(p1,p2)];

function cuboid_point(p1,p2)= 
  [rands(-p1.x,p2.x,1)[0],
   rands(-p1.y,p2.y,1)[0],
   rands(-p1.z,p2.z,1)[0]] ;

 


p1=[0,0,0];
p2=[100,100,100];
p=random_points_inside_cuboid(p1,p2,1000);
for(t=p)translate(t)sphere(4);
echo(p);
 

Monday, June 19, 2017

Stone wall brick offset calculations

(Layers mod 2) equals zero at even layers 
and one for uneven layers.
wikipedia.org/wiki/Modulo_operation
Challenge: What other brick patterns can you make?



brick_length=5;
half_brick_length=brick_length/2;
brick_width=3;
brick_height=2;

for(layer=[0:1:5])
for(run=[0:1:5]){
shift= layer % 2; // <-- this is the sauce
translate(
[run*brick_length+shift*half_brick_length,
0,
layer*brick_height])
brick();
}

module brick(){color(rands(0,1,3)) 
cube([brick_length,brick_width,brick_height]);}
 

Thursday, June 1, 2017

1 dimensional noise

Singel dimension noise layered in three octaves.
Return value is 0-1.

Usage:



seed=rnd(12);
for (a=[-15:0.1:15])  translate([a,0,0])  cube([0.1,0.1, noise_1d(a+seed)]);

function  noise_1d(i,
low_range=1/4,mid_range=1,hi_range=8,
low_magnitude=1/2,mid_magnitude=0.25,hi_magnitude=0.5)=
 (  
single_noise_1d(i/low_range)*low_magnitude+
single_noise_1d(i/mid_range)*mid_magnitude+
single_noise_1d(i/hi_range)*hi_magnitude

)/(low_magnitude+mid_magnitude+hi_magnitude);


function single_noise_1d(i)=
let(
a=floor(i),
b=ceil(i),
c=smooth_curve(i-a),
arnd=rnd(1,0,a),
brnd=rnd(1,0,b),
out=lerp(arnd,brnd,c)
)
out
;

function smooth_curve(a) =let (b = clamp(a))(b * b * (3 - 2 * b));
function clamp(a, b = 0, c = 1) = min(max(a, b), c);
function lerp(start, end, bias) = (end * bias + start * (1 - bias));
function rnd(a = 1, b = 0, s = []) = 
  s == [] ? 
   (rands(min(a, b), max(   a, b), 1)[0]) 
  : 
   (rands(min(a, b), max(a, b), 1, s)[0])
  ; 


Friday, May 12, 2017

Trilinear interpolation

Interpolate a value between eight values, typically corners of a cube. Lerp
Usage:

my_value=trilinear_lerp( 9,12,3,34, 25,16,7,18, 0.1,0.6,0.5 );

function trilinear_lerp(
V000,V100,V010,V001,
V101,V011,V110,V111,
bias_x,bias_y,bias_z)=
lerp(
    lerp(
        lerp(V000,V001,bias_z),
        lerp(V010,V011,bias_z),
    bias_y
    ),
    lerp(
        lerp(V100,V101,bias_z),
        lerp(V110,V111,bias_z),
    bias_y
    )
    ,bias_x
);
function lerp(start, end, bias) = (end * bias + start * (1 - bias));

d