正剧开始:
星历2016年04月23日 11:00:22, 银河系厄尔斯星球中华帝国江南行省。
[工程师阿伟]正在和[机器小伟]一起研究[空间向量与立体几何]。
<span style="font-size:18px;">#例5 def dot(a, b): if (len(a) >= 3): return a[0]*b[0] +a[1]*b[1]+a[2]*b[2]; elif (len(a) >= 2): return a[0]*b[0] +a[1]*b[1]; else: return a*b; #向量的绝对值 def rAbs(a): if (len(a) >= 3): return math.sqrt(a[0]**2 +a[1]**2 + a[2]**2); elif (len(a) >= 2): return math.sqrt(a[0]**2 +a[1]**2); else: return a; def angle(a, b): return math.acos(dot(a,b)/(rAbs(a)*rAbs(b)))*180/math.pi; def tmp(): a = [0,-0.25,1]; b = [0, 0.25, 1]; print(angle(a, b)); </span>
<span style="font-size:18px;">#例6 >>> 90.0 def tmp(): a = [-0.5, -0.5, 0.5]; b = [1, 0, 1]; print(angle(a, b));</span>
<span style="font-size:18px;">//平面向量 if (1) { var r = 20; config.setSector(1,1,1,1); config.graphPaper2D(0, 0, r); config.axis2D(0, 0,190); //坐标轴设定 var scaleX = 2*r, scaleY = 2*r; var spaceX = 2, spaceY = 2; var xS = -10, xE = 10; var yS = -10, yE = 10; config.axisSpacing(xS, xE, spaceX, scaleX, 'X'); config.axisSpacing(yS, yE, spaceY, scaleY, 'Y'); var array = [[1, 2], [4, 5], [7,2], [-8, -5], [-5, 3], [-8, 3], [-8, 5], [-2, 5], [-2, -1], [7, -3], [3, 1]]; var size = array.length; var transform = new Transform(); var tmp = []; array = transform.scale(transform.translate(array, 0, 0), scaleX/spaceX, scaleY/spaceY); tmp = [].concat(array); vectorDraw(tmp, 'red'); tmp = [].concat(array); shape.pointDraw(tmp, 'orange', 1, 1); plot.setFillStyle('blue'); plot.fillText('向量图', -270, -170, 300); }</span>
<span style="font-size:18px;"> if (1) { var r = 20; config.setSector(1,1,1,1); config.graphPaper2D(0, 0, r); config.axis2D(0, 0,190); //坐标轴设定 var scaleX = 4*r, scaleY = 4*r; var spaceX = 2, spaceY = 2; var xS = -10, xE = 10; var yS = -10, yE = 10; config.axisSpacing(xS, xE, spaceX, scaleX, 'X'); config.axisSpacing(yS, yE, spaceY, scaleY, 'Y'); var array = []; for (var x = -2; x <= 2; x+=4) { for (var y = -2; y <= 2; y+=4) { for (var z = -2; z<=2; z+=4) { array.push([x,y,z]); } } } array = shape.xyzSort(array); var size = array.length; var array2D = []; for (var i = 0; i < size; i++) { array2D.push(shape.point3D(array[i][0], array[i][1], array[i][2])); } var transform = new Transform(); var tmp = []; array2D = transform.scale(transform.translate(array2D, 0, 0), scaleX/spaceX, scaleY/spaceY); tmp = [].concat(array2D); vectorDraw(tmp, 'red'); tmp = [].concat(array2D); shape.pointDraw(tmp, 'orange', 1, 1); plot.setFillStyle('blue'); plot.fillText('向量图', -270, -170, 300); }</span>
<span style="font-size:18px;">//点阵中任意两点的距离排序集 function distanceSort(pointArray) { //传入点阵列pointArray //格式为[[px1, py1], [px2, py2], ...] //document.write(pointArray.join(' , ')+'<br/>'); //对于pointArray中的每个点,求它与所有其它点的距离 //结果放入distanceArray //格式为[[点1序号,点2序号, 距离值]] var distanceArray = []; //点的数量 var size = pointArray.length; //临时变量 var distance = x1 = y1 = z1 = x2 = y2 = z2 = 0; var dimension = pointArray[0].length; //计算并压入距离 for (var i = 0; i < size; i++) { for (var j = i+1; j < size; j++) { x1 = pointArray[i][0]; y1 = pointArray[i][1]; x2 = pointArray[j][0]; y2 = pointArray[j][1]; if (dimension > 2) { //三维点的处理 z1 = pointArray[i][2]; z2 = pointArray[j][2]; distance = Math.sqrt(Math.pow(x1-x2, 2)+Math.pow(y1-y2, 2)+Math.pow(z1-z2, 2)); } else { distance = Math.sqrt(Math.pow(x1-x2, 2)+Math.pow(y1-y2, 2)); } //注意这里已经保证i < j //所以起始点序号必须要小于终点序号 //这是为了连接起始点和终点的直线不会重复 distanceArray.push([i, j, distance]); } } //对距离阵列排序 //排序权重:起始点序号 > 距离 > 终点序号 distanceArray.sort(function(a, b) { if (a[0] == b[0]) { if (Math.abs(a[2] - b[2]) < 0.000001) { return a[1]-b[1]; } else { return a[2]-b[2]; } } else { return a[0] - b[0]; } }); //document.write(distanceArray.join(' , ')+'<br/>'); return distanceArray; } if (1) { var r = 20; config.setSector(1,1,1,1); config.graphPaper2D(0, 0, r); config.axis2D(0, 0,190); //坐标轴设定 var scaleX = 4*r, scaleY = 4*r; var spaceX = 2, spaceY = 2; var xS = -10, xE = 10; var yS = -10, yE = 10; config.axisSpacing(xS, xE, spaceX, scaleX, 'X'); config.axisSpacing(yS, yE, spaceY, scaleY, 'Y'); var array = []; for (var x = -2; x <= 2; x+=4) { for (var y = -2; y <= 2; y+=4) { for (var z = -2; z<=2; z+=4) { array.push([x,y,z]); } } } //array = shape.xyzSort(array); var size = array.length; var array2D = []; for (var i = 0; i < size; i++) { array2D.push(shape.point3D(array[i][0], array[i][1], array[i][2])); } //去除重复点 var pointArray = removeDuplicatedPoint(array2D); //无重复的点的数量 var points = pointArray.length; //得到距离阵列 //格式为[[点1序号,点2序号, 距离值], ...] var distanceArray = distanceSort(pointArray); //边的数量 var edges = distanceArray.length; //存放需要连通的边 var linkedArray = []; //连通的边的数量 var links = 0; //每个顶点相关的边的集合 var edgeOfVertex = []; for (var i = 0; i < points; i++) { //获得顶点相关的边的集合 edgeOfVertex = []; for (var j = 0; j < edges; j++) { if (distanceArray[j][0] == i || distanceArray[j][1] == i) { edgeOfVertex.push(distanceArray[j]); } } //根据起始点寻找最短长度的两条边 edgeOfVertex.sort(function(a, b) { return a[2] - b[2]; }); var choice = 2; if (edgeOfVertex.length > choice) { edgeOfVertex = edgeOfVertex.slice(0, choice); } linkedArray = linkedArray.concat(edgeOfVertex); } //document.write(linkedArray.join(' , ')+'<br/>'); linkedArray = removeDuplicatedPoint(linkedArray); links = linkedArray.length; //document.write(linkedArray.join(' , ')+'<br/>'); var startPoint, endPoint, x1, y1, x2, y2; //比例缩放 var scale = 40; for (var i = 0; i < links; i++) { startPoint = linkedArray[i][0]; endPoint = linkedArray[i][1]; x1 = pointArray[startPoint][0]; y1 = pointArray[startPoint][1]; x2 = pointArray[endPoint][0]; y2 = pointArray[endPoint][1]; shape.vectorDraw([[x1,y1], [x2, y2]], 'red', scale); } shape.pointDraw(pointArray, 'blue', scale, 1); /* //array2D = shape.angularSort(array2D); var transform = new Transform(); var tmp = []; array2D = transform.scale(transform.translate(array2D, 0, 0), scaleX/spaceX, scaleY/spaceY); tmp = [].concat(array2D); vectorDraw(tmp, 'red'); tmp = [].concat(array2D); shape.pointDraw(tmp, 'orange', 1, 1); */ plot.setFillStyle('blue'); plot.fillText('向量图', -270, -170, 300); }</span>
<span style="font-size:18px;">array = [[0,0,0], [0, 0, 5], [5,0,5], [0, 5, 0], [0, 5, 5]];</span>
<span style="font-size:18px;">array = [[0,0,0], [0, 0, 5], [5,0,0], [0, 5, 0]];</span>
<span style="font-size:18px;">array = [[0,0,0], [0, 0, 5], [5,0,0], [0, 5, 0], [2, 2, 2], [2, 0, 2]];</span>
<span style="font-size:18px;">array = [[0,0,0], [0, 0, 5], [5,0,0], [0, 5, 0], [2, 2, 2], [2, 0, 2], [-7,3, -7]];</span>