JSXGraph函数图像旋转故障排查:三次函数拐点交互演示问题
解决JSXGraph中三次函数段旋转变换不可见的问题
你判断的完全正确:JSXGraph的functiongraph元素不支持直接应用几何变换。这类元素是通过实时计算函数值动态绘制的,变换逻辑无法作用于它。要实现你需要的旋转效果,必须把需要旋转的函数段转换成基于点集的curve元素——curve是由离散点构成的图形,完全支持各类几何变换操作。
以下是修正后的完整代码,核心修改点已标注:
<div id="cubic11" class="jxgbox" style="width:400px; height:300px; margin:auto "></div> <script type="text/javascript"> // Create board template JXG.Options.text.useMathJax = true; (function() { var board = JXG.JSXGraph.initBoard('cubic11', { boundingbox: [-3, 4, 3, -4], axis: true, keepaspectratio: true, showGrid: true }); // Create function plot for f1: y = x^3 var f1 = board.create('functiongraph', [function(x) { return x * x * x; }, -6, 6], { strokeColor: '#EF233C' }); // 核心修改:用curve替代functiongraph生成x<0的函数段 // 生成[-6, 0]区间的采样点,保证曲线平滑度 var xVals = JXG.Math.Numerics.linspace(-6, 0, 200); var yVals = xVals.map(x => x * x * x); var f2 = board.create('curve', [xVals, yVals], { strokeColor: '#888888' }); // Label point of inflection/rotation var ip = board.create('point', [0, 0], { fixed: true, }); // Create slider for rotation of f2 var r = board.create('slider', [ [1, -2], [4, -2], [0, 0, 360] ], { name: 'rotation', snapWidth: 45, highline: { strokeColor: '#ED7D31' }, label: { strokeColor: '#ED7D31' }, fillColor: '#ED7D31', highlightStrokeColor: '#ED7D31' }); // 创建旋转变换并应用到curve元素 var t = board.create('transform', [function() { return r.Value() * Math.PI / 180 }, ip], { type: 'rotate' }); var rotatedf2 = board.create('curve', [f2, t], { strokeColor: '#2ECC40' }); // 调整文本位置避免超出画布 var text = board.create('text', [-2.5, -3, function() { var equationText = 'y=x^3'; return '$$' + equationText + '$$'; }], { fontSize: 14 }); })(); </script>
额外补充说明:
- 采样点数量(代码中为200)可按需调整,数量越多曲线越平滑
- 当滑块旋转至180°时,rotatedf2会与f1在x>0的部分完全重合,直观展示三次函数在原点的旋转对称性
- 保留了你原有的样式与交互设置,仅修改了核心的图形生成逻辑
内容的提问来源于stack exchange,提问作者Chris Munro
相关产品推荐
相关产品推荐

