如何修复 jsxgraph 代码绘制正多边形?
How to fix jsxgraph code draw regularpolygon?
我编写代码来绘制正多边形。但它不根据滑块值绘制。如何解决?
var n = brd.create('slider', [[0,-2],[8,-2],[3,5,10]], {name:'n',snapWidth:1});
var pol = brd.create('regularpolygon',[
function() { return [3, 0];},
function() { return [4, 0];},
function() { return [n.Value()];}]);
brd.unsuspendUpdate();
确实,在 JSXGraph 中,正多边形不能采用动态数量的顶点。解决方法是使用曲线,请参阅 https://jsfiddle.net/6emrfzyu/1/ :
const board = JXG.JSXGraph.initBoard('jxgbox', {
boundingbox: [-3, 6, 6, -3], axis:true
});
var n = board.create('slider', [[-2,-2],[4,-2],[3,5,10]], {name:'n',snapWidth:1});
var A = board.create('point', [3, 0]);
var B = board.create('point', [4, 0]);
var pol = board.create('curve',[[], []], {fillColor: 'yellow', fillOpacity: 0.2});
pol.updateDataArray = function() {
var i, dx, dy, alpha,
no = n.Value();
this.dataX = [A.X(), B.X()];
this.dataY = [A.Y(), B.Y()];
if (no < 2) {
return this;
}
alpha = 2.0 * Math.PI / no;
for (i = 2; i <= no; i++) {
dx = this.dataX[i - 1] - this.dataX[i - 2];
dy = this.dataY[i - 1] - this.dataY[i - 2];
this.dataX.push(Math.cos(alpha) * dx - Math.sin(alpha) * dy + this.dataX[i - 1]);
this.dataY.push(Math.sin(alpha) * dx + Math.cos(alpha) * dy + this.dataY[i - 1]);
}
return this;
};
board.update();
我编写代码来绘制正多边形。但它不根据滑块值绘制。如何解决?
var n = brd.create('slider', [[0,-2],[8,-2],[3,5,10]], {name:'n',snapWidth:1});
var pol = brd.create('regularpolygon',[
function() { return [3, 0];},
function() { return [4, 0];},
function() { return [n.Value()];}]);
brd.unsuspendUpdate();
确实,在 JSXGraph 中,正多边形不能采用动态数量的顶点。解决方法是使用曲线,请参阅 https://jsfiddle.net/6emrfzyu/1/ :
const board = JXG.JSXGraph.initBoard('jxgbox', {
boundingbox: [-3, 6, 6, -3], axis:true
});
var n = board.create('slider', [[-2,-2],[4,-2],[3,5,10]], {name:'n',snapWidth:1});
var A = board.create('point', [3, 0]);
var B = board.create('point', [4, 0]);
var pol = board.create('curve',[[], []], {fillColor: 'yellow', fillOpacity: 0.2});
pol.updateDataArray = function() {
var i, dx, dy, alpha,
no = n.Value();
this.dataX = [A.X(), B.X()];
this.dataY = [A.Y(), B.Y()];
if (no < 2) {
return this;
}
alpha = 2.0 * Math.PI / no;
for (i = 2; i <= no; i++) {
dx = this.dataX[i - 1] - this.dataX[i - 2];
dy = this.dataY[i - 1] - this.dataY[i - 2];
this.dataX.push(Math.cos(alpha) * dx - Math.sin(alpha) * dy + this.dataX[i - 1]);
this.dataY.push(Math.sin(alpha) * dx + Math.cos(alpha) * dy + this.dataY[i - 1]);
}
return this;
};
board.update();