-- The coordinates of an intersection of two lines, a line and a circle, -- or two circles lie in every constructibly closed set containing the -- coordinates of the defining points: the first is a linear system, the -- other two reduce to a quadratic equation over the set. -- Part of Cumulus, the Grace17 mathematics library. -- Copyright (c) 2026 Grant Passmore. -- Released under the Apache License 2.0. See LICENSE in the repository root. -- Authors: Grant Passmore module Grace.Cumulus.Geometry.Construction.Intersections import Grace.Cumulus.Geometry.Construction.Numbers import Grace.Cumulus.Geometry.Chords import Grace.Cumulus.Geometry.Lines -- Square roots and quadratic equations over a closed set -- A root of a quadratic with coefficients in S and leading coefficient -- nonzero lies in S: t = (−β ± √(β² − 4αγ)) / (2α), where the square root -- is realSqrt((2αt + β)²) = |2αt + β| ∈ S. theorem ConstructiblyClosed.quadratic_root (S : Set) (α β γ t : Real) requires closed : ConstructiblyClosed(S) requires αIn : α ∈ S requires βIn : β ∈ S requires γIn : γ ∈ S requires nonzero : α ≠ 0 requires root : α * (t * t) + β * t + γ = 0 shows t ∈ S proof have field : RealSubfield(S) := closed.left -- The square of 2αt + β is the discriminant, an element of S. have discriminant : ((α + α) * t + β) * ((α + α) * t + β) = β * β - (α + α) * (γ + γ) linear_combination ((α + α) + (α + α)) * root have discriminantIn : β * β - (α + α) * (γ + γ) ∈ S exact RealSubfield.sub_mem with * have squareIn : ((α + α) * t + β) * ((α + α) * t + β) ∈ S rw discriminant exact discriminantIn have sIn := ConstructiblyClosed.of_square_mem with closed, squareIn -- Solve for t. have doubleNonzero : α + α ≠ 0 intro zero apply nonzero linarith have inverse := realInvValue.mul_left with doubleNonzero have solved : t = ((α + α) * t + β - β) * realInvValue(α + α) linear_combination (-t) * inverse have doubleIn := RealSubfield.add_mem with field, αIn, αIn have inverseIn := RealSubfield.inv_mem with field, doubleIn, doubleNonzero rw solved exact RealSubfield.mul_mem with * -- A point (q, r) of a circle with center (c₁, c₂) and squared radius ρ and -- of a line Xq + Yr = K with X ≠ 0, all data in S, has coordinates in S: -- X² times the circle equation is a quadratic in r over S with leading -- coefficient X² + Y². theorem ConstructiblyClosed.circle_line_root (S : Set) (X Y K q r c₁ c₂ ρ : Real) requires closed : ConstructiblyClosed(S) requires XIn : X ∈ S requires YIn : Y ∈ S requires KIn : K ∈ S requires c₁In : c₁ ∈ S requires c₂In : c₂ ∈ S requires ρIn : ρ ∈ S requires nonzero : X ≠ 0 requires line : X * q + Y * r = K requires circle : (c₁ - q) * (c₁ - q) + (c₂ - r) * (c₂ - r) = ρ shows q ∈ S ∧ r ∈ S proof have field : RealSubfield(S) := closed.left have squareX := realMulValue.square_positive with nonzero have squareY := realMulValue.square_nonnegative at Y have leading : Y * Y + X * X ≠ 0 intro zero linarith have root : (Y * Y + X * X) * (r * r) + (Y * (X * c₁ - K) + Y * (X * c₁ - K) - X * X * c₂ - X * X * c₂) * r + ((X * c₁ - K) * (X * c₁ - K) + X * X * (c₂ * c₂) - X * X * ρ) = 0 linear_combination X * X * circle + (X * c₁ + X * c₁ - K + Y * r - X * q) * line have αIn : Y * Y + X * X ∈ S exact RealSubfield.add_mem with * have βIn : Y * (X * c₁ - K) + Y * (X * c₁ - K) - X * X * c₂ - X * X * c₂ ∈ S exact RealSubfield.sub_mem with * have γIn : (X * c₁ - K) * (X * c₁ - K) + X * X * (c₂ * c₂) - X * X * ρ ∈ S exact RealSubfield.sub_mem with * have rIn := ConstructiblyClosed.quadratic_root with closed, αIn, βIn, γIn, leading, root have inverse := realInvValue.mul_left with nonzero have inverseIn := RealSubfield.inv_mem with field, XIn, nonzero have solved : q = (K - Y * r) * realInvValue(X) linear_combination (-q) * inverse + realInvValue(X) * line constructor . rw solved exact RealSubfield.mul_mem with * . exact rIn -- The three intersections -- Two lines: p = a + t(b − a) = c + u(d − c) is a linear system in t, u -- with determinant directionCross(a, b, c, d), solved for t by Cramer. theorem lineLine.coordinates_closed (S : Set) (a b c d p : Point) (t u : Real) requires closed : ConstructiblyClosed(S) requires aIn : x(a) ∈ S ∧ y(a) ∈ S requires bIn : x(b) ∈ S ∧ y(b) ∈ S requires cIn : x(c) ∈ S ∧ y(c) ∈ S requires dIn : x(d) ∈ S ∧ y(d) ∈ S requires notParallel : directionCross(a, b, c, d) ≠ 0 requires onFirst : OnLine(p, a, b, t) requires onSecond : OnLine(p, c, d, u) shows x(p) ∈ S ∧ y(p) ∈ S proof have field : RealSubfield(S) := closed.left rcases aIn with ⟨axIn, ayIn⟩ rcases bIn with ⟨bxIn, byIn⟩ rcases cIn with ⟨cxIn, cyIn⟩ rcases dIn with ⟨dxIn, dyIn⟩ rcases onFirst with ⟨firstX, firstY⟩ rcases onSecond with ⟨secondX, secondY⟩ have crossIn : directionCross(a, b, c, d) ∈ S unfold directionCross exact RealSubfield.sub_mem with * have inverseIn := RealSubfield.inv_mem with field, crossIn, notParallel have inverse := realInvValue.mul_left with notParallel -- Cramer's rule for the parameter t. have solved : t = realInvValue(directionCross(a, b, c, d)) * ((x(c) - x(a)) * (y(d) - y(c)) - (y(c) - y(a)) * (x(d) - x(c))) have crossEq : directionCross(a, b, c, d) = (x(b) - x(a)) * (y(d) - y(c)) - (y(b) - y(a)) * (x(d) - x(c)) unfold directionCross rfl linear_combination (-t) * inverse + (t * realInvValue(directionCross(a, b, c, d))) * crossEq - (realInvValue(directionCross(a, b, c, d)) * (y(d) - y(c))) * firstX + (realInvValue(directionCross(a, b, c, d)) * (y(d) - y(c))) * secondX + (realInvValue(directionCross(a, b, c, d)) * (x(d) - x(c))) * firstY - (realInvValue(directionCross(a, b, c, d)) * (x(d) - x(c))) * secondY have tIn : t ∈ S rw solved exact RealSubfield.mul_mem with * constructor . rw firstX exact RealSubfield.add_mem with * . rw firstY exact RealSubfield.add_mem with * -- A line and a circle: substituting p = a + t(b − a) into the circle -- equation gives αt² + βt + γ = 0 with α = distanceSquared(a, b) > 0. theorem lineCircle.coordinates_closed (S : Set) (a b c w p : Point) (t : Real) requires closed : ConstructiblyClosed(S) requires aIn : x(a) ∈ S ∧ y(a) ∈ S requires bIn : x(b) ∈ S ∧ y(b) ∈ S requires cIn : x(c) ∈ S ∧ y(c) ∈ S requires wIn : x(w) ∈ S ∧ y(w) ∈ S requires distinct : a ≠ b requires onLine : OnLine(p, a, b, t) requires onCircle : OnCircle(p, c, w) shows x(p) ∈ S ∧ y(p) ∈ S proof have field : RealSubfield(S) := closed.left rcases aIn with ⟨axIn, ayIn⟩ rcases bIn with ⟨bxIn, byIn⟩ rcases cIn with ⟨cxIn, cyIn⟩ rcases wIn with ⟨wxIn, wyIn⟩ rcases onLine with ⟨lineX, lineY⟩ unfold OnCircle at onCircle have pointRadius := distanceSquared.coordinates at p, c with * have throughRadius := distanceSquared.coordinates at w, c with * rw [pointRadius, throughRadius, lineX, lineY] at onCircle -- The leading coefficient is the squared length of ab. have positive := distanceSquared.positive at a, b with distinct have leading := distanceSquared.coordinates at a, b with * rw leading at positive have nonzero : (x(b) - x(a)) * (x(b) - x(a)) + (y(b) - y(a)) * (y(b) - y(a)) ≠ 0 intro zero linarith have root : ((x(b) - x(a)) * (x(b) - x(a)) + (y(b) - y(a)) * (y(b) - y(a))) * (t * t) + ((x(b) - x(a)) * (x(a) - x(c)) + (y(b) - y(a)) * (y(a) - y(c)) + ((x(b) - x(a)) * (x(a) - x(c)) + (y(b) - y(a)) * (y(a) - y(c)))) * t + ((x(a) - x(c)) * (x(a) - x(c)) + (y(a) - y(c)) * (y(a) - y(c)) - ((x(c) - x(w)) * (x(c) - x(w)) + (y(c) - y(w)) * (y(c) - y(w)))) = 0 linear_combination onCircle have αIn : (x(b) - x(a)) * (x(b) - x(a)) + (y(b) - y(a)) * (y(b) - y(a)) ∈ S exact RealSubfield.add_mem with * have βIn : (x(b) - x(a)) * (x(a) - x(c)) + (y(b) - y(a)) * (y(a) - y(c)) + ((x(b) - x(a)) * (x(a) - x(c)) + (y(b) - y(a)) * (y(a) - y(c))) ∈ S exact RealSubfield.add_mem with * have γIn : (x(a) - x(c)) * (x(a) - x(c)) + (y(a) - y(c)) * (y(a) - y(c)) - ((x(c) - x(w)) * (x(c) - x(w)) + (y(c) - y(w)) * (y(c) - y(w))) ∈ S exact RealSubfield.sub_mem with * have tIn := ConstructiblyClosed.quadratic_root with closed, αIn, βIn, γIn, nonzero, root constructor . rw lineX exact RealSubfield.add_mem with * . rw lineY exact RealSubfield.add_mem with * -- Two circles: the difference of the circle equations is linear in the -- coordinates of p with a nonzero coefficient, which eliminates one -- coordinate and leaves a quadratic over S in the other. theorem circleCircle.coordinates_closed (S : Set) (c w e v p : Point) requires closed : ConstructiblyClosed(S) requires cIn : x(c) ∈ S ∧ y(c) ∈ S requires wIn : x(w) ∈ S ∧ y(w) ∈ S requires eIn : x(e) ∈ S ∧ y(e) ∈ S requires vIn : x(v) ∈ S ∧ y(v) ∈ S requires centers : c ≠ e requires onFirst : OnCircle(p, c, w) requires onSecond : OnCircle(p, e, v) shows x(p) ∈ S ∧ y(p) ∈ S proof have field : RealSubfield(S) := closed.left rcases cIn with ⟨cxIn, cyIn⟩ rcases wIn with ⟨wxIn, wyIn⟩ rcases eIn with ⟨exIn, eyIn⟩ rcases vIn with ⟨vxIn, vyIn⟩ unfold OnCircle at onFirst unfold OnCircle at onSecond have firstRadius := distanceSquared.coordinates at p, c with * have firstThrough := distanceSquared.coordinates at w, c with * have secondRadius := distanceSquared.coordinates at p, e with * have secondThrough := distanceSquared.coordinates at v, e with * rw [firstRadius, firstThrough] at onFirst rw [secondRadius, secondThrough] at onSecond -- The radical line of the two circles. have line : ((x(e) - x(c)) + (x(e) - x(c))) * x(p) + ((y(e) - y(c)) + (y(e) - y(c))) * y(p) = (x(c) - x(w)) * (x(c) - x(w)) + (y(c) - y(w)) * (y(c) - y(w)) - ((x(e) - x(v)) * (x(e) - x(v)) + (y(e) - y(v)) * (y(e) - y(v))) + x(e) * x(e) + y(e) * y(e) - x(c) * x(c) - y(c) * y(c) linear_combination onFirst - onSecond have XIn : (x(e) - x(c)) + (x(e) - x(c)) ∈ S exact RealSubfield.add_mem with * have YIn : (y(e) - y(c)) + (y(e) - y(c)) ∈ S exact RealSubfield.add_mem with * have KIn : (x(c) - x(w)) * (x(c) - x(w)) + (y(c) - y(w)) * (y(c) - y(w)) - ((x(e) - x(v)) * (x(e) - x(v)) + (y(e) - y(v)) * (y(e) - y(v))) + x(e) * x(e) + y(e) * y(e) - x(c) * x(c) - y(c) * y(c) ∈ S exact RealSubfield.sub_mem with * have ρIn : (x(c) - x(w)) * (x(c) - x(w)) + (y(c) - y(w)) * (y(c) - y(w)) ∈ S exact RealSubfield.add_mem with * by_cases degenerate : (x(e) - x(c)) + (x(e) - x(c)) = 0 . -- The radical line is horizontal, so eliminate x(p) instead. have vertical : (y(e) - y(c)) + (y(e) - y(c)) ≠ 0 intro zero apply centers have horizontal : x(c) = x(e) by linarith have upright : y(c) = y(e) by linarith exact point.ext with horizontal, upright have swapped : ((y(e) - y(c)) + (y(e) - y(c))) * y(p) + ((x(e) - x(c)) + (x(e) - x(c))) * x(p) = (x(c) - x(w)) * (x(c) - x(w)) + (y(c) - y(w)) * (y(c) - y(w)) - ((x(e) - x(v)) * (x(e) - x(v)) + (y(e) - y(v)) * (y(e) - y(v))) + x(e) * x(e) + y(e) * y(e) - x(c) * x(c) - y(c) * y(c) linear_combination line have circle : (y(c) - y(p)) * (y(c) - y(p)) + (x(c) - x(p)) * (x(c) - x(p)) = (x(c) - x(w)) * (x(c) - x(w)) + (y(c) - y(w)) * (y(c) - y(w)) linear_combination onFirst obtain ⟨yIn, xIn⟩ := ConstructiblyClosed.circle_line_root with closed, YIn, XIn, KIn, cyIn, cxIn, ρIn, vertical, swapped, circle exact ⟨xIn, yIn⟩ . exact ConstructiblyClosed.circle_line_root with closed, XIn, YIn, KIn, cxIn, cyIn, ρIn, degenerate, line, onFirst