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

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

image

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

戻る