Difference between revisions of "Bezier curves"

From JSXGraph Wiki
Jump to navigationJump to search
(New page: <jsxgraph width="600" height="600"> brd = JXG.board('jxgbox',{boundingbox:[-4,4,4,-4],keepaspectratio:true}); </jsxgraph>)
 
 
(18 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
The red points are connected by a cubic Bezier curve. The blue points are the control points.
 +
<html><form>
 +
<input type="button" value="Add segment" onClick="addSegment();">
 +
<input type="button" value="Remove last segment" onClick="removeSegment();">
 +
</form></html>
 
<jsxgraph width="600" height="600">
 
<jsxgraph width="600" height="600">
brd = JXG.board('jxgbox',{boundingbox:[-4,4,4,-4],keepaspectratio:true});
+
var brd = JXG.JSXGraph.initBoard('jxgbox',{boundingbox:[-4,4,4,-4],keepaspectratio:true,axis:true});
 +
 
 +
var p = [];
 +
 
 +
var col = 'red';
 +
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});
 +
 
 +
var addSegment = function() {
 +
  brd.suspendUpdate();
 +
  col = 'blue';
 +
  p.push(brd.create('point',[Math.random()*8-4,Math.random()*8-4],
 +
          {strokeColor:col,fillColor:col})); // control point
 +
  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();
 +
};
 +
 
 +
var removeSegment = function() {
 +
  brd.suspendUpdate();
 +
 
 +
  if (p.length>4) {
 +
      brd.removeObject(p[p.length-1]);
 +
      brd.removeObject(p[p.length-2]);
 +
      brd.removeObject(p[p.length-3]);
 +
      p.splice(p.length-3,3);
 +
  }
 +
  brd.unsuspendUpdate();
 +
};
 +
 
 
</jsxgraph>
 
</jsxgraph>
 +
 +
===The underlying JavaScript code===
 +
<source lang="javascript">
 +
var brd = JXG.JSXGraph.initBoard('jxgbox',{boundingbox:[-4,4,4,-4],keepaspectratio:true,axis:true});
 +
 +
var p = [];
 +
 +
var col = 'red';
 +
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});
 +
 +
var addSegment = function() {
 +
  brd.suspendUpdate();
 +
  col = 'blue';
 +
  p.push(brd.create('point',[Math.random()*8-4,Math.random()*8-4],
 +
          {strokeColor:col,fillColor:col})); // control point
 +
  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();
 +
};
 +
 +
var removeSegment = function() {
 +
  brd.suspendUpdate();
 +
 +
  if (p.length>4) {
 +
      brd.removeObject(p[p.length-1]);
 +
      brd.removeObject(p[p.length-2]);
 +
      brd.removeObject(p[p.length-3]);
 +
      p.splice(p.length-3,3);
 +
  }
 +
  brd.unsuspendUpdate();
 +
};
 +
</source>
 +
 +
[[Category:Examples]]
 +
[[Category:Curves]]
 +
[[Category:Interpolation]]

Latest revision as of 12:21, 29 July 2010

The red points are connected by a cubic Bezier curve. The blue points are the control points.

The underlying JavaScript code

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

var p = [];

var col = 'red'; 
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}); 

var addSegment = function() {
   brd.suspendUpdate();
   col = 'blue'; 
   p.push(brd.create('point',[Math.random()*8-4,Math.random()*8-4],
           {strokeColor:col,fillColor:col})); // control point
   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();
};

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

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