Circle approximation: Difference between revisions

From JSXGraph Wiki
No edit summary
 
(15 intermediate revisions by 2 users not shown)
Line 1: Line 1:
<jsxgraph width="600" height="600" box="box">
<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', {boundingbox: [-6, 6, 6, -6], grid: true});
var n = brd.createElement('slider',[[-5,5],[4.5,5],[3,4,96]],{name:'n',snapWidth:1});
var n = brd.create('slider',[[-5,5],[4.5,5],[3,4,96]],{name:'n',snapWidth:1});


var p0 = brd.create('point',[0,0],{strokeColor:'black',fillColor:'white',name:''});
var p0 = brd.create('point',[0,0],{strokeColor:'black',fillColor:'white',name:''});
Line 9: Line 9:
var cOut = brd.create('curve',[[0],[0]],{fillColor:'#5e9abf',highlightFillColor:'#5e9abf',fillOpacity:1,highlightFillOpacity:1,strokeColor:'black',highlightStrokeColor:'black'});
var cOut = brd.create('curve',[[0],[0]],{fillColor:'#5e9abf',highlightFillColor:'#5e9abf',fillOpacity:1,highlightFillOpacity:1,strokeColor:'black',highlightStrokeColor:'black'});
var circ = brd.create('circle',[p0,p1],{fillColor:'#fefd4c',highlightFillColor:'#fefd4c',fillOpacity:0.5,highlightFillOpacity:0.5,strokeColor:'black',highlightStrokeColor:'black'});
var circ = brd.create('circle',[p0,p1],{fillColor:'#fefd4c',highlightFillColor:'#fefd4c',fillOpacity:0.5,highlightFillOpacity:0.5,strokeColor:'black',highlightStrokeColor:'black'});
var cIn = brd.create('curve',[[0],[0]],{fillColor:'#d769a3',highlightFillColor:'#d769a3',fillOpacity:1,highlightFillOpacity:1,strokeColor:'black',highlightStrokeColor:'black'});
var cIn = brd.create('curve',[[0,1,2],[0,1,2]],{fillColor:'#d769a3',highlightFillColor:'#d769a3',fillOpacity:1,highlightFillOpacity:1,strokeColor:'black',highlightStrokeColor:'black'});


var tCirc = brd.create('text',[-5,-4.0,function(){
var tCirc = brd.create('text',[-5,-4.0,function(){
     return 'Area of the circle = ' + (0.0625*circ.getRadius()*circ.getRadius()*Math.PI).toFixed(5);
     return 'Area of the circle = ' + (0.0625*circ.getRadius()*circ.getRadius()*Math.PI).toFixed(5);
   }],{fontSize:'20px'});
   }],{fontSize:20});
var tIn = brd.create('text',[-5,-4.5,function(){
var tIn = brd.create('text',[-5,-4.5,function(){
     return 'Area of the inscribed ' +n.Value().toFixed(0)+ '-polygon = ' + (0.0625*n.Value()*circ.getRadius()*circ.getRadius()*Math.sin(Math.PI/n.Value())).toFixed(5);
     return 'Area of the inscribed ' +n.Value().toFixed(0)+ '-polygon = ' + (0.0625*n.Value()*circ.getRadius()*circ.getRadius()*Math.sin(Math.PI/n.Value())).toFixed(5);
   }],{fontSize:'20px'});
   }],{fontSize:20});
var tOut = brd.create('text',[-5,-5,function(){
var tOut = brd.create('text',[-5,-5,function(){
     return 'Area of the circumscribed  ' +n.Value().toFixed(0)+ '-polygon = ' + (0.0625*n.Value()*circ.getRadius()*circ.getRadius()*Math.tan(Math.PI/n.Value())).toFixed(5);
     return 'Area of the circumscribed  ' +n.Value().toFixed(0)+ '-polygon = ' + (0.0625*n.Value()*circ.getRadius()*circ.getRadius()*Math.tan(Math.PI/n.Value())).toFixed(5);
   }],{fontSize:'20px'});
   }],{fontSize:20});


