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

  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(textBoxBase.Text) && !"".Equals(textBoxHypotenuse.Text))
  15.             {
  16.                 // 角度
  17.                 textBoxAngle.Text = "" + calcAngle(double.Parse(textBoxBase.Text), double.Parse(textBoxHypotenuse.Text));
  18.                 // 高さ
  19.                 textBoxHeight.Text = "" + calcHeight(double.Parse(textBoxBase.Text), double.Parse(textBoxHypotenuse.Text));
  20.             } 
  21.         }
  22.         /** 
  23.          * 底辺と斜辺から角度を求める 
  24.          * @_base 底辺
  25.          * @hypotenuse 斜辺
  26.          * @return 角度
  27.          */
  28.         private double calcAngle(double _base, double hypotenuse)
  29.         {
  30.             return Math.Acos(_base / hypotenuse) * 180 / Math.PI;
  31.         }
  32.         /** 
  33.          * 底辺と斜辺から高さを求める 
  34.          * @_base 底辺
  35.          * @hypotenuse 斜辺
  36.          * @return 高さ
  37.          */
  38.         private double calcHeight(double _base, double hypotenuse)
  39.         {
  40.             return Math.Sqrt(Math.Pow(hypotenuse, 2) - Math.Pow(_base, 2));
  41.         }
  42.     }
  43. }

image

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

戻る