How to calculate the centre point of a circle given three points?

后端 未结 2 869
無奈伤痛
無奈伤痛 2021-01-27 03:42

I am using Javascript and I know the positions of 3 points. I wish to use these to find out the center point of a circle.

I have found this logic (Not the chosen answer

2条回答
  •  被撕碎了的回忆
    2021-01-27 04:01

    My favorite resolution:

    Translate the three points to bring one of them at the origin (subtract (X0,Y0)).

    The equation of a circle through two points and the origin can be written

    2X.Xc + 2Y.Yc = X² + Y²
    

    Plugging the coordinates of the two points, you get an easy system of two equations in two unknowns, and by Cramer

    Xc = (Z1.Y2 - Z2.Y1) / D
    Yc = (X1.Z2 - X2.Z1) / D
    
    D = 2(X1.Y2 - X2.Y1), Z1 = X1²+Y1², Z2 = X2²+Y2²
    

    to be translated back (add (X0,Y0)).


    The formula fails when the three points are aligned, which is diagnosed by D = 0 (or small in comparison to the numerators).


            X1-= X0; Y1-= Y0; X2-= X0; Y2-= Y0;
    
            double Z1= X1 * X1 + Y1 * Y1;
            double Z2= X2 * X2 + Y2 * Y2;
            double D= 2 * (X1 * Y2 - X2 * Y1);
    
            double Xc= (Z1 * Y2 - Z2 * Y1) / D + X0;
            double Yc= (X1 * Z2 - X2 * Z1) / D + Y0;
    

提交回复
热议问题