C#已知三點求圓方程算法

如果不趕時間可以自己推算出算式或者直接參考另一個博主的文章:

https://blog.csdn.net/liyuanbhu/article/details/52891868#commentBox

程序完整部分《C#已知三點求圓方程算法.rar》已經上傳到CSDN,地址是:

https://download.csdn.net/download/weixin_42117950/10903015

在這裏貼出算法部分:

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 DrawCircle
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            FirstPoint = new List<double>();
            SecondPoint = new List<double>();
            ThridPoint = new List<double>();
            this.StartPosition = FormStartPosition.CenterScreen;//窗口居中
        }
        List<double> FirstPoint;
        List<double> SecondPoint;
        List<double> ThridPoint;

        private void btn_Calculate_Click(object sender, EventArgs e)
        {
          //先清除上一次儲存的數值
            FirstPoint.Clear();
            SecondPoint.Clear();
            ThridPoint.Clear();
          //把文本框的數值加入到數組中
            try
            {
                //第一點的X Y座標值
                FirstPoint.Add(double.Parse(tb_FirstPointX.Text));
                FirstPoint.Add(double.Parse(tb_FirstPointY.Text));
                //第二點的X Y座標值
                SecondPoint.Add(double.Parse(tb_SecondPointX.Text));
                SecondPoint.Add(double.Parse(tb_SecondPointY.Text));
                //第三點的X Y座標值
                ThridPoint.Add(double.Parse(tb_ThirdPointX.Text));
                ThridPoint.Add(double.Parse(tb_ThirdPointY.Text));
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message+"\n請重新輸入","提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
                return;
            }
          //算法內容
            double a = FirstPoint[0] - SecondPoint[0];// X1-X2
            double b = FirstPoint[1] - SecondPoint[1];//Y1-Y2
            double c = FirstPoint[0] - ThridPoint[0];//X1-X3
            double d = FirstPoint[1] - ThridPoint[1];//Y1-Y3
            double aa = Math.Pow(FirstPoint[0], 2) - Math.Pow(SecondPoint[0], 2);//X1^2-X2^2
            double bb = Math.Pow(SecondPoint[1], 2) - Math.Pow(FirstPoint[1], 2);//Y2^2-Y1^2
            double cc = Math.Pow(FirstPoint[0], 2) - Math.Pow(ThridPoint[0], 2);//X1^2-X3^2
            double dd = Math.Pow(ThridPoint[1], 2) - Math.Pow(FirstPoint[1], 2);//Y3^2-Y1^2
            double E = (aa-bb) / 2;
            double F = (cc-dd) / 2;
            double resultY = (a * F - c * E) / (a * d - b * c);
            double resultX = (F * b - E * d) / (b * c - a * d);
            double resultR = Math.Sqrt((Math.Pow((FirstPoint[0] - resultX), 2)) + (Math.Pow((FirstPoint[1] - resultY), 2)));

          //輸出圓心的座標和半徑值
            lb_Point.Text = "X座標爲:" + resultX.ToString() + ";  Y座標爲:" + resultY.ToString();
            lb_Radius.Text = resultR.ToString();

        }
    }
}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章