Difference between revisions of "Bezier curves"

From JSXGraph Wiki
Jump to: navigation, search
Line 1: Line 1:
 
The red points are connected by a cubic Bezier curve. The blue points are the control points.
 
The red points are connected by a cubic Bezier curve. The blue points are the control points.
<html><form>
+
<html><!--<form>
 
<input type="button" value="Add segment" onClick="addSegment();">
 
<input type="button" value="Add segment" onClick="addSegment();">
 
<input type="button" value="Remove last segment" onClick="removeSegment();">
 
<input type="button" value="Remove last segment" onClick="removeSegment();">
</form></html>
+
</form>--></html>
 
<jsxgraph width="600" height="600">
 
<jsxgraph width="600" height="600">
 
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});
Line 9: Line 9:
 
var p = [];
 
var p = [];
  
col = 'red';  
+
var col = 'red';  
 
p.push(brd.createElement('point',[2,1],{strokeColor:col,fillColor:col}));        // data point
 
p.push(brd.createElement('point',[2,1],{strokeColor:col,fillColor:col}));        // data point
 
col = 'blue';  
 
col = 'blue';  
Line 27: Line 27:
  
 
var addSegment = function() {
 
var addSegment = function() {
col = 'blue';  
+
  col = 'blue';  
p.push(brd.createElement('point',[Math.random()*8-4,Math.random()*8-4],
+
  p.push(brd.createElement('point',[Math.random()*8-4,Math.random()*8-4],
 
           {strokeColor:col,fillColor:col})); // control point
 
           {strokeColor:col,fillColor:col})); // control point
p.push(brd.createElement('point',[Math.random()*8-4,Math.random()*8-4],
+
  p.push(brd.createElement('point',[Math.random()*8-4,Math.random()*8-4],
 
           {strokeColor:col,fillColor:col}));  // control point
 
           {strokeColor:col,fillColor:col}));  // control point
col = 'red';  
+
  col = 'red';  
p.push(brd.createElement('point',[Math.random()*8-4,Math.random()*8-4],
+
  p.push(brd.createElement('point',[Math.random()*8-4,Math.random()*8-4],
 
           {strokeColor:col,fillColor:col}));    // data point
 
           {strokeColor:col,fillColor:col}));    // data point
 
};
 
};

Revision as of 08:15, 29 September 2009

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 = [];

col = 'red'; 
p.push(brd.createElement('point',[2,1],{strokeColor:col,fillColor:col}));        // data point
col = 'blue'; 
p.push(brd.createElement('point',[0.75,2.5],{strokeColor:col,fillColor:col}));   // control point
p.push(brd.createElement('point',[-0.3,0.3],{strokeColor:col,fillColor:col}));   // control point

col = 'red'; 
p.push(brd.createElement('point',[-3,1],{strokeColor:col,fillColor:col}));       // data point
col = 'blue'; 
p.push(brd.createElement('point',[-0.75,-2.5],{strokeColor:col,fillColor:col})); // control point
p.push(brd.createElement('point',[1.5,-2.8],{strokeColor:col,fillColor:col}));   // control point

col = 'red'; 
p.push(brd.createElement('point',[2,-0.5],{strokeColor:col,fillColor:col}));     // data point

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