(window.onload = function() {var mouseX, mouseY;var r = 36;var s = Math.sqrt(3);var t = Math.sqrt(-3*r*r+4*r*r*Math.sqrt(3));var p = new Array( r, 3*r, r, r, 3*r, 3*r, 3*r, r, 5*r, 3*r, r+r*s, 2*r, r+2*r*s, 3*r, r+2*r*s, r, 2*r, 3*r-r*s, 4*r, 3*r-r*s, 2*r+t, r, 3*r+t, r+r*s, 4*r+t, r, 3*r+2*t, 3*r, 4*r+2*t, 3*r-r*s, 5*r+2*t, 3*r );var flag = 0x0001; draw();function draw() { var canvas = document.getElementById('cvs'); if ( ! canvas || ! canvas.getContext ) return false; var ctx = canvas.getContext('2d'); display(); function near(x,y) { x-=10; y-=10; var a, b, c, m; switch ( flag ) { case 0x0001: a = (p[2]-x)*(p[2]-x)+(p[3]-y)*(p[3]-y); b = (p[4]-x)*(p[4]-x)+(p[5]-y)*(p[5]-y); m = Math.min(Math.min(a, b), r*r); if ( m == a ) return 1; if ( m == b ) return 2; break; case 0x0003: a = (p[4]-x)*(p[4]-x)+(p[5]-y)*(p[5]-y); b = (p[6]-x)*(p[6]-x)+(p[7]-y)*(p[7]-y); c = (p[10]-x)*(p[10]-x)+(p[11]-y)*(p[11]-y); m = Math.min(Math.min(Math.min(a, b), c), r*r); if ( m == a ) return 2; if ( m == b ) return 3; if ( m == c ) return 5; break; case 0x0005: a = (p[2]-x)*(p[2]-x)+(p[3]-y)*(p[3]-y); b = (p[16]-x)*(p[16]-x)+(p[17]-y)*(p[17]-y); m = Math.min(Math.min(a, b), r*r); if ( m == a ) return 1; if ( m == b ) return 8; break; case 0x0007: a = (p[6]-x)*(p[6]-x)+(p[7]-y)*(p[7]-y); m = Math.min(a, r*r); if ( m == a ) return 3; break; case 0x000B: a = (p[4]-x)*(p[4]-x)+(p[5]-y)*(p[5]-y); m = Math.min(a, r*r); if ( m == a ) return 2; break; case 0x0023: a = (p[12]-x)*(p[12]-x)+(p[13]-y)*(p[13]-y); b = (p[14]-x)*(p[14]-x)+(p[15]-y)*(p[15]-y); m = Math.min(Math.min(a, b), r*r); if ( m == a ) return 6; if ( m == b ) return 7; break; case 0x0063: a = (p[14]-x)*(p[14]-x)+(p[15]-y)*(p[15]-y); m = Math.min(a, r*r); if ( m == a ) return 7; break; case 0x00A3: a = (p[12]-x)*(p[12]-x)+(p[13]-y)*(p[13]-y); m = Math.min(a, r*r); if ( m == a ) return 6; break; case 0x0105: a = (p[8]-x)*(p[8]-x)+(p[9]-y)*(p[9]-y); b = (p[18]-x)*(p[18]-x)+(p[19]-y)*(p[19]-y); c = (p[20]-x)*(p[20]-x)+(p[21]-y)*(p[21]-y); m = Math.min(Math.min(Math.min(a, b), c), r*r); if ( m == a ) return 4; if ( m == b ) return 9; if ( m == c ) return 10; break; case 0x0115: a = (p[18]-x)*(p[18]-x)+(p[19]-y)*(p[19]-y); m = Math.min(a, r*r); if ( m == a ) return 9; break; case 0x0305: a = (p[8]-x)*(p[8]-x)+(p[9]-y)*(p[9]-y); m = Math.min(a, r*r); if ( m == a ) return 4; break; case 0x0505: a = (p[22]-x)*(p[22]-x)+(p[23]-y)*(p[23]-y); b = (p[24]-x)*(p[24]-x)+(p[25]-y)*(p[25]-y); m = Math.min(Math.min(a, b), r*r); if ( m == a ) return 11; if ( m == b ) return 12; break; case 0x0D05: a = (p[24]-x)*(p[24]-x)+(p[25]-y)*(p[25]-y); b = (p[26]-x)*(p[26]-x)+(p[27]-y)*(p[27]-y); m = Math.min(Math.min(a, b), r*r); if ( m == a ) return 12; if ( m == b ) return 13; break; case 0x1505: a = (p[22]-x)*(p[22]-x)+(p[23]-y)*(p[23]-y); m = Math.min(a, r*r); if ( m == a ) return 11; break; case 0x1D05: a = (p[26]-x)*(p[26]-x)+(p[27]-y)*(p[27]-y); m = Math.min(a, r*r); if ( m == a ) return 13; break; case 0x2D05: a = (p[24]-x)*(p[24]-x)+(p[25]-y)*(p[25]-y); b = (p[30]-x)*(p[30]-x)+(p[31]-y)*(p[31]-y); m = Math.min(Math.min(a, b), r*r); if ( m == a ) return 12; if ( m == b ) return 15; break; case 0x3D05: a = (p[28]-x)*(p[28]-x)+(p[29]-y)*(p[29]-y); b = (p[30]-x)*(p[30]-x)+(p[31]-y)*(p[31]-y); m = Math.min(Math.min(a, b), r*r); if ( m == a ) return 14; if ( m == b ) return 15; break; case 0x7D05: a = (p[30]-x)*(p[30]-x)+(p[31]-y)*(p[31]-y); m = Math.min(a, r*r); if ( m == a ) return 15; break; case 0xAD05: a = (p[24]-x)*(p[24]-x)+(p[25]-y)*(p[25]-y); b = (p[28]-x)*(p[28]-x)+(p[29]-y)*(p[29]-y); m = Math.min(Math.min(a, b), r*r); if ( m == a ) return 12; if ( m == b ) return 14; break; case 0xBD05: a = (p[28]-x)*(p[28]-x)+(p[29]-y)*(p[29]-y); m = Math.min(a, r*r); if ( m == a ) return 14; break; case 0xED05: a = (p[24]-x)*(p[24]-x)+(p[25]-y)*(p[25]-y); m = Math.min(a, r*r); if ( m == a ) return 12; break; case 0x000F: case 0x00E3: case 0x0315: case 0xFD05: return -2; } return -1; } function display() { eval('ctx.fillS'+'tyle = \'#FFFFFF\';'); ctx.fillRect(0,0,400,400); eval('ctx.strokeS'+'tyle = \'#000000\';'); var a, b, c, d, w, x, f; f = flag; switch ( flag ) { case 0x000F: a = 0; b = 2; c = '2×0.5'; d = '4×0.5'; w = p[b*2]-p[a*2]; x = Math.floor((1000*r-2*p[a*2])/w); w += 1000*r-x*w; break; case 0x00E3: a = 0; b = 6; c = '2×0.5'; d = '(1+4×0.5)'; w = p[b*2]-p[a*2]; x = Math.floor((1000*r-2*p[a*2])/w); w += 1000*r-x*w; break; case 0x0315: a = 0; b = 2; c = 0.5; d = '(1+2×0.5)'; w = p[b*2]-p[a*2]; x = Math.floor((1000*r-2*p[a*2])/w); w += 1000*r-x*w; f = 0x0105; break; case 0xFD05: a = 8; b = 14; c = '(1+0.5)'; d = '(5+2×0.5)'; w = p[b*2]-p[a*2]; x = Math.floor((1000*r-2*p[a*2])/w); w += 1000*r-x*w; break; default: w = 400; x = 0; break; } eval('ctx.fillS'+'tyle = \'#FFBBBB\';'); ctx.beginPath(); ctx.moveTo(p[a*2]+10, 10); ctx.lineTo(10, 10); ctx.lineTo(10, 4*r+10); ctx.lineTo(p[a*2]+10, 4*r+10); ctx.closePath(); ctx.fill(); ctx.beginPath(); ctx.moveTo(w+10, 10); ctx.lineTo(p[b*2]+10, 10); ctx.lineTo(p[b*2]+10, 4*r+10); ctx.lineTo(w+10, 4*r+10); ctx.closePath(); ctx.fill(); eval('ctx.fillS'+'tyle = \'#BBBBFF\';'); ctx.beginPath(); ctx.moveTo(p[b*2]+10, 10); ctx.lineTo(p[a*2]+10, 10); ctx.lineTo(p[a*2]+10, 4*r+10); ctx.lineTo(p[b*2]+10, 4*r+10); ctx.closePath(); ctx.fill(); var i, j; for (i=0, j=1; i!=16; i++, j*=2) { if ( Math.floor(f/j)%2 == 1 ) { if ( w == 400 ) { eval('ctx.fillS'+'tyle = \'#00FF00\';'); ctx.beginPath(); ctx.arc(p[i*2]+10, p[i*2+1]+10, r, 0, 2*Math.PI, false); ctx.closePath(); ctx.fill(); } else if ( p[i*2] == p[a*2] ) { eval('ctx.fillS'+'tyle = \'#FF0000\';'); ctx.beginPath(); ctx.arc(p[i*2]+10, p[i*2+1]+10, r, Math.PI/2.0, 3.0*Math.PI/2.0, false); ctx.closePath(); ctx.fill(); eval('ctx.fillS'+'tyle = \'#0000FF\';'); ctx.beginPath(); ctx.arc(p[i*2]+10, p[i*2+1]+10, r, 3.0*Math.PI/2.0, 5.0*Math.PI/2.0, false); ctx.closePath(); ctx.fill(); } else if ( p[i*2] == p[b*2] ) { eval('ctx.fillS'+'tyle = \'#0000FF\';'); ctx.beginPath(); ctx.arc(p[i*2]+10, p[i*2+1]+10, r, Math.PI/2.0, 3.0*Math.PI/2.0, false); ctx.closePath(); ctx.fill(); eval('ctx.fillS'+'tyle = \'#FF0000\';'); ctx.beginPath(); ctx.arc(p[i*2]+10, p[i*2+1]+10, r, 3.0*Math.PI/2.0, 5.0*Math.PI/2.0, false); ctx.closePath(); ctx.fill(); } else if ( Math.max(p[a*2],p[i*2]) == Math.min(p[b*2],p[i*2]) ) { eval('ctx.fillS'+'tyle = \'#0000FF\';'); ctx.beginPath(); ctx.arc(p[i*2]+10, p[i*2+1]+10, r, 0, 2*Math.PI, false); ctx.closePath(); ctx.fill(); } else { eval('ctx.fillS'+'tyle = \'#FF0000\';'); ctx.beginPath(); ctx.arc(p[i*2]+10, p[i*2+1]+10, r, 0, 2*Math.PI, false); ctx.closePath(); ctx.fill(); } } } eval('ctx.strokeS'+'tyle = \'#000000\';'); ctx.lineWidth = 1.3; ctx.beginPath(); ctx.moveTo(w+10,10); ctx.lineTo(10,10); ctx.lineTo(10,154); ctx.lineTo(w+10,154); ctx.closePath(); ctx.stroke(); if ( w != 400 ) { ctx.font = '20pt sans-serif'; ctx.textAlign = 'center'; ctx.textBaseline = 'middle'; eval('ctx.fillS'+'tyle = \'#FF0000\';'); ctx.fillText(p[a*2]/r, p[a*2]/2.0+10, 4.5*r+10); var l = Math.floor(((p[b*2]-p[a*2])/r)*10000)/10000; eval('ctx.fillS'+'tyle = \'#0000FF\';'); ctx.fillText(l, (p[b*2]-p[a*2])/2.0+p[a*2]+10, 4.5*r+10); eval('ctx.strokeS'+'tyle = \'#000000\';'); var i = 180; var j = ctx.measureText(p[a*2]/r).width; var k = (p[a*2]-j)/2.0; ctx.moveTo(10+k+k*Math.cos(i*Math.PI/180.0), 4*r+10-0.5*r*Math.sin(i*Math.PI/180.0)); for (; i!=271; i++) ctx.lineTo(10+k+k*Math.cos(i*Math.PI/180.0), 4*r+10-0.5*r*Math.sin(i*Math.PI/180.0)); ctx.stroke(); ctx.moveTo(10+j+k+k*Math.cos(i*Math.PI/180.0), 4*r+10-0.5*r*Math.sin(i*Math.PI/180.0)); for (; i!=360; i++) ctx.lineTo(10+j+k+k*Math.cos(i*Math.PI/180.0), 4*r+10-0.5*r*Math.sin(i*Math.PI/180.0)); ctx.stroke(); var i = 180; var j = ctx.measureText(l).width; var k = (p[b*2]-p[a*2]-j)/2.0; ctx.moveTo(10+p[a*2]+k+k*Math.cos(i*Math.PI/180.0), 4*r+10-0.5*r*Math.sin(i*Math.PI/180.0)); for (; i!=271; i++) ctx.lineTo(10+p[a*2]+k+k*Math.cos(i*Math.PI/180.0), 4*r+10-0.5*r*Math.sin(i*Math.PI/180.0)); ctx.stroke(); ctx.moveTo(10+p[a*2]+j+k+k*Math.cos(i*Math.PI/180.0), 4*r+10-0.5*r*Math.sin(i*Math.PI/180.0)); for (; i!=360; i++) ctx.lineTo(10+p[a*2]+j+k+k*Math.cos(i*Math.PI/180.0), 4*r+10-0.5*r*Math.sin(i*Math.PI/180.0)); ctx.stroke(); ctx.textAlign = 'left'; var t = 10; var u = 210; eval('ctx.fillS'+'tyle = \'#000000\';'); ctx.fillText('[(1000-2×', t, u); t += ctx.measureText('[(1000-2×').width; eval('ctx.fillS'+'tyle = \'#FF0000\';'); ctx.fillText(p[a*2]/r, t, u); t += ctx.measureText(p[a*2]/r).width; eval('ctx.fillS'+'tyle = \'#000000\';'); ctx.fillText(')÷', t, u); t += ctx.measureText(')÷').width; eval('ctx.fillS'+'tyle = \'#0000FF\';'); ctx.fillText(l, t, u); t += ctx.measureText(l).width; eval('ctx.fillS'+'tyle = \'#000000\';'); ctx.fillText(']='+x, t, u); t = 10; u = 240; ctx.fillText('2×', t, u); t += ctx.measureText('2×').width; eval('ctx.fillS'+'tyle = \'#FF0000\';'); ctx.fillText(c, t, u); t += ctx.measureText(c).width; eval('ctx.fillS'+'tyle = \'#000000\';'); ctx.fillText('+'+x+'×', t, u); t += ctx.measureText('+'+x+'×').width; eval('ctx.fillS'+'tyle = \'#0000FF\';'); ctx.fillText(d, t, u); t += ctx.measureText(d).width; eval('ctx.fillS'+'tyle = \'#000000\';'); ctx.fillText('=', t, u); ctx.font = '120pt sans-serif'; ctx.textAlign = 'center'; l = eval(('2*'+c+'+'+d+'*x').replace('×','*').replace('×','*')); ctx.fillText(l, 200, 320); } } canvas.onmousemove = mouseMoveListner; function mouseMoveListner(e) { adjustXY(e); display(); i = near(mouseX, mouseY); if ( i != -1 ) { eval('ctx.fillS'+'tyle = \'#BBFFBB\';'); ctx.beginPath(); ctx.arc(p[i*2]+10, p[i*2+1]+10, r, 0, 2*Math.PI, false); ctx.closePath(); ctx.fill(); } } canvas.onmousedown = mouseDownListner; function mouseDownListner(e) { adjustXY(e); } canvas.onmouseup = mouseUpListner; function mouseUpListner(e) { adjustXY(e); var i = near(mouseX, mouseY); if ( i == -2 ) { flag = 0x0001; } else if ( i != -1 ) { flag += Math.floor(Math.pow(2,i)); } } function adjustXY(e) { var rect = e.target.getBoundingClientRect(); mouseX = e.clientX - rect.left; mouseY = e.clientY - rect.top; }}})();
縦4、横1000、深さnの箱に、半径1、高さnの円柱を出来るだけ詰めろ。
縦4、横1000、深さnの箱に、半径1、高さnの円柱を出来るだけ詰めろ。