Showing posts with label line. Show all posts
Showing posts with label line. Show all posts

Friday, December 1, 2017

Fit a 4 point bezier curve for desired length

module to draw a simple 4 point Bez curve. 
function for piece-wise length  estimation. 

And a recursive function to fit control points for the length of your pleasure. 

function AdjustBezier(v,l, precision = 0.00001)=
l<norm(v[0]-v[3])?
let(e=echo("Cant be that short, sorry"))[v[0],v[0] ,v[3] ,v[3]]:
let(
current_lenght=len3bz(v),
error=l/current_lenght,
e=echo(l,current_lenght,error),
new_v=[v[0],v[0]+(v[1]-v[0])*error,v[3]+(v[2]-v[3])*error,v[3]]
)
abs(1-error)>precision?AdjustBezier(new_v,l):v;



function len3bz(v, precision = 0.001, t = 0, acc = 0) =
 t > 1 ? acc : 
len3bz(v, precision, t + precision, acc 
+ norm(bez2(t, v) - bez2(t + precision, v)));

function bez2(t, v) = (len(v) > 2) ? bez2(t, [
  for (i = [0: len(v) - 2]) v[i]* (t)  + v[i + 1] * (1 - t)
]): v[0]* (t)  + v[1]* (1 - t) ;

module ShowControl(v) { // translate(t(v[0])) sphere(v[0][3]);
 
    for (i = [1: len(v) - 1]) {
      // vg  translate(t(v[i])) sphere(v[i][3]);
      hull() {
        translate(t(v[i])) sphere(0.5);
        translate(t(v[i - 1])) sphere(0.5);
      }
    }
}

module ShowBezier(v,steps=50) 
{ // translate(t(v[0])) sphere(v[0][3]);
 step=1/steps;
    for (i = [-step:step:  1+step]) {
      // vg  translate(t(v[i])) sphere(v[i][3]);
      hull() {
        translate(t(bez2(clamp(i), v) ))sphere(1);
        translate(t(bez2(clamp(i+step), v))) sphere(1);
      }
    }
}


function t(v) = [v.x, v.y, v.z];
function clamp(a, b = 0, c = 1) = min(max(a, b), c);

MyBezier=[[0,0,0],[0,0,10],[50,0,50],[50,10,50]];
Newlengh=175;

ShowControl(MyBezier) ; 
ShowBezier(MyBezier) ;
echo(len3bz(MyBezier));

MyNewBezier=AdjustBezier(MyBezier,Newlengh) ;

color("red"){
ShowControl(MyNewBezier) ; 
ShowBezier(MyNewBezier) ;
echo(len3bz(MyNewBezier));}

Wednesday, May 31, 2017

Find a point some partial length along a poly-line

len3v sum's up total length if a poly-line
v2t returns a point
 some partial length along the poly-line.
Far_Along is a 0-1 multiplier to regularize the expression with other code
Usage:



My_poly_line=make_points(6);
LengthOfIt=(len3v(My_poly_line));
Far_Along=rnd(1);
My_Point_Exactly=v2t(My_poly_line,LengthOfIt*Far_Along);
echo( str(round(Far_Along*100),"% "),My_Point_Exactly);
polyline(My_poly_line);
translate(My_Point_Exactly)color("gold")sphere(3);
translate(My_poly_line[0])color("red")sphere(2);
translate(My_poly_line[len(My_poly_line)-1])color("blue")sphere(2);

module line(p1, p2 ,width=0.1) { hull() {        
translate(p1) sphere(width);
translate(p2) sphere(width);    }}

module polyline(p) {for(i=[0:max(0,len(p)-2)])line(p[i],p[i+1]);}

function un(v)=v/max(1e-15,norm(v));
function len3v(v,acc=0,p=0)=p+1>len(v)-1?
        acc: len3v(v,acc+norm(v[p]-v[p+1]),p+1)  ;
function v2t(v,stop,p=0)=p+1>len(v)-1|| norm(v[p]-v[p+1])>stop?  
 v[p]+un(v[p+1]-v[p])*stop:v2t(v,stop-norm(v[p]-v[p+1]),p+1);
function make_points(j=10,l1=[-30,-30,-30],l2=[30,30,30])= 
     ([for(i=[1:j])[
rnd(l1.x,l2.x)/10+i*10,//linear spagetti
rnd(l1.y,l2.y),
rnd(l1.z,l2.z)]]);
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])
  ; 

module line(p1, p2 ,width=0.1) { hull() {        
translate(p1) sphere(width);
translate(p2) sphere(width);    }}

module polyline(p) {for(i=[0:max(0,len(p)-2)])line(p[i],p[i+1]);}


 

Friday, May 12, 2017

Line

Constructs a line between point p1 and point p2

Usage:


line([1,-1],[2,2]);

module line(p1, p2 ,width=0.05) 
{
    hull() {
        translate(p1) sphere(width);
        translate(p2) sphere(width);
    }
}