Circle approximation: Difference between revisions
From JSXGraph Wiki
A WASSERMANN (talk | contribs) No edit summary |
A WASSERMANN (talk | contribs) No edit summary |
||
Line 52: | Line 52: | ||
===The underlying JavaScript code=== | ===The underlying JavaScript code=== | ||
<source lang="xml"> | <source lang="xml"> | ||
<jsxgraph width="600" height="600" box="box"> | |||
var brd = JXG.JSXGraph.initBoard('box', {originX: 300, originY: 300, grid:true, unitX: 50, unitY: 50}); | var brd = JXG.JSXGraph.initBoard('box', {originX: 300, originY: 300, grid:true, unitX: 50, unitY: 50}); | ||
var n = brd.createElement('slider',[[-5,5],[4.5,5],[3,4,96]],{name:'n',snapWidth:1}); | var n = brd.createElement('slider',[[-5,5],[4.5,5],[3,4,96]],{name:'n',snapWidth:1}); | ||
var p0 = brd.createElement('point',[0,0]); | var p0 = brd.createElement('point',[0,0],{strokeColor:'black',fillColor:'white',name:''}); | ||
var p1 = brd.createElement('point',[4,0] | var p1 = brd.createElement('point',[4,0],{strokeColor:'black',fillColor:'white',name:''}); | ||
var rot = brd.createElement('transform', [function() {return Math.PI*2.0/n.Value();},p0], {type:'rotate'}); // angle, rotation center | var rot = brd.createElement('transform', [function() {return Math.PI*2.0/n.Value();},p0], {type:'rotate'}); // angle, rotation center | ||
var ptmp = brd.createElement('point',[0,0],{visible:false,withLabel:false}); // dummy point for the rotation | var ptmp = brd.createElement('point',[0,0],{visible:false,withLabel:false}); // dummy point for the rotation | ||
var cOut = brd.createElement('curve',[[],[]],{fillColor:'# | var cOut = brd.createElement('curve',[[],[]],{fillColor:'#5e9abf',highlightFillColor:'#5e9abf',fillOpacity:1,highlightFillOpacity:1,strokeColor:'black',highlightStrokeColor:'black'}); | ||
var cIn = brd.createElement('curve',[[],[]],{fillColor:'# | var circ = brd.createElement('circle',[p0,p1],{fillColor:'#fefd4c',highlightFillColor:'#fefd4c',fillOpacity:1,highlightFillOpacity:1,strokeColor:'black',highlightStrokeColor:'black'}); | ||
var cIn = brd.createElement('curve',[[],[]],{fillColor:'#d769a3',highlightFillColor:'#d769a3',fillOpacity:1,highlightFillOpacity:1,strokeColor:'black',highlightStrokeColor:'black'}); | |||
var tCirc = brd.createElement('text',[-5,-4.0,function(){ | var tCirc = brd.createElement('text',[-5,-4.0,function(){ | ||
Line 99: | Line 100: | ||
brd.update(); | brd.update(); | ||
</jsxgraph> | |||
</source> | </source> | ||
[[Category:Examples]] | [[Category:Examples]] | ||
[[Category:Calculus]] | [[Category:Calculus]] |
Revision as of 15:07, 30 July 2009
The underlying JavaScript code
<jsxgraph width="600" height="600" box="box">
var brd = JXG.JSXGraph.initBoard('box', {originX: 300, originY: 300, grid:true, unitX: 50, unitY: 50});
var n = brd.createElement('slider',[[-5,5],[4.5,5],[3,4,96]],{name:'n',snapWidth:1});
var p0 = brd.createElement('point',[0,0],{strokeColor:'black',fillColor:'white',name:''});
var p1 = brd.createElement('point',[4,0],{strokeColor:'black',fillColor:'white',name:''});
var rot = brd.createElement('transform', [function() {return Math.PI*2.0/n.Value();},p0], {type:'rotate'}); // angle, rotation center
var ptmp = brd.createElement('point',[0,0],{visible:false,withLabel:false}); // dummy point for the rotation
var cOut = brd.createElement('curve',[[],[]],{fillColor:'#5e9abf',highlightFillColor:'#5e9abf',fillOpacity:1,highlightFillOpacity:1,strokeColor:'black',highlightStrokeColor:'black'});
var circ = brd.createElement('circle',[p0,p1],{fillColor:'#fefd4c',highlightFillColor:'#fefd4c',fillOpacity:1,highlightFillOpacity:1,strokeColor:'black',highlightStrokeColor:'black'});
var cIn = brd.createElement('curve',[[],[]],{fillColor:'#d769a3',highlightFillColor:'#d769a3',fillOpacity:1,highlightFillOpacity:1,strokeColor:'black',highlightStrokeColor:'black'});
var tCirc = brd.createElement('text',[-5,-4.0,function(){
return 'Area of the circle (radius='+circ.getRadius().toFixed(2)+') = ' + (circ.getRadius()*circ.getRadius()*Math.PI).toFixed(4);
}],{fontSize:'20px'});
var tIn = brd.createElement('text',[-5,-4.5,function(){
return 'Area of the inscribed ' +n.Value().toFixed(0)+ '-polygon = ' + (n.Value()*circ.getRadius()*circ.getRadius()*Math.sin(Math.PI/n.Value())).toFixed(4);
}],{fontSize:'20px'});
var tOut = brd.createElement('text',[-5,-5,function(){
return 'Area of the circumscribed ' +n.Value().toFixed(0)+ '-polygon = ' + (n.Value()*circ.getRadius()*circ.getRadius()*Math.tan(Math.PI/n.Value())).toFixed(4);
}],{fontSize:'20px'});
cIn.updateDataArray = function() {
var i;
this.dataX = [p0.X()+circ.getRadius()];
this.dataY = [p0.Y()];
ptmp.setPositionDirectly(JXG.COORDS_BY_USER,p0.X()+circ.getRadius(),p0.Y());
for (i=0;i<n.Value();i++) {
rot.applyOnce(ptmp);
this.dataX.push(ptmp.X());
this.dataY.push(ptmp.Y());
}
}
cOut.updateDataArray = function() {
var i;
var s = circ.getRadius()/Math.cos(Math.PI/n.Value());
this.dataX = [p0.X()+s];
this.dataY = [p0.Y()];
ptmp.setPositionDirectly(JXG.COORDS_BY_USER, p0.X()+s,p0.Y());
for (i=0;i<n.Value();i++) {
rot.applyOnce(ptmp);
this.dataX.push(ptmp.X());
this.dataY.push(ptmp.Y());
}
}
brd.update();
</jsxgraph>