# Some Formulas

Here are some handy algebraic formulas.

## Circle Thru 3 Points

Let's have 3 points (x0,y0), (x1,y1), (x2,y2). I want to find the circle with center (p,q) and radius r which goes thru these points.
```  Generally,

(X-p)^2+(Y-q)^2= r^2

r^2 = X^2-2Xp+p^2 + Y^2-2Yq+q^2

r^2 + 2Xp + 2Yq - p^2 - q^2 = X^2+Y^2

A:= 2X   B:= 2Y  C:= X^2+Y^2
r^2 + Ap + Bq -p^2-q^2= C

Let's apply it to our points:

r^2 + A0p + B0q -p^2-q^2= C0
r^2 + A1p + B1q -p^2-q^2= C1
r^2 + A2p + B2q -p^2-q^2= C2

To get rid of the square terms, I'll compute

K0:=2C0 - C1- C2
K1:=2C1 - C0- C2

K0= 2r^2 + 2A0p + 2B0q -2p^2 -2q^2
-r^2 -  A1p -  B1q  +p^2  +q^2
-r^2 -  A2p -  B2q  +p^2  +q^2
K0= (2A0-A1-A2)p + (2B0-B1-B2)q = D0p+E0q

D0:= 2A0-A1-A2   E0:= 2B0-B1-B2

K1= -r^2 -  A0p -  B0q + p^2 + q^2
+2r^2 + 2A1p + 2B1q -2p^2 -2q^2
-r^2 -  A2p -  B2q + p^2 + q^2
K1= (2A1-A0-A2)p + (2B1-B0-B2)q = D1p+E1q

D1:= 2A1-A0-A2    E1:= 2B1-B0-B2
```
Now we have two linear equations:
```   K0= D0p+E0q
K1= D1p+E1q
```
Multiply the first equation by E1 and the second by -E0, then add them
```  K0*E1 =  D0*E1p + E0*E1q
-K1*E0 = -D1*E0p - E0*E1q
---------------------------
K0*E1-K1*E0 = (D0*E1-D1*E0)p

p:= (K0*E1-K1*E0) / (D0*E1-D1*E0)
```
Substitute q in K0 equation:
```  K0 = D0*p+E0q
q:= (K0-D0p)/E0

r:= sqrt((X0-p)^2+(Y0-q)^2)
```
To summarize:
```  A0= 2*X0;   B0= 2*Y0;   C0= X0*X0+Y0*Y0;
A1= 2*X1;   B1= 2*Y1;   C1= X1*X1+Y1*Y1;
A2= 2*X2;   B2= 2*Y2;   C2= X2*X2+Y2*Y2;

D0= 2*A0-A1-A2;  E0= 2*B0-B1-B2; K0= 2*C0-C1-C2;
D1= 2*A1-A0-A2;  E1= 2*B1-B0-B2; K1= 2*C1-C0-C2;

p= (K0*E1-K1*E0) / (D0*E1-D1*E0);
q= (K0-D0*p)/E0;
r= sqrt( (X0-p)*(X0-p)+(Y0-q)*(Y0-q) );
```