Arrix 的个人资料Arrix照片日志列表更多 ![]() | 帮助 |
|
2008/7/17 C# 3.0 一行求方差某python程序员说用C#求方差需要五十行。于是我试了试 python C# Func<List<double>, double> variance = numbers => (from n in numbers select Math.Pow((n - numbers.Sum() / numbers.Count), 2)).Sum() / numbers.Count; Console.Write(variance(new List<double> { 1, 2, 5, 6 })); 我很久没干过这么无聊的事情了…… 2008/7/16 三角形拼图 这怎么可能?我看了半天没看出破绽,于是用Canvas + jQuery UI做了个网页拼图,拼了半天终于发现了其中的奥妙。 我讨厌剧透,所以贴出代码占位,以免想独立思考的同学意外看到答案。<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Missing Square?</title> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" src="ui.core.js"></script> <script type="text/javascript" src="ui.draggable.js"></script> <script type="text/javascript" src="missingsquare.js"></script> </head> <body> <div style="float:left"><img src="missingsquare.jpg"/></div> <div style="float:left; margin: 10px"><canvas id="canvas-grid" width="150px" height="150px"></canvas></div> </body> </html> missingsquare.js
$(init); var cellWidth = 18.5, cellHeight = 15;function init() {drawGrid(); drawTriangle1(); drawTriangle2(); drawPoly1(); drawPoly2(); $('#canvas-triangle1,#canvas-triangle2,#canvas-poly1,#canvas-poly2').draggable();} function drawGrid() { var cellCountX = 15, cellCountY = 15;var c = document.getElementById('canvas-grid'); c.width = cellWidth * cellCountX + 1; c.height = cellHeight * cellCountY + 1; var ctx = c.getContext('2d'); ctx.fillStyle = 'white';ctx.fillRect(0, 0, c.width, c.height); ctx.strokeStyle = 'rgb(127, 127, 127)';for (var i = 0; i <= cellCountX; i++) { ctx.moveTo(i * cellWidth, 0); ctx.lineTo(i * cellWidth, c.height); ctx.stroke(); } for (var i = 0; i <= cellCountX; i++) { ctx.moveTo(0, i * cellHeight); ctx.lineTo(c.width, i * cellHeight); ctx.stroke(); } } function drawTriangle1() {var canv = document.createElement('canvas'); document.body.appendChild(canv); canv.id = 'canvas-triangle1';canv.width = cellWidth * 5; canv.height = cellHeight * 2; var ctx = canv.getContext('2d'); ctx.strokeStyle = 'black'; ctx.fillStyle = 'rgba(4, 145, 110, .5)';ctx.beginPath(); ctx.moveTo(0, canv.height); ctx.lineTo(canv.width, canv.height); ctx.lineTo(canv.width, 0); ctx.fill(); ctx.closePath(); ctx.stroke(); } function drawTriangle2() {var canv = document.createElement('canvas'); document.body.appendChild(canv); canv.id = 'canvas-triangle2';canv.width = cellWidth * 8; canv.height = cellHeight * 3; var ctx = canv.getContext('2d'); ctx.strokeStyle = 'black'; ctx.fillStyle = 'rgba(194, 5, 3, .5)';ctx.beginPath(); ctx.moveTo(0, canv.height); ctx.lineTo(canv.width, canv.height); ctx.lineTo(canv.width, 0); ctx.fill(); ctx.closePath(); ctx.stroke(); } function drawPoly1() {var canv = document.createElement('canvas'); canv.id = 'canvas-poly1';document.body.appendChild(canv); canv.width = cellWidth * 5; canv.height = cellHeight * 2; var ctx = canv.getContext('2d'); ctx.strokeStyle = 'black'; ctx.fillStyle = 'rgba(224, 152, 49, .5)';ctx.beginPath(); ctx.moveTo(0, 0); ctx.lineTo(canv.width, 0); ctx.lineTo(canv.width, cellHeight * 1); ctx.lineTo(cellWidth * 2, cellHeight * 1); ctx.lineTo(cellWidth * 2, cellHeight * 2); ctx.lineTo(0, cellHeight * 2); ctx.fill(); ctx.closePath(); ctx.stroke(); } function drawPoly2() {var canv = document.createElement('canvas'); canv.id = 'canvas-poly2';document.body.appendChild(canv); canv.width = cellWidth * 5; canv.height = cellHeight * 2; var ctx = canv.getContext('2d'); ctx.strokeStyle = 'black'; ctx.fillStyle = 'rgba(115, 198, 71, .5)';ctx.beginPath(); ctx.moveTo(cellWidth * 2, 0); ctx.lineTo(cellWidth * 5, 0); ctx.lineTo(cellWidth * 5, cellHeight * 2); ctx.lineTo(0, cellHeight * 2); ctx.lineTo(0, cellHeight * 1); ctx.lineTo(cellWidth * 2, cellHeight * 1); ctx.fill(); ctx.closePath(); ctx.stroke(); }
貌似三角形,并非三角形。简单起见,假设格子是边长为1的正方形。红绿两三角形直角边比一个是3:8 一个是2:5 ,不相似,锐角也不等,所以接在一起两条斜边实际上是有夹角的,这样便围成了一个狭长的三角形。
(define (hypo a b) (triangle-area (hypo 8 3) (hypo 5 2) (hypo 13 5)) ;0.4999999999994106 实际上刚好等于0.5。我还在想为什么…… 所以上下两个图形的面积差为2 * 0.5 = 1,这就解释了那个空格。
总结这个quiz非常巧妙,故意用扁长的格子,使红绿三角形顶角差异更小,组合图形的“斜边”夹角更加难以察觉,看上去完全就像是一条直线。 人总觉得“眼见为实”,看起来觉得是直角三角形便毫不怀疑,结果陷入困境。 反思一下,解决类似矛盾时,怎么才能避免落入思考的陷阱,明察秋毫呢?
|
|
|