cIn.updateDataArray = function() {
cIn.updateDataArray = function() {
Line 25: Line 25:
   this.dataX = [p0.X()+circ.getRadius()];
   this.dataX = [p0.X()+circ.getRadius()];
   this.dataY = [p0.Y()];
   this.dataY = [p0.Y()];
   ptmp.setPositionDirectly(JXG.COORDS_BY_USER,p0.X()+circ.getRadius(),p0.Y());
   ptmp.setPositionDirectly(JXG.COORDS_BY_USER, [p0.X()+circ.getRadius(), p0.Y()]);
   for (i=0;i<len;i++) {
   for (i=0;i<len;i++) {
     rot.applyOnce(ptmp);
     rot.applyOnce(ptmp);
Line 38: Line 38:
   this.dataX = [p0.X()+s];
   this.dataX = [p0.X()+s];
   this.dataY = [p0.Y()];
   this.dataY = [p0.Y()];
   ptmp.setPositionDirectly(JXG.COORDS_BY_USER, p0.X()+s,p0.Y());
   ptmp.setPositionDirectly(JXG.COORDS_BY_USER, [p0.X()+s,p0.Y()]);
   for (i=0;i<len;i++) {
   for (i=0;i<len;i++) {
     rot.applyOnce(ptmp);
     rot.applyOnce(ptmp);
Line 50: Line 50:
</jsxgraph>
</jsxgraph>


===The underlying JavaScript code===
===The JavaScript code===
<source lang="xml">
<source lang="javascript">
<jsxgraph width="600" height="600" box="box">
var brd = JXG.JSXGraph.initBoard('box', {boundingbox: [-6, 6, 6, -6], grid: true});
var brd = JXG.JSXGraph.initBoard('box', {originX: 300, originY: 300, grid:true, unitX: 50, unitY: 50});
var n = brd.create('slider',[[-5,5],[4.5,5],[3,4,96]],{name:'n',snapWidth:1});


// Slider that controls the number n of vertices of the two polygons
var n = brd.createElement('slider',[[-5,5],[4.5,5],[3,4,96]],{name:'n',snapWidth:1});
// p0, p1 define the circle 'circ'
var p0 = brd.create('point',[0,0],{strokeColor:'black',fillColor:'white',name:''});
var p0 = brd.create('point',[0,0],{strokeColor:'black',fillColor:'white',name:''});
var p1 = brd.create('point',[4,0],{strokeColor:'black',fillColor:'white',name:''});
var p1 = brd.create('point',[4,0],{strokeColor:'black',fillColor:'white',name:''});
// With the help of the rotational transform  'rot' we will compute the vertices
// of the polygons
var rot = brd.create('transform', [function() {return Math.PI*2.0/n.Value();},p0], {type:'rotate'});  // angle, rotation center
var rot = brd.create('transform', [function() {return Math.PI*2.0/n.Value();},p0], {type:'rotate'});  // angle, rotation center
var ptmp = brd.create('point',[0,0],{visible:false,withLabel:false});  // dummy point for the rotation
var ptmp = brd.create('point',[0,0],{visible:false,withLabel:false});  // dummy point for the rotation
// Two curves cOut and cIn defining the inner and outer polygon. They both contain dummy paths,
// the correct paths will be computed later.
var cOut = brd.create('curve',[[0],[0]],{fillColor:'#5e9abf',highlightFillColor:'#5e9abf',fillOpacity:1,highlightFillOpacity:1,strokeColor:'black',highlightStrokeColor:'black'});
var cOut = brd.create('curve',[[0],[0]],{fillColor:'#5e9abf',highlightFillColor:'#5e9abf',fillOpacity:1,highlightFillOpacity:1,strokeColor:'black',highlightStrokeColor:'black'});
// Construction of the circle
var circ = brd.create('circle',[p0,p1],{fillColor:'#fefd4c',highlightFillColor:'#fefd4c',fillOpacity:0.5,highlightFillOpacity:0.5,strokeColor:'black',highlightStrokeColor:'black'});
var circ = brd.create('circle',[p0,p1],{fillColor:'#fefd4c',highlightFillColor:'#fefd4c',fillOpacity:0.5,highlightFillOpacity:0.5,strokeColor:'black',highlightStrokeColor:'black'});
var cIn = brd.create('curve',[[0],[0]],{fillColor:'#d769a3',highlightFillColor:'#d769a3',fillOpacity:1,highlightFillOpacity:1,strokeColor:'black',highlightStrokeColor:'black'});
var cIn = brd.create('curve',[[0,1,2],[0,1,2]],{fillColor:'#d769a3',highlightFillColor:'#d769a3',fillOpacity:1,highlightFillOpacity:1,strokeColor:'black',highlightStrokeColor:'black'});


// Construction of the text
var tCirc = brd.create('text',[-5,-4.0,function(){
var tCirc = brd.create('text',[-5,-4.0,function(){
     return 'Area of the circle = ' + (0.0625*circ.getRadius()*circ.getRadius()*Math.PI).toFixed(5);
     return 'Area of the circle = ' + (0.0625*circ.getRadius()*circ.getRadius()*Math.PI).toFixed(5);
   }],{fontSize:'20px'});
   }],{fontSize:20});
var tIn = brd.create('text',[-5,-4.5,function(){
var tIn = brd.create('text',[-5,-4.5,function(){
     return 'Area of the inscribed ' +n.Value().toFixed(0)+ '-polygon = ' + (0.0625*n.Value()*circ.getRadius()*circ.getRadius()*Math.sin(Math.PI/n.Value())).toFixed(5);
     return 'Area of the inscribed ' +n.Value().toFixed(0)+ '-polygon = ' + (0.0625*n.Value()*circ.getRadius()*circ.getRadius()*Math.sin(Math.PI/n.Value())).toFixed(5);
   }],{fontSize:'20px'});
   }],{fontSize:20});
var tOut = brd.create('text',[-5,-5,function(){
var tOut = brd.create('text',[-5,-5,function(){
     return 'Area of the circumscribed  ' +n.Value().toFixed(0)+ '-polygon = ' + (0.0625*n.Value()*circ.getRadius()*circ.getRadius()*Math.tan(Math.PI/n.Value())).toFixed(5);
     return 'Area of the circumscribed  ' +n.Value().toFixed(0)+ '-polygon = ' + (0.0625*n.Value()*circ.getRadius()*circ.getRadius()*Math.tan(Math.PI/n.Value())).toFixed(5);
   }],{fontSize:'20px'});
   }],{fontSize:20});


// In the updateDataArray methods of the curves cIn and cOut the correct paths are computed.
// updateDataArray() is called during the update of the curve. By default, this method
// is empty, but it can be overwritten.
cIn.updateDataArray = function() {
cIn.updateDataArray = function() {
   var i, len = n.Value();
   var i, len = n.Value();
   this.dataX = [p0.X()+circ.getRadius()];
   this.dataX = [p0.X()+circ.getRadius()];
   this.dataY = [p0.Y()];
   this.dataY = [p0.Y()];
   ptmp.setPositionDirectly(JXG.COORDS_BY_USER,p0.X()+circ.getRadius(),p0.Y());
   ptmp.setPositionDirectly(JXG.COORDS_BY_USER, [p0.X()+circ.getRadius(), p0.Y()]);
   for (i=0;i<len;i++) {
   for (i=0;i<len;i++) {
     rot.applyOnce(ptmp);
     rot.applyOnce(ptmp);
Line 106: Line 90:
   this.dataX = [p0.X()+s];
   this.dataX = [p0.X()+s];
   this.dataY = [p0.Y()];
   this.dataY = [p0.Y()];
   ptmp.setPositionDirectly(JXG.COORDS_BY_USER, p0.X()+s,p0.Y());
   ptmp.setPositionDirectly(JXG.COORDS_BY_USER, [p0.X()+s,p0.Y()]);
   for (i=0;i<len;i++) {
   for (i=0;i<len;i++) {
     rot.applyOnce(ptmp);
     rot.applyOnce(ptmp);
Line 115: Line 99:


brd.update();
brd.update();
</jsxgraph>
 
</source>
</source>



Latest revision as of 13:32, 3 March 2021

The JavaScript code

var brd = JXG.JSXGraph.initBoard('box', {boundingbox: [-6, 6, 6, -6], grid: true});
var n = brd.create('slider',[[-5,5],[4.5,5],[3,4,96]],{name:'n',snapWidth:1});

var p0 = brd.create('point',[0,0],{strokeColor:'black',fillColor:'white',name:''});
var p1 = brd.create('point',[4,0],{strokeColor:'black',fillColor:'white',name:''});
var rot = brd.create('transform', [function() {return Math.PI*2.0/n.Value();},p0], {type:'rotate'});  // angle, rotation center
var ptmp = brd.create('point',[0,0],{visible:false,withLabel:false});  // dummy point for the rotation
var cOut = brd.create('curve',[[0],[0]],{fillColor:'#5e9abf',highlightFillColor:'#5e9abf',fillOpacity:1,highlightFillOpacity:1,strokeColor:'black',highlightStrokeColor:'black'});
var circ = brd.create('circle',[p0,p1],{fillColor:'#fefd4c',highlightFillColor:'#fefd4c',fillOpacity:0.5,highlightFillOpacity:0.5,strokeColor:'black',highlightStrokeColor:'black'});
var cIn = brd.create('curve',[[0,1,2],[0,1,2]],{fillColor:'#d769a3',highlightFillColor:'#d769a3',fillOpacity:1,highlightFillOpacity:1,strokeColor:'black',highlightStrokeColor:'black'});

var tCirc = brd.create('text',[-5,-4.0,function(){
    return 'Area of the circle = ' + (0.0625*circ.getRadius()*circ.getRadius()*Math.PI).toFixed(5);
  }],{fontSize:20});
var tIn = brd.create('text',[-5,-4.5,function(){
    return 'Area of the inscribed ' +n.Value().toFixed(0)+ '-polygon = ' + (0.0625*n.Value()*circ.getRadius()*circ.getRadius()*Math.sin(Math.PI/n.Value())).toFixed(5);
  }],{fontSize:20});
var tOut = brd.create('text',[-5,-5,function(){
    return 'Area of the circumscribed  ' +n.Value().toFixed(0)+ '-polygon = ' + (0.0625*n.Value()*circ.getRadius()*circ.getRadius()*Math.tan(Math.PI/n.Value())).toFixed(5);
  }],{fontSize:20});

cIn.updateDataArray = function() {
  var i, len = n.Value();
  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<len;i++) {
    rot.applyOnce(ptmp);
    this.dataX.push(ptmp.X());
    this.dataY.push(ptmp.Y());
  }
}

cOut.updateDataArray = function() {
  var i, len = n.Value();
  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<len;i++) {
    rot.applyOnce(ptmp);
    this.dataX.push(ptmp.X());
    this.dataY.push(ptmp.Y());
  }
}

brd.update();