B-splines

From JSXGraph Wiki
Revision as of 07:58, 4 August 2010 by A WASSERMANN (talk | contribs)
Jump to navigationJump to search

The points are connected by a cubic B-spline curve (i.e. order=4).

The underlying JavaScript code

var brd = JXG.JSXGraph.initBoard('jxgbox',{boundingbox:[-4,4,4,-4],keepaspectratio:true,axis:true});

var p = [], col = 'red'; 
p.push(brd.create('point',[2,1],{strokeColor:col,fillColor:col,shadow:true})); 
p.push(brd.create('point',[0.75,2.5],{strokeColor:col,fillColor:col,shadow:true})); 
p.push(brd.create('point',[-0.3,0.3],{strokeColor:col,fillColor:col,shadow:true})); 
p.push(brd.create('point',[-3,1],{strokeColor:col,fillColor:col,shadow:true}));      
p.push(brd.create('point',[-0.75,-2.5],{strokeColor:col,fillColor:col,shadow:true}));
p.push(brd.create('point',[1.5,-2.8],{strokeColor:col,fillColor:col,shadow:true}));  
p.push(brd.create('point',[2,-0.5],{strokeColor:col,fillColor:col,shadow:true})); 

var c = brd.create('curve', JXG.Math.Numerics.bspline(p,4), 
               {strokecolor:'blue', strokeOpacity:0.6, strokeWidth:5,shadow:true}); 

var addSegment = function() {
   brd.suspendUpdate();
   p.push(brd.create('point',[Math.random()*8-4,Math.random()*8-4],
           {strokeColor:col,fillColor:col})); 
   brd.unsuspendUpdate();
};

var removeSegment = function() {
   brd.suspendUpdate();

   if (p.length>2) {
       brd.removeObject(p[p.length-1]);
       p.splice(p.length-1,1);
   }
   brd.unsuspendUpdate();
};