正剧开始:
星历2016年03月17日 09:30:43, 银河系厄尔斯星球中华帝国江南行省。
[工程师阿伟]正在和[机器小伟]一起研究[分式]。
本来小伟想和[人叫板老师]一样写一写这种中间带横线的分式的,但后来还是放弃了,
因为觉得这样其实并不会带来方便,不管是从在页面上的排列,总体的效果,
还有以后的代数取值计算,等等。因为分式可以出现在系数上,指数上,还可以
有很多很多的层次。还是简明一点好了,所以小伟决定还是这样好了:
<span style="font-size:18px;">//例3 function myDraw(xGlobal, yGlobal) { var config = new PlotConfiguration(); config.init(); config.setPreference(); var r = 20; //config.setSector(1,1,1,1); //config.graphPaper2D(0, 0, r); //config.axis2D(0, 0,180); var mathText = new MathText(); var s = ['2/(3x)', 'x/(x-1)', '1/(5-3b)', '(x+y)/(x-y)', '', '(-25a^[2]bc^[3])/(15ab^[2]c)', '=-(5abc*5ac^[2])/(5abc*3b)', '=-5ac^[2]/(3b)', '', '(x^[2]-9)/(x^[2]+6x+9)', '=((x+3)(x-3))/(x+3)^[2]', '=(x-3)/(x+3)', '', '(6x^[2]-12xy+6y^[2])/(3x-3y)', '=6(x-y)^[2]/(3(x-y))', '=2(x-y)' ]; var x = 20, y=20; var len = s.length; for (var i = 0; i < len; i++) { if (s[i] == '') { if (x < 100) { x += 300; y-=30*3; } else { x = 20; y += 30; } } else { mathText.print(s[i], x, y); y+=30; } } }</span>
意思到了也就行了,这样写起来也方便。
<span style="font-size:18px;">/** * @usage 数学表达式,代数式的书写 * @author mw * @date 2016年03月12日 星期六 11:05:12 * @param * @return * */ function MathText() { //上标标记形式为...^[内容]... //分数不进行处理, 根式不进行处理,都转成指数式进行 //特殊数学符号设想加\[alpha]进行转义,待续 //可以进行指数上标代数式的书写 //可扩展下标,待续 this.setNormalFont = function() { plot.setFont("normal normal normal 24px Times Lt Std"); } this.setScriptFont = function() { plot.setFont("italic normal 800 16px Times New Roman"); } this.print = function(text, xPos, yPos) { xPos = xPos ? xPos : 0; yPos = yPos ? yPos : 0; plot.save(); var s = text ? text : ''; if (s != '') { s = s.replace('//', '÷'); } //字符串长度 var len = s.length; //不同字体大小设置在此 var r1 = 20; //单个字符暂存处 var c; //文本显示位置 var x = xPos, y = yPos; //正常文本暂存 var s0 = ''; //字符串打印长度 var measure; //设置正常字体 this.setNormalFont(); for (var i = 0; i < len; i++) { if (s[i] == '^') { //上标开始 //上标标记形式为...^[内容]... if (s0 != '') { //先把正常字符打印出 if (r1 != 20) { //字体字号大小还在上标状态 r1 = 20; this.setNormalFont(); } measure = plot.measureText(s0); plot.fillText(s0, x, y, measure); s0 = ''; x += measure; } var upperScript = ''; for (var j = i+1; s[j]!=']'; j++) { if (s[j] != '[') { upperScript+=s[j]; } } if (r1 != 10) {//正常字体状态,需要改为上标字体 r1 = 10; this.setScriptFont(); } measure = plot.measureText(upperScript); plot.fillText(upperScript, x, y-10, measure); x += 1.2*measure; //直接跳跃过上标字符区段 i = j; } else { c = s[i]; if (c == '*') { if (i > 0 && /[0-9]/.test(s[i-1]) && /[0-9]/.test(s[i+1])) { //对于乘号前后都是数字的情况,把乘号改成叉号 c = ' \u00D7 '; } else { //对于代数式中,乘号改为点号 c = ' \u00B7 '; } } s0 += c; } } if (s0 != '') { //先把正常字符打印出 if (r1 != 20) { //字体字号大小还在上标状态 r1 = 20; this.setNormalFont(); } measure = plot.measureText(s0); plot.fillText(s0, x, y, measure); x += measure; } plot.restore(); } }</span>
<span style="font-size:18px;">//例4 function myDraw(xGlobal, yGlobal) { var config = new PlotConfiguration(); config.init(); config.setPreference(); var r = 20; //config.setSector(1,1,1,1); //config.graphPaper2D(0, 0, r); //config.axis2D(0, 0,180); var mathText = new MathText(); var s = [ '3/(2a^[2]b)', '=(3*bc)/(2a^[2]b*bc)', '=(3bc)/(2a^[2]b^[2]c)', '', '(a-b)/(ab^[2]c)', '=((a-b)*2a)/(ab^[2]c*2a)', '=(2a^[2]-2ab)/(2a^[2]b^[2]c)', '', '(2x)/(x-5)', '=(2x(x+5))/((x-5)(x+5))', '=(2x^[2]+10x)/(x^[2]-25)', '', '(3x)/(x+5)', '=(3x(x-5))/((x+5)(x-5))', '=(3x^[2]-15x)/(x^[2]-25)' ]; var x = 20, y=40; var len = s.length; for (var i = 0; i < len; i++) { if (s[i] == '') { if (x < 100) { x += 300; y-=30*3; } else { x = 20; y += 30; } } else { mathText.print(s[i], x, y); y+=30; } } }</span>
<span style="font-size:18px;">//例1 function myDraw(xGlobal, yGlobal) { var config = new PlotConfiguration(); config.init(); config.setPreference(); var r = 20; //config.setSector(1,1,1,1); //config.graphPaper2D(0, 0, r); //config.axis2D(0, 0,180); var mathText = new MathText(); var s = [ '((4x)/(3y))*((y)/(2x^[3]))', '=(4xy)/(6x^[3]*y)', '=2/(3x^[2]', ' ', ' ', '((ab^[3]/2c^[2]))//((-5a^[2]b^[2])/(4cd))', '=((ab^[3])/(2c^[2]))*((4cd)/(-5a^[2]b^[2]))', '=-(4ab^[3]cd)/(10a^[2]b^[2]c^[2])', ]; var x = 20, y=40; var len = s.length; for (var i = 0; i < len; i++) { if (s[i] == '') { if (x < 100) { x += 300; y-=30*3; } else { x = 20; y += 30; } } else { mathText.print(s[i], x, y); y+=30; } } }</span>
<span style="font-size:18px;">//例9 function myDraw(xGlobal, yGlobal) { var config = new PlotConfiguration(); config.init(); config.setPreference(); var r = 20; //config.setSector(1,1,1,1); //config.graphPaper2D(0, 0, r); //config.axis2D(0, 0,180); var mathText = new MathText(); var s = [ 'a^[-2]//a^[5]', '=a^[-2-5]', '=a^[-7]', '', '(b^[3]/a^[2])^[-2]', '=b^[-6]/a^[-4]', '=a^[4]b^[-6]', '', '(a^[-1]b^[2])^[3]', '=a^[-3]b^[4]', ' ', '', 'a^[-2]b^[2]*(a^[2]b^[-2])^[-3]', '=a^[-2]b^[2]*(a^[-6]b^[6])', '=a^[-8]b^[8]' ]; var x = 20, y=40; var len = s.length; for (var i = 0; i < len; i++) { if (s[i] == '') { if (x < 100) { x += 300; y-=30*3; } else { x = 20; y += 30; } } else { mathText.print(s[i], x, y); y+=30; } } } </span>
<span style="font-size:18px;">function myDraw(xGlobal, yGlobal) { var config = new PlotConfiguration(); config.init(); config.setPreference(); var r = 20; //config.setSector(1,1,1,1); //config.graphPaper2D(0, 0, r); //config.axis2D(0, 0,180); var mathText = new MathText(); var s = [ 'a/b = c/d', '=> bc = ad', '=> a/c = b/d', '', 'a/b = c/d', '=> 1/(a/b) = 1/(c/d)', '=> b/a = d/c', '', 'a/b = c/d', '=> 1+a/b = 1+c/d', '=> (a+b)/b = (c+d)/d', '', 'a/b = c/d', '=> bc = ad', '=> bc-ad = ad-bc', '=> (a+b)(c-d) = (a-b)(c+d)' ]; var x = 20, y=40; var len = s.length; for (var i = 0; i < len; i++) { if (s[i] == '') { if (x < 100) { x += 300; y-=30*3; } else { x = 20; y += 30; } } else { mathText.print(s[i], x, y); y+=30; } } }</span>