1. 程式人生 > >已知圓上三個點座標,求圓半徑 r 和 圓心座標

已知圓上三個點座標,求圓半徑 r 和 圓心座標

問題:

已知圓上三個點座標分別為(x1,y1)、(x2,y2)、(x3,y3)

求圓半徑R和圓心座標(X,Y)

X,Y,R為未知數,x1,y1,x2,y2,x3,y3為常數

則由圓公式:
(x1-X)²+(y1-Y)²=R²      (1)式
(x2-X)²+(y2-Y)²=R²      (2)式
(x3-X)²+(y3-Y)²=R²      (3)式
(1)-(2),就是左邊減左邊,右邊減右邊,得到
x1²-2Xx1+X²+(y1²-2Yy1+Y²)-(x2²-2Xx2+X²)-(y2²-2Yy2+Y²)=R²-R²
整理得
x1²-x2²-2*x1*X+2*x2*X+y12-y22-2*y1*Y+2*y2*Y=0
(2)-(3)整理得:
x2²-x3²-2*x2*X+2*x3*X+y22-y32-2*y2*Y+2y3*Y=0
再整理上面兩式得
(2x2-2x1)X+(2y2-2y1)Y=x2²-x1²+y2²-y1²

(2x3-2x2)X+(2y3-2y2)Y=x3²-x2²+y3²-y2²

令:

a=2x3-2x2;b=2y3-2y2;c=x3²-x2²+y3²-y2²

e = 2x2-2x1;f=2y2-2y1;g=x2²-x1²+y2²-y1²

於是有

eX+fY=g 

aX+bY=c

解得

X=(gb-cf)\(eb-af)

Y=(ag-ce)\(af-be)

R=sqrt((X-x1)*(X-x1)+(Y-y1)*(Y-y1))則圓心座標為(X,Y),半徑為R

程式實現:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

void Calculate_cicular(Point px1, Point px2, Point px3)

{

    int x1, y1, x2, y2, x3, y3;

    int a, b, c, g, e, f;

    x1 = px1.x;

    y1 = px1.y;

    

x2 = px2.x;

    y2 = px2.y;

    x3 = px3.x;

    y3 = px3.y;

    e = 2 * (x2 - x1);

    f = 2 * (y2 - y1);

    g = x2*x2 - x1*x1 + y2*y2 - y1*y1;

    a = 2 * (x3 - x2);

    b = 2 * (y3 - y2);

    c = x3*x3 - x2*x2 + y3*y3 - y2*y2;

    X = (g*b - c*f) / (e*b - a*f);

    Y = (a*g - c*e) / (a*f - b*e);

    R = sqrt((X-x1)*(X-x1)+(Y-y1)*(Y-y1));

 

}