/* bezier-spline.js * * computes cubic bezier coefficients to generate a smooth * line through specified points. couples with SVG graphics * for interactive processing. * * For more info see: * http://www.particleincell.com/2012/bezier-splines/ * * Lubos Brieda, Particle In Cell Consulting LLC, 2012 * you may freely use this algorithm in your codes however where feasible * please include a link/reference to the source article */ //var S=new Array() /*splines*/ var S; var V=new Array() /*vertices*/ var C /*current object*/ var x0,y0 /*svg offset*/ /*saves elements as global variables*/ function init() { /*create splines*/ //S[0] = createPath("blue"); //S[1] = createPath("red"); //S[2] = createPath("green"); //S[3] = createPath("brown"); /*create control points*/ //V[0] = createKnot(60,60); //V[1] = createKnot(220,300); //V[2] = createKnot(420,300); //V[3] = createKnot(700,240); updateSplines(); } /*creates and adds an SVG circle to represent knots*/ function createKnot(x,y) { V.push([x,y]); } /*creates and adds an SVG path without defining the nodes*/ function createPath(color,width) { width = (typeof width == 'undefined' ? "8" : width); var P=document.createElementNS("http://www.w3.org/2000/svg","path") P.setAttributeNS(null,"fill","none") P.setAttributeNS(null,"stroke",color) P.setAttributeNS(null,"stroke-width",width) svg.appendChild(P) return P } /*computes spline control points*/ function updateSplines() { /*grab (x,y) coordinates of the control points*/ x=new Array(); y=new Array(); for (i=0;i= 0; --i) p1[i] = (r[i] - c[i] * p1[i+1]) / b[i]; /*we have p1, now compute p2*/ for (i=0;i