What is the algorithm for finding the center of a circle from three points?

前端 未结 6 1995
慢半拍i
慢半拍i 2020-12-02 15:54

I have three points on the circumference of a circle:

pt A = (A.x, A.y);
pt B = (B.x, B.y);
pt C = (C.x, C.y);

How do I calculate the cente

6条回答
  •  心在旅途
    2020-12-02 16:21

    public Vector2 CarculateCircleCenter(Vector2 p1, Vector2 p2, Vector2 p3)
    {
        if (
            p2.x - p1.x == 0 ||
            p3.x - p2.x == 0 ||
            p2.y - p1.y == 0 ||
            p3.y - p2.y == 0
        ) return null;
    
        Vector2 center = new Vector2();
        float ma = (p2.y - p1.y) / (p2.x - p1.x);
        float mb = (p3.y - p2.y) / (p3.x - p2.x);
        center.x = (ma * mb * (p1.y - p3.y) + mb * (p1.x - p2.x) - ma * (p2.x + p3.x)) / (2 * (mb - ma));
        center.y = (-1 / ma) * (center.x - (p1.x + p2.x) * 0.5) + (p1.y + p2.y) * 0.5;
        return center;
    }
    

提交回复
热议问题