正剧开始:
星历2016年02月02日 12:08:32, 银河系厄尔斯星球中华帝国江南行省。
[工程师阿伟]正在和[机器小伟]一起研究三角形。
埃及宏伟的金字塔出现了。。。
阿伟给小伟做了一个临时的画三角形的工具,这个工具可以很方便的知道三角形的所有边长和角度:
<span style="font-size:18px;">function myDraw() { var config = new PlotConfiguration(); config.init(); config.setPreference(); config.setSector(1,1,1,1); config.axis2D(0, 0, 180); var tri = new Triangle(); tri.know3edges([6, 6, 6]); tri.draw(20); var s = tri.print(); plot.fillText(s, -200, 100, 500); } function Triangle() { this.edges = []; this.angles = []; //已知三条边 this.know3edges = function(edges) { this.edges = edges; //角度为弧度单位 //a边对应角 this.angles.push(Math.acos((edges[1]*edges[1] + edges[2]*edges[2]-edges[0]*edges[0])/(2*edges[1]*edges[2]))); //b边对应角 this.angles.push(Math.acos((edges[0]*edges[0] + edges[2]*edges[2]-edges[1]*edges[1])/(2*edges[0]*edges[2]))); //c边对应角 this.angles.push(Math.acos((edges[0]*edges[0] + edges[1]*edges[1]-edges[2]*edges[2])/(2*edges[0]*edges[1]))); } this.draw = function(scale) { scale = scale ? scale : 1; plot.save() .setLineWidth(3) .setStrokeStyle('red'); var x0 = 0, y0 = 0; var x1 = x0 + this.edges[0], y1 = y0; var x2 = x0 + this.edges[1] * Math.cos(-this.angles[2]), y2 = y0 + this.edges[1] * Math.sin(-this.angles[2]); //为了应对边长可能过大或过小的情况,绘图时引入缩放比例scale x0 = x0*scale, y0 = y0 * scale; x1 = x1 * scale, y1 = y1 * scale; x2 = x2 * scale, y2 = y2 * scale; plot.fillText('B', x0, y0, 20); plot.fillText('A', x1, y1, 20); plot.fillText('C', x2, y2, 20); plot.beginPath() .moveTo(x0, y0) .lineTo(x1, y1) .lineTo(x2, y2) .closePath() .stroke(); plot.restore(); } this.print = function() { var angleLabel = ['C', 'A', 'B']; var edgeLabel = ['ab', 'bc', 'ac']; var s = ''; for (var i = 0; i < 3; i++) { s += edgeLabel[i]+' : '; s += this.edges[i].toFixed(2)+' ; '; } for (var i = 0; i < 3; i++) { s += angleLabel[i]+' : '; s += (this.angles[i]*180/Math.PI).toFixed(2) + ' ; '; } return s; } }</span>
小伟最喜欢的是画画,这次画了这么多的三角形,他感到很开心。
本节到此结束,欲知后事如何,请看下回分解。