当前位置:编程学习 > C#/ASP.NET >>

C#编程已知三个点求组成三角形的面积和周长

答案:举个例子,希望有所帮助。

代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TestWork
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            double x1 = Convert.ToDouble(this.textBox1.Text);
            double y1 = Convert.ToDouble(this.textBox2.Text);
            double x2 = Convert.ToDouble(this.textBox3.Text);
            double y2 = Convert.ToDouble(this.textBox4.Text);
            double x3 = Convert.ToDouble(this.textBox5.Text);
            double y3 = Convert.ToDouble(this.textBox6.Text);

            double a = System.Math.Sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
            double b = System.Math.Sqrt((x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3));
            double c = System.Math.Sqrt((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3));

            double L = a + b + c;
            double p = L / 2;
            double S = System.Math.Sqrt(p*(p - a)*(p - b)*(p - c));

            MessageBox.Show("三角形周长:" + L.ToString() + Environment.NewLine + "三角形面积:" + S.ToString(), "提示信息");               

        }
     

    }
}
结果

1行:INPUT   x1,y1   2行:INPUT   x2,y2  3行:x3,y3  4行:a=SQR[(x1-x2)^2+(y1-y2)^2]   5行:b=SQR[(x2-x3)^2+(y2-y3)^2]    6行:c=SQR[(x3-x1)^2+(y3-y1)^2]     7行:L=a+b+c     8行:P=L/2     9行:S=SQR[p(p-a)(p-b)(p-c)]    10行:PRINT  L,S  11行:END

上一个:为什么下面的 C# 编程代码运行 form界面没有显示呢??
下一个:请大家推荐一本C#编程书,要好的

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,