Showing posts with label rotate. Show all posts
Showing posts with label rotate. Show all posts

Tuesday, May 30, 2017

LookAt function and module

Rotate an object towards a waypoint.
Uses angle+vector format of openSCADs native rotate operand.

Usage:

Lookatpoint=[rnd(-20,20),rnd(-20,20),rnd(-20,20)];
Origin=[rnd(-20,20),rnd(-20,20),rnd(-20,20)];
translate(Lookatpoint)color("red")sphere(2);
translate(Origin)color("blue")sphere(2);
translate(Origin)look_at(Lookatpoint ,Origin ){
translate([0,0,5])cylinder(7,2,0);
translate([0,0,-3])cylinder(9,1,1);
}
module look_at(lookpoint,origin=[0,0,0],rotatefrom=[0,0,1]){
rotations=look_at(lookpoint,origin ,rotatefrom);
rotate(rotations[0],rotations[1]) children();
}

function look_at(p,o=[0,0,0],up=[0,0,1])=
let(
a=up,
b=p-o,
c=cross(a,b) ,
d=angle(a,b))
[d,c];

function angle (a,b)=
atan2(
sqrt((cross(a, b)*cross(a, b))), 
(a* b)
);


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 26, 2017

Rows to columns transposition

Rows to columns transposition.
Works best with even length lists.

In other case, columns will be padded with undefined.
Usage:

rows = [[40, 16, 54, 36, 36, 42, 67, 17, 40] ,
        [76, 20, 60, 51, 58, 73, 11, 17, 48] ,
        [50, 72, 11, 57,  3, 94, 33, 93, 13] ];
cols=rows_to_columns(rows);
echo ( rows);echo (  cols);

function rows_to_columns(l)= 
let(longest_list=max( [for(i=[0:len(l)-1])len(l[i])-1]))
[for( i=[0:longest_list])[for( j=[0:len(l)-1]) l[j][i]]];
 

Thursday, May 11, 2017

Cycle elements of a list

Cycle the first element of a list to the end of the list.
Optionally repet recursively.

Usage:
my_list
=shuffle([1,2,3,4,5,6,7,8]);

function shuffle(list,l=1)=
let(c=concat( [for(i=[1:len(list)-1])list[i]],[list[0]]))
l>0?
 shuffle(c,l-1)
 :c
;