正剧开始:
星历2016年02月23日 11:53:25, 银河系厄尔斯星球中华帝国江南行省。
[工程师阿伟]正在和[机器小伟]一起复习着[图形与几何]。
<span style="font-size:18px;">function myDraw() { var config = new PlotConfiguration(); config.init(); config.setPreference(); config.setSector(1,1,1,1); config.axis3D(0, 0, 0, 180); var array = []; for (var z = 0; z < 2; z++) { for (var y = 0; y < 3; y++) { for (var x = 0; x < 2; x++) { if (z == 0 && y == 2) {} else { array.push([x, y, z]); } } } } shape.threeView(array); plot.fillText('共有'+array.length.toFixed(0)+'个', 50, 100, 100); } /** * @usage 把三维点阵列按照z, y, x优先级由小到大排列 * @author mw * @date 2016年02月23日 星期二 09:38:27 * @param [[x1, y1, z1], [x2,y2, z2], ...] * @return 排序后的[[x, y, z]...] * */ this.xyzSort = function(array) { var arr = new Array(); arr = array; arr.sort(function(a, b) { if (a[2] != b[2]) { return a[2] - b[2]; } else { if (a[1] != b[1]) { return (a[1] - b[1]); } else { return a[0] - b[0]; } } }); //document.write(arr); return arr; } /** * @usage 三视图 * @author mw * @date 2016年02月23日 星期二 09:49:23 * @param * @return * */ this.threeView = function(array) { var cubic = this.xyzSort(array); plot.save(); plot.setTransform(1, 0, 0, 1, 0, 0) .translate(300, 200); //三维图和三视图 var r = 50; var style = 'red'; var len = cubic.length; for (var i = 0; i < len; i++) { this.drawCubic(cubic[i][0], cubic[i][1], cubic[i][2], r, style); } var height = 400; r = r/3; plot.setTransform(1, 0, 0, 1, 0, 0); plot.fillText('左视图', 20, 20, 100); plot.fillText('主视图', 20, 20+1*height/3, 100); plot.fillText('俯视图', 20, 20+2*height/3, 100); plot.setFillStyle(style) .setStrokeStyle('white'); //左视图 plot.translate(100, 80); for (var i = 0; i < len; i++) { //y, z两坐标,z坐标变为x坐标 this.fillRect(cubic[i][2]*r, -cubic[i][1]*r, r, r); this.strokeRect(cubic[i][2]*r, -cubic[i][1]*r, r, r); } //主视图 plot.translate(0, 130); for (var i = 0; i < len; i++) { //x, y两坐标 this.fillRect(cubic[i][0]*r, -cubic[i][1]*r, r, r); this.strokeRect(cubic[i][0]*r, -cubic[i][1]*r, r, r); } //俯视图 plot.translate(0, 100); for (var i = 0; i < len; i++) { //x, z两坐标,z坐标变为y坐标 this.fillRect(cubic[i][0]*r, cubic[i][2]*r, r, r); this.strokeRect(cubic[i][0]*r, cubic[i][2]*r, r, r); } plot.restore(); }</span>
<span style="font-size:18px;">#15 Scubic = lambda a : a*a; Vcubic = lambda a : a*a*a; Scylinder = lambda d, h : 3.1416/4*d*d*2+3.1416*d*h; Vcylinder = lambda d, h : 3.1416/4*d*d*h; >>> Scubic = lambda a : a*a*6; >>> Vcubic = lambda a : a*a*a; >>> Scylinder = lambda d, h : 3.1416/4*d*d*2+3.1416*d*h; >>> Vcylinder = lambda d, h : 3.1416/4*d*d*h; >>> S = Scubic(20)*5/6+Scylinder(20, 20)/2; >>> S 2942.48 >>> V = Vcubic(20)+Vcylinder(20, 20)/2; >>> V 11141.6</span>
<span style="font-size:18px;">//16题 function myDraw() { var config = new PlotConfiguration(); config.init(); config.setPreference(); config.setSector(1,1,1,1); config.axis3D(0, 0, 0, 180); var array = []; for (var z = 0; z < 3; z++) { for (var y = 0; y < 1; y++) { for (var x = 0; x < 3; x++) { if (z == 0 && x == 2) {} else { array.push([x, y, z]); } } } } array.push([0, 1, 0]); array.push([0, 1, 1]); shape.threeView(array, 'green'); plot.fillText('共有'+array.length.toFixed(0)+'个', 50, 100, 100); }</span>
<span style="font-size:18px;">[0, 0, 0]有3面涂色。 [1, 0, 0]有4面涂色。 [0, 0, 1]有2面涂色。 [1, 0, 1]有2面涂色。 [2, 0, 1]有4面涂色。 [0, 0, 2]有4面涂色。 [1, 0, 2]有3面涂色。 [2, 0, 2]有4面涂色。 [0, 1, 0]有4面涂色。 [0, 1, 1]有4面涂色。 总计两面涂色的有2个, 三面涂色的有2个,四面涂色的有6个。 #16 正方体着色统计 def tmp(): a = []; for z in range(3): for y in range(1): for x in range(3): if z == 0 and x == 2: pass; else: a.append([x, y, z]); a.append([0, 1, 0]); a.append([0, 1, 1]); #print(a); size = len(a); b = [6]*size; #print(b); for i in range(size): for j in range(i, size): xdiff = abs(a[i][0] - a[j][0]); ydiff = abs(a[i][1] - a[j][1]); zdiff = abs(a[i][2] - a[j][2]); #对于相邻的立方体只有一个坐标相差1,其余都相同。 if (xdiff + ydiff + zdiff == 1): b[i]-=1; b[j]-=1; #统计n面涂色的数量。 b2 = 0; b3 = 0; b4 = 0; for i in range(size): if (b[i] == 2): b2+=1; elif (b[i] == 3): b3+=1; elif (b[i] == 4): b4+=1; print('{0}有{1}面涂色。'.format(a[i], b[i])); print('总计两面涂色的有{0}个, 三面涂色的有{1}个,四面涂色的有{2}个。'.format(\ b2, b3, b4)); </span>
<span style="font-size:18px;">//19题 function myDraw() { var config = new PlotConfiguration(); config.init(); config.setPreference(); config.setSector(1,1,1,1); config.axis3D(0, 0, 0, 180); var array = []; for (var z = 0; z < 2; z++) { for (var y = 0; y < 3; y++) { for (var x = 0; x < 2; x++) { if (x == 1 && y == 2) {} else if (x == 1 && y == 1 && z == 1) {} else { array.push([x, y, z]); } } } } shape.threeView(array, 'green'); plot.fillText('共有'+array.length.toFixed(0)+'个', 50, 100, 100); }</span>
<span style="font-size:18px;">//题3(1) function myDraw() { var config = new PlotConfiguration(); config.init(); config.setPreference(); config.setSector(1,1,1,1); config.axis2D(0, 0, 180); var r1 = 100, r2 = r1/2; var a = [[1,1], [1,-1], [-1,-1], [-1,1]]; plot.setStrokeStyle('red'); for (var i = 0; i < 4; i++) { shape.strokeCircle(a[i][0]*r2*0.707, a[i][1]*r2*0.707, r2); } shape.strokeCircle(0, 0, r1); }</span>
<span style="font-size:18px;">//题3(2) function myDraw() { var config = new PlotConfiguration(); config.init(); config.setPreference(); config.setSector(1,1,1,1); config.axis2D(0, 0, 180); var r1 = 100; plot.setStrokeStyle('red'); shape.strokeCircle(0, 0, r1); shape.strokeDraw(shape.nEdge(0, 0, r1, 6), 'red'); shape.strokeDraw(shape.nEdge(0, 0, r1, 3, -Math.PI/2), 'red'); var x1, y1, x2, y2, thita = 0; for (var i = 0; i < 3; i++) { x1 = r1*Math.cos(thita-Math.PI), y1 = r1*Math.sin(thita-Math.PI); x2 = r1*Math.cos(thita), y2 = r1*Math.sin(thita); plot.beginPath() .moveTo(x1, y1) .lineTo(x2, y2) .closePath() .stroke(); thita += Math.PI*2/3; } } </span>
<span style="font-size:18px;">//题3(3) function myDraw() { var config = new PlotConfiguration(); config.init(); config.setPreference(); config.setSector(1,1,1,1); config.axis2D(0, 0, 180); var r1 = 100, r2 = r1/2; plot.setStrokeStyle('red'); shape.strokeCircle(0, 0, r1); var sAngle = Math.PI/2, eAngle = sAngle-Math.PI; plot.beginPath() .arc(0, r2, r2, sAngle, eAngle, 1) .moveTo(0,0) .closePath() .stroke(); sAngle = -Math.PI/2, eAngle = sAngle+Math.PI; plot.beginPath() .arc(0, -r2, r2, sAngle, eAngle, 1) .moveTo(0,0) .closePath() .stroke(); } </span>
<span style="font-size:18px;">>>> import math; >>> dis = lambda x1, y1, x2, y2 : math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); >>> dis(5,0,8,9)*50 474.3416490252569 >>> angle = lambda x1, y1, x2, y2 : math.atan((y1-y2)/(x1-x2))*180/3.1416; >>> 90-angle(5,0,8,9) 18.435116172750895 >>> dis(5,0,10,3); 5.830951894845301 >>> _*50 291.54759474226506 >>> angle(5,0,10,3); 30.963684125510934 >>> 90-_ 59.03631587448906</span>