hey,
i just added variable "tension" (straight from the site); if anyone wants to muck about with adding bias/etc., please do.. i'm too bit busy+lazy to finish porting the rest of the ideas on that site..
raiganCode://catmull-rom spline, from GPG1 3.4 function vec2(x,y) { this.x = x; this.y = y; } function DrawPlus(mc, x,y, r) { mc.moveTo(x+r, y); mc.lineTo(x-r, y); mc.moveTo(x, y+r); mc.lineTo(x, y-r); } pList = new Array(); pList.push(new vec2(50, 250)); pList.push(new vec2(100, 300)); pList.push(new vec2(150, 200)); pList.push(new vec2(200, 250)); pList.push(new vec2(250, 300)); pList.push(new vec2(180, 350)); pList.push(new vec2(80, 320)); var sbuf = _root.createEmptyMovieClip("staticBuffer", 100); sbuf.clear(); sbuf.lineStyle(0, 0x000000, 100); for(var i in pList) { var v = pList[i]; DrawPlus(sbuf, v.x, v.y, 2); } var dbuf = _root.createEmptyMovieClip("dynamicBuffer", 200); P_INDEX = 0; U_PARAM = 0.4; function DoStuff() { if(Key.isDown(Key.LEFT)) { U_PARAM -= 0.02; UpdatePos(); } else if(Key.isDown(Key.RIGHT)) { U_PARAM += 0.02; UpdatePos(); } } function UpdatePos() { //wrap point indices if(U_PARAM < 0) { //we moved into the next segment, backwards P_INDEX = ((P_INDEX - 1)+pList.length)%pList.length; U_PARAM = 1 + U_PARAM; } else if(1 < U_PARAM) { //we moved into the next segment, forwards P_INDEX = (P_INDEX + 1)%pList.length; U_PARAM = U_PARAM - 1; } //----- //get the 4 points to interpolate between var p0 = pList[P_INDEX]; var p1 = pList[(P_INDEX + 1)%pList.length]; var p2 = pList[(P_INDEX + 2)%pList.length]; var p3 = pList[(P_INDEX + 3)%pList.length]; var u = U_PARAM;//get parametric position between p1 and p2 var uu = u*u; var uuu = uu*u; /* //calculate weights given u var w0 = -0.5*uuu + uu - 0.5*u; var w1 = 1.5*uuu - 2.5*uu + 1; var w2 = -1.5*uuu + 2*uu + 0.5*u; var w3 = 0.5*uuu - 0.5*uu; var x = p0.x*w0 + p1.x*w1 + p2.x*w2 + p3.x*w3; var y = p0.y*w0 + p1.y*w1 + p2.y*w2 + p3.y*w3; */ /* //this is "the long version"; more than one .x and .y lookup per vector var x = 0.5 *((2 * p1.x) + (-p0.x + p2.x)*u + (2*p0.x - 5*p1.x + 4*p2.x - p3.x)*uu + (-p0.x + 3*p1.x - 3*p2.x + p3.x)*uuu); var y = 0.5 *((2 * p1.y) + (-p0.y + p2.y)*u + (2*p0.y - 5*p1.y + 4*p2.y - p3.y)*uu + (-p0.y + 3*p1.y - 3*p2.y + p3.y)*uuu); */ //lets' try using a generic cardinal spline with tension //(from http://www.cubic.org/~submissive/sourcerer/hermite.htm) var h0 = 2*uuu - 3*uu + 1; // calculate basis function 1 var h1 = -2*uuu + 3*uu; // calculate basis function 2 var h2 = uuu - 2*uu + u; // calculate basis function 3 var h3 = uuu - uu; // calculate basis function 4 //use p0 and p3 to calculate tangents at p1 and p2 var a = 0.5;//tension; 0 is linear, 0.5 is catmull-rom, >1 is a bit weird.. var t1x = a*(p2.x - p0.x); var t1y = a*(p2.y - p0.y); var t2x = a*(p3.x - p1.x); var t2y = a*(p3.y - p1.y); var x = h0*p1.x + h1*p2.x + h2*t1x + h3*t2x; var y = h0*p1.y + h1*p2.y + h2*t1y + h3*t2y; //draw the result dbuf.clear(); dbuf.lineStyle(0, 0x228822, 100); DrawPlus(dbuf, x, y, 4); } UpdatePos(); _root.onEnterFrame = DoStuff;




Reply With Quote