⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.cpp

📁 输入X,Y座标
💻 CPP
字号:
//---------------------------------------------------------------------------

#include <vcl.h>
#include "math.h"
#pragma hdrstop

#include "Unit1.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
struct circle
{
    int Cx;
    int Cy;
    int R;
};
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
struct circle fit_circle(int x[],int y[],int count)
{
    struct circle ret;
    float M[10],D[4];  // array
    float sX=0,sY=0,sXY=0,sX2=0,sX3=0,sX4=0,sX2Y=0,dived;
    float x1,y1,x2,y2,a1,b1,c1;
    float z1=0,z2=0,z3=0,sY2=0;
    float r,a,b,c;
    byte* ptr;
    int i,j,n,index=0;
    sX=0,sY=0,sXY=0,sX2=0,sX3=0,sX4=0,sX2Y=0;
    z1=0,z2=0,z3=0,r,sY2=0;
    index=0;

    for (int i=0;i< count  ;i++)
    {
        sX += x[i] ;
        sY += y[i] ;
        sX2 += x[i]*x[i] ;
        sXY += x[i]*y[i] ;
        z1 += (x[i]*x[i]*x[i]+x[i]*y[i]*y[i]);
        z2 += (x[i]*x[i]*y[i]+y[i]*y[i]*y[i]);
        z3 += (x[i]*x[i] + y[i]*y[i]);
        sY2 += (y[i]*y[i]) ;
    }

    M[1] = 2*sX2;  M[2] = 2*sXY; M[3] =  -sX;         D[1] =  z1 ;
    M[4] = 2*sXY;  M[5] = 2*sY2; M[6] =  -sY;         D[2] =  z2 ;
    M[7] = 2*sX;   M[8] = 2*sY;  M[9] =  -count;      D[3] =  z3;
    dived = M[1]*M[5]*M[9]+M[2]*M[6]*M[7]+M[4]*M[8]*M[3]-(M[3]*M[5]*M[7]+M[1]*M[6]*M[8]+M[2]*M[4]*M[9]);
    a1 =   D[1]*M[5]*M[9]+M[2]*M[6]*D[3]+D[2]*M[8]*M[3]-(M[3]*M[5]*D[3]+D[1]*M[6]*M[8]+M[2]*D[2]*M[9]);
    b1 =   M[1]*D[2]*M[9]+D[1]*M[6]*M[7]+M[4]*D[3]*M[3]-(M[3]*D[2]*M[7]+M[1]*M[6]*D[3]+D[1]*M[4]*M[9]);
    c1 =   M[1]*M[5]*D[3]+M[2]*D[2]*M[7]+M[4]*M[8]*D[1]-(D[1]*M[5]*M[7]+M[1]*D[2]*M[8]+M[2]*M[4]*D[3]);
    if(dived==0)
        return ret;
    a = a1/dived;   //Xc
    b = b1/dived;   //Yc
    c = c1/dived;
    ret.R = sqrt(abs(a*a+b*b-c));
    ret.Cx = a;
    ret.Cy = b;
    return ret;
}
         


void __fastcall TForm1::Button1Click(TObject *Sender)
{
    struct circle s;
    int a,b,r;
    int  x[]={220,243,270,290,270,240,210};
    int y[]={310,290,270,250,270,290,310};
    s = fit_circle(x,y,7);
    a = s.Cx;
    b = s.Cy;
    r = s.R;
    for(int i=0;i< 7;i++)
        Canvas->Ellipse(x[i]-2,y[i]-2,x[i]+2,y[i]+2);
    Canvas->Pen->Color = clRed;
    Canvas->Brush->Style =  bsSolid ;
    Canvas->Brush->Color= clRed;

    Canvas->MoveTo(a-3,b); //

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -