import Mathlib.Data.Real.Basic import Mathlib.Tactic /-! # Scaled circle equations and a denominator-free tangency criterion We represent a (possibly scaled) circle by an equation $$A(x^2+y^2) + Bx + Cy + D = 0,$$ and a line by $$a x + b y + c = 0.$$ The *denominator-free* tangency condition is $$\left(a\left(-\frac{B}{2}\right)+b\left(-\frac{C}{2}\right)+cA\right)^2 =(a^2+b^2)\left(\frac{B^2+C^2}{4}-AD\right).$$ This equals the usual distance-to-center formulation after multiplying by $A^2$. -/ namespace CircleEqScaled noncomputable section abbrev Pt : Type := ℝ × ℝ /-- A line `a*x + b*y + c = 0`. -/ structure LineEq where a : ℝ b : ℝ c : ℝ /-- Evaluate a `LineEq` at a point. -/ def LineEq.eval (L : LineEq) (p : Pt) : ℝ := L.a * p.1 + L.b * p.2 + L.c /-- A *scaled* circle equation $A(x^2+y^2)+Bx+Cy+D=0$. -/ structure CircleEq where A : ℝ B : ℝ C : ℝ D : ℝ /-- Point membership in a scaled circle equation. -/ def CircleEq.Mem (s : CircleEq) (p : Pt) : Prop := s.A * (p.1^2 + p.2^2) + s.B * p.1 + s.C * p.2 + s.D = 0 /-- Center of the circle (requires `A ≠ 0`). -/ def CircleEq.center (s : CircleEq) : Pt := (-s.B / (2*s.A), -s.C / (2*s.A)) /-- Radius squared of the circle (requires `A ≠ 0`). -/ def CircleEq.radiusSq (s : CircleEq) : ℝ := (s.B^2 + s.C^2) / (4*s.A^2) - s.D / s.A /-- Denominator-free tangency predicate for a line against a scaled circle equation. This avoids division by `s.A`. -/ def IsTangent (s : CircleEq) (L : LineEq) : Prop := (L.a * (-s.B/2) + L.b * (-s.C/2) + L.c * s.A)^2 = (L.a^2 + L.b^2) * ((s.B^2 + s.C^2)/4 - s.A * s.D) /-- If `A ≠ 0`, the denominator-free tangency condition is equivalent to the usual center/radius formula. -/ lemma isTangent_iff_center_radius (s : CircleEq) (L : LineEq) (hA : s.A ≠ 0) : IsTangent s L ↔ (L.a * (s.center.1) + L.b * (s.center.2) + L.c)^2 = (L.a^2 + L.b^2) * s.radiusSq := by have h2A : (2 * s.A) ≠ 0 := by exact mul_ne_zero (by norm_num) hA -- unfold and clear denominators -- unfold and clear denominators simp [IsTangent, CircleEq.center, CircleEq.radiusSq] field_simp [hA, h2A] /-- Scaling the circle equation by a nonzero constant does not change tangency. This matches the fact that determinant-defined circle equations come with an arbitrary scale. -/ def CircleEq.smul (t : ℝ) (s : CircleEq) : CircleEq := ⟨t*s.A, t*s.B, t*s.C, t*s.D⟩ lemma isTangent_smul_iff (t : ℝ) (ht : t ≠ 0) (s : CircleEq) (L : LineEq) : IsTangent (CircleEq.smul t s) L ↔ IsTangent s L := by have ht2 : t^2 ≠ 0 := by simpa [pow_two] using mul_ne_zero ht ht -- unfold both sides and simplify; both become `t^2 * (...) = t^2 * (...)`. unfold IsTangent CircleEq.smul -- `simp` does the scalar factoring, `ring` does normalization. -- We then cancel `t^2`. constructor · intro h have h' : t^2 * (L.a * (-s.B / 2) + L.b * (-s.C / 2) + L.c * s.A) ^ 2 = t^2 * ((L.a ^ 2 + L.b ^ 2) * ((s.B ^ 2 + s.C ^ 2) / 4 - s.A * s.D)) := by -- turn `h` into a statement with a common factor `t^2` -- (pure ring manipulation) -- -- We use `nlinarith` as a convenient ring-normalizer on equalities. nlinarith [h] exact mul_left_cancel₀ ht2 h' · intro h have h' : t^2 * (L.a * (-s.B / 2) + L.b * (-s.C / 2) + L.c * s.A) ^ 2 = t^2 * ((L.a ^ 2 + L.b ^ 2) * ((s.B ^ 2 + s.C ^ 2) / 4 - s.A * s.D)) := by nlinarith [h] -- go back to the scaled equation nlinarith [h'] end end CircleEqScaled