高さと角度から、底辺と斜辺を計算するサンプル

  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. namespace WindowsFormsApplication1
  5. {
  6.     public partial class Form1 : Form
  7.     {
  8.         public Form1()
  9.         {
  10.             InitializeComponent();
  11.         }
  12.         private void button1_Click(object sender, EventArgs e)
  13.         {
  14.             if (!"".Equals(textBoxAngle.Text) && !"".Equals(textBoxHeightResult.Text))
  15.             {
  16.                 // 底辺
  17.                 textBoxBaseResult.Text = "" + calcBase(double.Parse(textBoxHeightResult.Text), double.Parse(textBoxAngle.Text));
  18.                 // 斜辺
  19.                 textBoxHypotenuse.Text = "" + calcHypotenuse(double.Parse(textBoxHeightResult.Text), double.Parse(textBoxAngle.Text));
  20.             } 
  21.         }
  22.         /** 
  23.          * 高さと角度から底辺を求める 
  24.          * @height 高さ
  25.          * @angle 角度
  26.          * @return 底辺
  27.          */
  28.         private double calcBase(double height, double angle)
  29.         {
  30.             return height / Math.Tan(Math.PI * angle / 180);
  31.         }
  32.         /** 
  33.          * 高さと角度から斜辺を求める
  34.          * @height 高さ
  35.          * @angle 角度
  36.          * @return 底辺
  37.          */
  38.         private double calcHypotenuse(double height, double angle)
  39.         {
  40.             return height / Math.Sin(Math.PI * angle / 180);
  41.         }
  42.     }
  43. }

image

実行ファイルダウンロード

戻る