C#已知三点求圆方程算法

如果不赶时间可以自己推算出算式或者直接参考另一个博主的文章:

三点确定一个圆的计算方法_Ivan 的专栏-CSDN博客_三点确定一个圆

程序完整部分《C#已知三点求圆方程算法.rar》已经上传到CSDN,地址是:

C#已知三点求圆方程算法_C-C#代码类资源-CSDN下载

在这里贴出算法部分:

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();

        }
    }
}

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值