import Mathlib.Data.Real.Basic import Mathlib.LinearAlgebra.Matrix.Determinant.Basic import Mathlib.Tactic /-! # Circle through three points via a determinant (plane coordinates) This file develops reusable algebraic infrastructure for the geometry problem published in [ajc59j]. Given three points $P_1,P_2,P_3$ in the plane, we define the (possibly degenerate) *general circle equation* by the $4\times 4$ determinant $$ \det\begin{pmatrix} x^2+y^2 & x & y & 1\\ s_1 & x_1 & y_1 & 1\\ s_2 & x_2 & y_2 & 1\\ s_3 & x_3 & y_3 & 1 \end{pmatrix}=0, $$ where $s_i=x_i^2+y_i^2$. This determinant is a polynomial of the form $$A(x^2+y^2)+Bx+Cy+D,$$ and it vanishes at each of the three defining points (because two rows coincide). The intent is to use this to avoid explicitly solving the linear system for $\alpha,\beta,\gamma$ in the equation $x^2+y^2+\alpha x+\beta y+\gamma=0$. -/ namespace ApTangentDet noncomputable section /-- Plane point as a pair of reals. -/ abbrev Pt : Type := ℝ × ℝ def sx (p : Pt) : ℝ := p.1^2 + p.2^2 /-- The $4\times 4$ matrix used to define the circle through three points. -/ def circleMat (p1 p2 p3 : Pt) (x y : ℝ) : Matrix (Fin 4) (Fin 4) ℝ := fun i j => match (i.1, j.1) with | (0,0) => x^2 + y^2 | (0,1) => x | (0,2) => y | (0,3) => 1 | (1,0) => sx p1 | (1,1) => p1.1 | (1,2) => p1.2 | (1,3) => 1 | (2,0) => sx p2 | (2,1) => p2.1 | (2,2) => p2.2 | (2,3) => 1 | (3,0) => sx p3 | (3,1) => p3.1 | (3,2) => p3.2 | (3,3) => 1 | _ => 0 /-- The determinant polynomial defining the (general) circle through three points. -/ def circlePoly (p1 p2 p3 : Pt) (x y : ℝ) : ℝ := (circleMat p1 p2 p3 x y).det /-- A point lies on the determinant circle through three points. -/ def MemCircle (p1 p2 p3 : Pt) (p : Pt) : Prop := circlePoly p1 p2 p3 p.1 p.2 = 0 lemma memCircle_p1 (p1 p2 p3 : Pt) : MemCircle p1 p2 p3 p1 := by -- row 0 equals row 1 when (x,y)=(p1.1,p1.2) have hrow : (circleMat p1 p2 p3 p1.1 p1.2) 0 = (circleMat p1 p2 p3 p1.1 p1.2) 1 := by funext j fin_cases j <;> simp [circleMat, sx] -- determinant is zero with repeated rows have : (circleMat p1 p2 p3 p1.1 p1.2).det = 0 := by simpa [hrow] using Matrix.det_zero_of_row_eq (M := circleMat p1 p2 p3 p1.1 p1.2) (i_ne_j := by decide) hrow simpa [MemCircle, circlePoly] using this lemma memCircle_p2 (p1 p2 p3 : Pt) : MemCircle p1 p2 p3 p2 := by have hrow : (circleMat p1 p2 p3 p2.1 p2.2) 0 = (circleMat p1 p2 p3 p2.1 p2.2) 2 := by funext j fin_cases j <;> simp [circleMat, sx] have : (circleMat p1 p2 p3 p2.1 p2.2).det = 0 := by simpa [hrow] using Matrix.det_zero_of_row_eq (M := circleMat p1 p2 p3 p2.1 p2.2) (i_ne_j := by decide) hrow simpa [MemCircle, circlePoly] using this lemma memCircle_p3 (p1 p2 p3 : Pt) : MemCircle p1 p2 p3 p3 := by have hrow : (circleMat p1 p2 p3 p3.1 p3.2) 0 = (circleMat p1 p2 p3 p3.1 p3.2) 3 := by funext j fin_cases j <;> simp [circleMat, sx] have : (circleMat p1 p2 p3 p3.1 p3.2).det = 0 := by simpa [hrow] using Matrix.det_zero_of_row_eq (M := circleMat p1 p2 p3 p3.1 p3.2) (i_ne_j := by decide) hrow simpa [MemCircle, circlePoly] using this /-- TODO: extract coefficients $(A,B,C,D)$ so that `circlePoly p1 p2 p3 x y = A*(x^2+y^2)+B*x+C*y+D`. This would allow tangency checks with all denominators cleared. -/ lemma circlePoly_eq_quadratic_linear (p1 p2 p3 : Pt) : ∃ A B C D : ℝ, ∀ x y, circlePoly p1 p2 p3 x y = A*(x^2+y^2) + B*x + C*y + D := by -- Pure determinant expansion. sorry end end ApTangentDet