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

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

image

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

戻る