sin関数

広告

sin関数は指定した角度の正弦(サイン)を計算して返します。

指定した角度の正弦(サイン)を計算して返します。

パラメータ:
  rad  角度(ラジアン)
戻り値:
  正弦(サイン)の値

引数に指定した角度の正弦(サイン)を計算します。

trigonometric

上記の三角形を例にすると正弦(サイン)は次のように計算されます。

sinθ = a / h

なお角度の単位はラジアンです。360度は2πラジアンなので、角度degをラジアンradに変換するには次のように計算して下さい。

var rad = deg * (Math.PI / 180);

例として30度のサインを計算するには次のように記述します。

var val = Math.sin(30 * (Math.PI / 180));
サンプルコード

では簡単なサンプルで試してみます。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title>JavaScript テスト</title>
</head>
<body>

<script type="text/javascript" src="./js/script9_1.js">
</script>

</body>
</html>
function print(str){
  document.write(str + "<br />");
}

document.write("<p>");

print("sin30 = " + Math.sin(30 * (Math.PI / 180)));
print("sin45 = " + Math.sin(45 * (Math.PI / 180)));
print("sin60 = " + Math.sin(60 * (Math.PI / 180)));

document.write("</p>");

上記を実際にブラウザ見てみると次のように表示されます。

p9-1

( Written by Tatsuo Ikura )