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

前端 未结 6 1998
慢半拍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:04

    I am sorry my answer is late. Any solution using "slope" will fail when two of the points form a vertical line, because the slope will be infinite.

    Here is a simple robust solution for 2019 that always works correctly:

    public static boolean circleCenter(double[] p1, double[] p2, double[] p3, double[] center) {
        double ax = (p1[0] + p2[0]) / 2;
        double ay = (p1[1] + p2[1]) / 2;
        double ux = (p1[1] - p2[1]);
        double uy = (p2[0] - p1[0]);
        double bx = (p2[0] + p3[0]) / 2;
        double by = (p2[1] + p3[1]) / 2;
        double vx = (p2[1] - p3[1]);
        double vy = (p3[0] - p2[0]);
        double dx = ax - bx;
        double dy = ay - by;
        double vu = vx * uy - vy * ux;
        if (vu == 0)
            return false; // Points are collinear, so no unique solution
        double g = (dx * uy - dy * ux) / vu;
        center[0] = bx + g * vx;
        center[1] = by + g * vy;
        return true;
    }
    

    The above code will return "false" if and only if the 3 points are collinear.

提交回复
热议问题