Bezier curves II: Difference between revisions
From JSXGraph Wiki
No edit summary |
A WASSERMANN (talk | contribs) No edit summary |
||
Line 10: | Line 10: | ||
brd.suspendUpdate(); | brd.suspendUpdate(); | ||
col = 'red'; | col = 'red'; | ||
p.push(brd. | p.push(brd.create('point',[2,1],{strokeColor:col,fillColor:col})); // data point | ||
col = 'blue'; | col = 'blue'; | ||
p.push(brd. | p.push(brd.create('point',[0.75,2.5],{strokeColor:col,fillColor:col})); // control point | ||
p.push(brd. | p.push(brd.create('point',[-1.5,2.4],{strokeColor:col,fillColor:col})); // control point | ||
col = 'red'; | col = 'red'; | ||
p.push(brd. | p.push(brd.create('point',[-3,1],{strokeColor:col,fillColor:col})); // data point | ||
l.push(brd. | l.push(brd.create('segment',[p[0],p[1]],{strokeOpacity:0.5})); | ||
l.push(brd. | l.push(brd.create('segment',[p[1],p[2]],{strokeOpacity:0.5})); | ||
l.push(brd. | l.push(brd.create('segment',[p[2],p[3]],{strokeOpacity:0.5})); | ||
col = 'black'; | col = 'black'; | ||
m.push(brd. | m.push(brd.create('midpoint',[l[0]],{face:'o',size:1,strokeColor:col,fillColor:col, name:''})); | ||
m.push(brd. | m.push(brd.create('midpoint',[l[1]],{face:'o',size:1,strokeColor:col,fillColor:col, name:''})); | ||
m.push(brd. | m.push(brd.create('midpoint',[l[2]],{face:'o',size:1,strokeColor:col,fillColor:col, name:''})); | ||
l.push(brd. | l.push(brd.create('segment',[m[0],m[1]],{strokeOpacity:0.5})); | ||
l.push(brd. | l.push(brd.create('segment',[m[1],m[2]],{strokeOpacity:0.5})); | ||
m.push(brd. | m.push(brd.create('midpoint',[l[3]],{face:'o',size:1,strokeColor:col,fillColor:col, name:''})); | ||
m.push(brd. | m.push(brd.create('midpoint',[l[4]],{face:'o',size:1,strokeColor:col,fillColor:col, name:''})); | ||
l.push(brd. | l.push(brd.create('segment',[m[3],m[4]],{strokeOpacity:0.5})); | ||
m.push(brd. | m.push(brd.create('midpoint',[l[5]],{face:'o',size:1,strokeColor:col,fillColor:col, name:''})); | ||
var c = brd. | var c = brd.create('curve', JXG.Math.Numerics.bezier(p), {strokecolor:'blue', strokeOpacity:0.6, strokeWidth:5}); | ||
brd.unsuspendUpdate(); | brd.unsuspendUpdate(); | ||
</jsxgraph> | </jsxgraph> | ||
Line 43: | Line 43: | ||
===The underlying JavaScript code=== | ===The underlying JavaScript code=== | ||
<source lang="javascript"> | <source lang="javascript"> | ||
JXG.Options.renderer = "canvas"; | |||
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 49: | Line 50: | ||
brd.suspendUpdate(); | brd.suspendUpdate(); | ||
col = 'red'; | col = 'red'; | ||
p.push(brd. | p.push(brd.create('point',[2,1],{strokeColor:col,fillColor:col})); // data point | ||
col = 'blue'; | col = 'blue'; | ||
p.push(brd. | p.push(brd.create('point',[0.75,2.5],{strokeColor:col,fillColor:col})); // control point | ||
p.push(brd. | p.push(brd.create('point',[-1.5,2.4],{strokeColor:col,fillColor:col})); // control point | ||
col = 'red'; | col = 'red'; | ||
p.push(brd. | p.push(brd.create('point',[-3,1],{strokeColor:col,fillColor:col})); // data point | ||
l.push(brd. | l.push(brd.create('segment',[p[0],p[1]],{strokeOpacity:0.5})); | ||
l.push(brd. | l.push(brd.create('segment',[p[1],p[2]],{strokeOpacity:0.5})); | ||
l.push(brd. | l.push(brd.create('segment',[p[2],p[3]],{strokeOpacity:0.5})); | ||
col = 'black'; | col = 'black'; | ||
m.push(brd. | m.push(brd.create('midpoint',[l[0]],{face:'o',size:1,strokeColor:col,fillColor:col, name:''})); | ||
m.push(brd. | m.push(brd.create('midpoint',[l[1]],{face:'o',size:1,strokeColor:col,fillColor:col, name:''})); | ||
m.push(brd. | m.push(brd.create('midpoint',[l[2]],{face:'o',size:1,strokeColor:col,fillColor:col, name:''})); | ||
l.push(brd. | l.push(brd.create('segment',[m[0],m[1]],{strokeOpacity:0.5})); | ||
l.push(brd. | l.push(brd.create('segment',[m[1],m[2]],{strokeOpacity:0.5})); | ||
m.push(brd. | m.push(brd.create('midpoint',[l[3]],{face:'o',size:1,strokeColor:col,fillColor:col, name:''})); | ||
m.push(brd. | m.push(brd.create('midpoint',[l[4]],{face:'o',size:1,strokeColor:col,fillColor:col, name:''})); | ||
l.push(brd. | l.push(brd.create('segment',[m[3],m[4]],{strokeOpacity:0.5})); | ||
m.push(brd. | m.push(brd.create('midpoint',[l[5]],{face:'o',size:1,strokeColor:col,fillColor:col, name:''})); | ||
var c = brd. | var c = brd.create('curve', JXG.Math.Numerics.bezier(p), {strokecolor:'blue', strokeOpacity:0.6, strokeWidth:5}); | ||
brd.unsuspendUpdate(); | brd.unsuspendUpdate(); | ||
</source> | </source> | ||
Latest revision as of 09:18, 18 January 2021
The red points are connected by a cubic Bezier curve. The blue points are the control points.
Here, the Bezier curve together with its scaffolds is shown. The black points are always the midpoints of their line segment.
The underlying JavaScript code
JXG.Options.renderer = "canvas";
var brd = JXG.JSXGraph.initBoard('jxgbox',{boundingbox:[-4,4,4,-4],keepaspectratio:true,axis:true});
var p = []; l = [], m= [];
brd.suspendUpdate();
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',[-1.5,2.4],{strokeColor:col,fillColor:col})); // control point
col = 'red';
p.push(brd.create('point',[-3,1],{strokeColor:col,fillColor:col})); // data point
l.push(brd.create('segment',[p[0],p[1]],{strokeOpacity:0.5}));
l.push(brd.create('segment',[p[1],p[2]],{strokeOpacity:0.5}));
l.push(brd.create('segment',[p[2],p[3]],{strokeOpacity:0.5}));
col = 'black';
m.push(brd.create('midpoint',[l[0]],{face:'o',size:1,strokeColor:col,fillColor:col, name:''}));
m.push(brd.create('midpoint',[l[1]],{face:'o',size:1,strokeColor:col,fillColor:col, name:''}));
m.push(brd.create('midpoint',[l[2]],{face:'o',size:1,strokeColor:col,fillColor:col, name:''}));
l.push(brd.create('segment',[m[0],m[1]],{strokeOpacity:0.5}));
l.push(brd.create('segment',[m[1],m[2]],{strokeOpacity:0.5}));
m.push(brd.create('midpoint',[l[3]],{face:'o',size:1,strokeColor:col,fillColor:col, name:''}));
m.push(brd.create('midpoint',[l[4]],{face:'o',size:1,strokeColor:col,fillColor:col, name:''}));
l.push(brd.create('segment',[m[3],m[4]],{strokeOpacity:0.5}));
m.push(brd.create('midpoint',[l[5]],{face:'o',size:1,strokeColor:col,fillColor:col, name:''}));
var c = brd.create('curve', JXG.Math.Numerics.bezier(p), {strokecolor:'blue', strokeOpacity:0.6, strokeWidth:5});
brd.unsuspendUpdate();