B-splines: Difference between revisions

From JSXGraph Wiki
(New page: The points are connected by a cubic B-spline curve (i.e. order=4). <html><form> <input type="button" value="Add segment" onClick="addSegment();"> <input type="button" value="Remove last s...)
 
No edit summary
Line 7: Line 7:
var brd = JXG.JSXGraph.initBoard('jxgbox',{boundingbox:[-4,4,4,-4],keepaspectratio:true,axis:true});
var brd = JXG.JSXGraph.initBoard('jxgbox',{boundingbox:[-4,4,4,-4],keepaspectratio:true,axis:true});


var p = [];
var p = [], col = 'red';  
 
var col = 'red';  
p.push(brd.create('point',[2,1],{strokeColor:col,fillColor:col}));  
p.push(brd.create('point',[2,1],{strokeColor:col,fillColor:col}));  
p.push(brd.create('point',[0.75,2.5],{strokeColor:col,fillColor:col}));  
p.push(brd.create('point',[0.75,2.5],{strokeColor:col,fillColor:col}));  
Line 37: Line 35:
   brd.unsuspendUpdate();
   brd.unsuspendUpdate();
};
};
</jsxgraph>
</jsxgraph>


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


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


var col = 'red';
var c = brd.create('curve', JXG.Math.Numerics.bspline(p,4),  
p.push(brd.create('point',[2,1],{strokeColor:col,fillColor:col}));        // data point
col = 'blue';
p.push(brd.create('point',[0.75,2.5],{strokeColor:col,fillColor:col}));  // control point
p.push(brd.create('point',[-0.3,0.3],{strokeColor:col,fillColor:col}));  // control point
 
col = 'red';
p.push(brd.create('point',[-3,1],{strokeColor:col,fillColor:col}));      // data point
col = 'blue';
p.push(brd.create('point',[-0.75,-2.5],{strokeColor:col,fillColor:col})); // control point
p.push(brd.create('point',[1.5,-2.8],{strokeColor:col,fillColor:col}));  // control point
 
col = 'red';
p.push(brd.create('point',[2,-0.5],{strokeColor:col,fillColor:col}));    // data point
 
var c = brd.create('curve', JXG.Math.Numerics.bezier(p),  
               {strokecolor:'blue', strokeOpacity:0.6, strokeWidth:5});  
               {strokecolor:'blue', strokeOpacity:0.6, strokeWidth:5});  


var addSegment = function() {
var addSegment = function() {
   brd.suspendUpdate();
   brd.suspendUpdate();
  col = 'blue';
   p.push(brd.create('point',[Math.random()*8-4,Math.random()*8-4],
   p.push(brd.create('point',[Math.random()*8-4,Math.random()*8-4],
           {strokeColor:col,fillColor:col})); // control point
           {strokeColor:col,fillColor:col}));  
  p.push(brd.create('point',[Math.random()*8-4,Math.random()*8-4],
          {strokeColor:col,fillColor:col}));  // control point
  col = 'red';
  p.push(brd.create('point',[Math.random()*8-4,Math.random()*8-4],
          {strokeColor:col,fillColor:col}));    // data point
   brd.unsuspendUpdate();
   brd.unsuspendUpdate();
};
};
Line 80: Line 63:
   brd.suspendUpdate();
   brd.suspendUpdate();


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

Revision as of 13:40, 30 July 2010

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})); 
p.push(brd.create('point',[0.75,2.5],{strokeColor:col,fillColor:col})); 
p.push(brd.create('point',[-0.3,0.3],{strokeColor:col,fillColor:col})); 
p.push(brd.create('point',[-3,1],{strokeColor:col,fillColor:col}));      
p.push(brd.create('point',[-0.75,-2.5],{strokeColor:col,fillColor:col}));
p.push(brd.create('point',[1.5,-2.8],{strokeColor:col,fillColor:col}));  
p.push(brd.create('point',[2,-0.5],{strokeColor:col,fillColor:col})); 

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

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();
};