JavaScript 数値演算 新しいページはコチラ
提供: yonewiki
(→円周率 $ \pi $ Math.PI) |
(→三角関数 sin/cos/tan/asin/acos/atan/atan2) |
||
210行: | 210行: | ||
== 関数 == | == 関数 == | ||
=== 三角関数 sin/cos/tan/asin/acos/atan/atan2 === | === 三角関数 sin/cos/tan/asin/acos/atan/atan2 === | ||
+ | <yjavascript> | ||
+ | onload = function() { | ||
+ | draw2(); | ||
+ | |||
+ | }; | ||
+ | /* 円を描く */ | ||
+ | function draw2() { | ||
+ | var canvas = document.getElementById('c2'); | ||
+ | if ( ! canvas || ! canvas.getContext ) { return false; } | ||
+ | var ctx = canvas.getContext('2d'); | ||
+ | ctx.beginPath(); | ||
+ | ctx.arc(70, 70, 60, 0, Math.PI*2, false); | ||
+ | ctx.stroke(); | ||
+ | |||
+ | ctx.beginPath(); | ||
+ | ctx.moveTo(0, 70); | ||
+ | ctx.lineTo(610, 70); | ||
+ | ctx.stroke(); | ||
+ | |||
+ | ctx.beginPath(); | ||
+ | ctx.moveTo(70, 0); | ||
+ | ctx.lineTo(70, 140); | ||
+ | ctx.stroke(); | ||
+ | |||
+ | ctx.beginPath(); | ||
+ | ctx.moveTo(130, 0); | ||
+ | ctx.lineTo(130, 140); | ||
+ | ctx.stroke(); | ||
+ | |||
+ | ctx.beginPath(); | ||
+ | ctx.strokeText("sin x", 130 + 65, 70 - 60 * Math.sin(0) - 30); | ||
+ | |||
+ | ctx.moveTo(130, 70 - 60 * Math.sin(0)); | ||
+ | for (var i = 1; i <= 480; i += 1) { | ||
+ | ctx.lineTo(i + 130, 70 - 60 * Math.sin( Math.PI * i / 60)); | ||
+ | } | ||
+ | ctx.stroke(); | ||
+ | |||
+ | ctx.beginPath(); | ||
+ | ctx.moveTo(55 + 130, 70 - 60 * Math.sin(Math.PI * 55 / 60)); | ||
+ | ctx.lineTo(130 + 63, 70 - 60 * Math.sin(0) - 32); | ||
+ | ctx.stroke(); | ||
+ | |||
+ | |||
+ | ctx.beginPath(); | ||
+ | ctx.strokeStyle = 'rgb(192, 80, 77)'; | ||
+ | |||
+ | ctx.moveTo(130, 70 - 60 * Math.cos(0)); | ||
+ | ctx.strokeText("cos x", 130 + 50, 70 - 60 * Math.cos(0) + 10); | ||
+ | |||
+ | for (var i = 1; i <= 480; i += 1) { | ||
+ | ctx.lineTo(i + 130, 70 - 60 * Math.cos( Math.PI * i / 60)); | ||
+ | } | ||
+ | ctx.stroke(); | ||
+ | |||
+ | ctx.beginPath(); | ||
+ | ctx.strokeStyle = 'rgb(192, 80, 77)'; | ||
+ | ctx.moveTo(20 + 130, 70 - 60 * Math.cos(Math.PI * 20 / 60)); | ||
+ | ctx.lineTo(130 + 48, 70 - 60 * Math.cos(0) + 8 ); | ||
+ | ctx.stroke(); | ||
+ | |||
+ | } | ||
+ | </yjavascript> | ||
=== 最小値・最大値 max/min === | === 最小値・最大値 max/min === |