正剧开始:
星历2016年03月09日 10:38:21, 银河系厄尔斯星球中华帝国江南行省。
[工程师阿伟]正在和[机器小伟]一起研究[三角形]。
这是小伟进入筑基中期后遇到的第一部分功法。
怎样确定这个垂足点,其实对于小伟来说也不算什么,但考虑到现在的功法并不要求精确的解,[人叫板老师]只是泛泛地谈。
而这些线画多了会很乱,小伟也就没有了深入的兴趣了。先放着吧。
这些线都会有交点的,但是如果三条高,三条中线,再加三条角平分线同上画上,这个三角形就太挤了。
不过小伟早就想要一个能自己算角度的函数了,这下可找到机会了。
<span style="font-size:18px;">function myDraw(xGlobal, yGlobal) { var config = new PlotConfiguration(); config.init(); config.setPreference(); var r = 40; config.setSector(1,1,1,1); config.graphPaper2D(0, 0, r); config.axis2D(0, 0,180); r/=10; var a0 = [[10, 0], [0, 10], [-20, 20], [5, -5]]; var len = a0.length; for (var i = 0; i < len; i++) { for (var j = 0; j < 2; j++) { a0[i][j]*=r; } } var a = shape.angularSort(a0); var d1, d2, d3, angle; var x1,y1, x2, y2, x3, y3; var s; for (var i = 0; i < len; i++) { if (i == 0) { x1 = a[len-1][0]; y1 = a[len-1][1]; x3 = a[i+1][0]; y3 = a[i+1][1]; } else if (i == len-1) { x1 = a[i-1][0]; y1 = a[i-1][1]; x3 = a[0][0]; y3 = a[0][1]; } else { x1 = a[i-1][0]; y1 = a[i-1][1]; x3 = a[i+1][0]; y3 = a[i+1][1]; } x2 = a[i][0]; y2 = a[i][1]; d1 = (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2); d2 = (x2-x3)*(x2-x3)+(y2-y3)*(y2-y3); d3 = (x1-x3)*(x1-x3)+(y1-y3)*(y1-y3); angle = Math.acos((d1+d2-d3)/(2*Math.sqrt(d1*d2)))/Math.PI*180; s = angle.toFixed(2)+'°'; //document.write(s+'<p>'); plot.fillText(s, x2, -y2-5, 100); } var tmp = [].concat(a); shape.pointDraw(tmp); tmp = [].concat(a); shape.strokeDraw(tmp); }</span>
<span style="font-size:18px;">>>> 22.62+78.69+78.69 180.0</span>
<span style="font-size:18px;">>>> 18.43+161.57+90+90 360.0</span>
真是太方便了。小伟很满意。
<span style="font-size:18px;">function myDraw(xGlobal, yGlobal) { var config = new PlotConfiguration(); config.init(); config.setPreference(); var r = 40; config.setSector(1,1,1,1); config.graphPaper2D(0, 0, r); config.axis2D(0, 0,180); r/=5; var a0 = [[10, 0], [0, -10], [-20, 20], [15, -15], [6, 12], [18, -6]]; var len = a0.length; for (var i = 0; i < len; i++) { for (var j = 0; j < 2; j++) { a0[i][j]*=r; } } var a = shape.angularSort(a0); var d1, d2, d3, angle; var x1,y1, x2, y2, x3, y3; var s; for (var i = 0; i < len; i++) { if (i == 0) { x1 = a[len-1][0]; y1 = a[len-1][1]; x3 = a[i+1][0]; y3 = a[i+1][1]; } else if (i == len-1) { x1 = a[i-1][0]; y1 = a[i-1][1]; x3 = a[0][0]; y3 = a[0][1]; } else { x1 = a[i-1][0]; y1 = a[i-1][1]; x3 = a[i+1][0]; y3 = a[i+1][1]; } x2 = a[i][0]; y2 = a[i][1]; d1 = (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2); d2 = (x2-x3)*(x2-x3)+(y2-y3)*(y2-y3); d3 = (x1-x3)*(x1-x3)+(y1-y3)*(y1-y3); angle = Math.acos((d1+d2-d3)/(2*Math.sqrt(d1*d2)))/Math.PI*180; s = angle.toFixed(2)+'°'; //document.write(s+'<p>'); plot.fillText(s, x2, -y2-5, 100); } var tmp = [].concat(a); shape.pointDraw(tmp); tmp = [].concat(a); shape.strokeDraw(tmp); }</span>
<span style="font-size:18px;">>>> 125.54+39.21+142.13+90+108.43+145.3 650.61</span>
<span style="font-size:18px;">>>> 125.54+39.21+142.13+90+108.43+360-145.3 720.01</span>
接着,小伟把它做成了工具,以后可以直接调用了:
<span style="font-size:18px;">this.angleDraw = function(array, scale) { //array是一个存放二维坐标点序列的数组 var a0 = [].concat(array); scale = scale ? scale : 1; var len = a0.length; if (scale != 1 && scale > 0) { for (var i = 0; i < len; i++) { for (var j = 0; j < 2; j++) { a0[i][j]*=scale; } } } //进行环状排序,这样传入的array就可以任意顺序放置坐标点。 var a = this.angularSort(a0); var d1, d2, d3, angle; var x1,y1, x2, y2, x3, y3; var s; //坐标点编号 var s0 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; //为每个点利用余弦定理求角 for (var i = 0; i < len; i++) { if (i == 0) { x1 = a[len-1][0]; y1 = a[len-1][1]; x3 = a[i+1][0]; y3 = a[i+1][1]; } else if (i == len-1) { x1 = a[i-1][0]; y1 = a[i-1][1]; x3 = a[0][0]; y3 = a[0][1]; } else { x1 = a[i-1][0]; y1 = a[i-1][1]; x3 = a[i+1][0]; y3 = a[i+1][1]; } x2 = a[i][0]; y2 = a[i][1]; d1 = (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2); d2 = (x2-x3)*(x2-x3)+(y2-y3)*(y2-y3); d3 = (x1-x3)*(x1-x3)+(y1-y3)*(y1-y3); angle = Math.acos((d1+d2-d3)/(2*Math.sqrt(d1*d2)))/Math.PI*180; s = angle.toFixed(2)+'°'; //document.write(s+'<p>'); //标注角度和顶点编号 plot.fillText(s, x2, -y2-5, 100); plot.fillText(s0[i], x2, -y2+20, 20); } //分两次绘点和连线 var tmp = [].concat(a); this.pointDraw(tmp); tmp = [].concat(a); this.strokeDraw(tmp); }</span>
<span style="font-size:18px;">>>> 145.3+33.69+93.18+90+108.43 470.6</span>
<span style="font-size:18px;">>>> 360-145.3+33.69+93.18+90+108.43 540.0</span>
<span style="font-size:18px;">function myDraw(xGlobal, yGlobal) { var config = new PlotConfiguration(); config.init(); config.setPreference(); var r = 50; config.setSector(1,1,1,1); config.graphPaper2D(0, 0, r); config.axis2D(0, 0,180); r/=5; var a0 = [[10, 0], [0, -10], [15, -15], [6, 12], [18, -6]]; shape.angleDraw(a0, r); }</span>
本节到此结束,欲知后事如何,请看下回分解。