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

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

image

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

戻る