Grace/Cumulus/Geometry/Construction/Intersections.g17
Imports
Grace.Cumulus.Geometry.Construction.Numbers · Grace.Cumulus.Geometry.Chords · Grace.Cumulus.Geometry.Lines
Declarations
- ConstructiblyClosed.quadratic_root theorem derivation
- ConstructiblyClosed.circle_line_root theorem derivation
- lineLine.coordinates_closed theorem derivation
- lineCircle.coordinates_closed theorem derivation
- circleCircle.coordinates_closed theorem derivation
Source
1-- The coordinates of an intersection of two lines, a line and a circle,2-- or two circles lie in every constructibly closed set containing the3-- coordinates of the defining points: the first is a linear system, the4-- other two reduce to a quadratic equation over the set.5-- Part of Cumulus, the Grace17 mathematics library.6-- Copyright (c) 2026 Grant Passmore.7-- Released under the Apache License 2.0. See LICENSE in the repository root.8-- Authors: Grant Passmore910module Grace.Cumulus.Geometry.Construction.Intersections11import Grace.Cumulus.Geometry.Construction.Numbers12import Grace.Cumulus.Geometry.Chords13import Grace.Cumulus.Geometry.Lines1415-- Square roots and quadratic equations over a closed set1617-- A root of a quadratic with coefficients in S and leading coefficient18-- nonzero lies in S: t = (−β ± √(β² − 4αγ)) / (2α), where the square root19-- is realSqrt((2αt + β)²) = |2αt + β| ∈ S.20theorem ConstructiblyClosed.quadratic_root (S : Set) (α β γ t : Real)21 requires closed : ConstructiblyClosed(S)22 requires αIn : α ∈ S23 requires βIn : β ∈ S24 requires γIn : γ ∈ S25 requires nonzero : α ≠ 026 requires root : α * (t * t) + β * t + γ = 027 shows t ∈ S28proof29 have field : RealSubfield(S) := closed.left30 -- The square of 2αt + β is the discriminant, an element of S.31 have discriminant : ((α + α) * t + β) * ((α + α) * t + β) =32 β * β - (α + α) * (γ + γ)33 linear_combination ((α + α) + (α + α)) * root34 have discriminantIn : β * β - (α + α) * (γ + γ) ∈ S35 exact RealSubfield.sub_mem with *36 have squareIn : ((α + α) * t + β) * ((α + α) * t + β) ∈ S37 rw discriminant38 exact discriminantIn39 have sIn := ConstructiblyClosed.of_square_mem with closed, squareIn40 -- Solve for t.41 have doubleNonzero : α + α ≠ 042 intro zero43 apply nonzero44 linarith45 have inverse := realInvValue.mul_left with doubleNonzero46 have solved : t = ((α + α) * t + β - β) * realInvValue(α + α)47 linear_combination (-t) * inverse48 have doubleIn := RealSubfield.add_mem with field, αIn, αIn49 have inverseIn := RealSubfield.inv_mem with field, doubleIn, doubleNonzero50 rw solved51 exact RealSubfield.mul_mem with *5253-- A point (q, r) of a circle with center (c₁, c₂) and squared radius ρ and54-- of a line Xq + Yr = K with X ≠ 0, all data in S, has coordinates in S:55-- X² times the circle equation is a quadratic in r over S with leading56-- coefficient X² + Y².57theorem ConstructiblyClosed.circle_line_root (S : Set)58 (X Y K q r c₁ c₂ ρ : Real)59 requires closed : ConstructiblyClosed(S)60 requires XIn : X ∈ S61 requires YIn : Y ∈ S62 requires KIn : K ∈ S63 requires c₁In : c₁ ∈ S64 requires c₂In : c₂ ∈ S65 requires ρIn : ρ ∈ S66 requires nonzero : X ≠ 067 requires line : X * q + Y * r = K68 requires circle : (c₁ - q) * (c₁ - q) + (c₂ - r) * (c₂ - r) = ρ69 shows q ∈ S ∧ r ∈ S70proof71 have field : RealSubfield(S) := closed.left72 have squareX := realMulValue.square_positive with nonzero73 have squareY := realMulValue.square_nonnegative at Y74 have leading : Y * Y + X * X ≠ 075 intro zero76 linarith77 have root : (Y * Y + X * X) * (r * r) +78 (Y * (X * c₁ - K) + Y * (X * c₁ - K) - X * X * c₂ - X * X * c₂) * r +79 ((X * c₁ - K) * (X * c₁ - K) + X * X * (c₂ * c₂) - X * X * ρ) = 080 linear_combination X * X * circle +81 (X * c₁ + X * c₁ - K + Y * r - X * q) * line82 have αIn : Y * Y + X * X ∈ S83 exact RealSubfield.add_mem with *84 have βIn : Y * (X * c₁ - K) + Y * (X * c₁ - K) - X * X * c₂ - X * X * c₂ ∈ S85 exact RealSubfield.sub_mem with *86 have γIn : (X * c₁ - K) * (X * c₁ - K) + X * X * (c₂ * c₂) - X * X * ρ ∈ S87 exact RealSubfield.sub_mem with *88 have rIn := ConstructiblyClosed.quadratic_root89 with closed, αIn, βIn, γIn, leading, root90 have inverse := realInvValue.mul_left with nonzero91 have inverseIn := RealSubfield.inv_mem with field, XIn, nonzero92 have solved : q = (K - Y * r) * realInvValue(X)93 linear_combination (-q) * inverse + realInvValue(X) * line94 constructor95 . rw solved96 exact RealSubfield.mul_mem with *97 . exact rIn9899-- The three intersections100101-- Two lines: p = a + t(b − a) = c + u(d − c) is a linear system in t, u102-- with determinant directionCross(a, b, c, d), solved for t by Cramer.103theorem lineLine.coordinates_closed (S : Set) (a b c d p : Point)104 (t u : Real)105 requires closed : ConstructiblyClosed(S)106 requires aIn : x(a) ∈ S ∧ y(a) ∈ S107 requires bIn : x(b) ∈ S ∧ y(b) ∈ S108 requires cIn : x(c) ∈ S ∧ y(c) ∈ S109 requires dIn : x(d) ∈ S ∧ y(d) ∈ S110 requires notParallel : directionCross(a, b, c, d) ≠ 0111 requires onFirst : OnLine(p, a, b, t)112 requires onSecond : OnLine(p, c, d, u)113 shows x(p) ∈ S ∧ y(p) ∈ S114proof115 have field : RealSubfield(S) := closed.left116 rcases aIn with ⟨axIn, ayIn⟩117 rcases bIn with ⟨bxIn, byIn⟩118 rcases cIn with ⟨cxIn, cyIn⟩119 rcases dIn with ⟨dxIn, dyIn⟩120 rcases onFirst with ⟨firstX, firstY⟩121 rcases onSecond with ⟨secondX, secondY⟩122 have crossIn : directionCross(a, b, c, d) ∈ S123 unfold directionCross124 exact RealSubfield.sub_mem with *125 have inverseIn := RealSubfield.inv_mem with field, crossIn, notParallel126 have inverse := realInvValue.mul_left with notParallel127 -- Cramer's rule for the parameter t.128 have solved : t = realInvValue(directionCross(a, b, c, d)) *129 ((x(c) - x(a)) * (y(d) - y(c)) - (y(c) - y(a)) * (x(d) - x(c)))130 have crossEq : directionCross(a, b, c, d) =131 (x(b) - x(a)) * (y(d) - y(c)) - (y(b) - y(a)) * (x(d) - x(c))132 unfold directionCross133 rfl134 linear_combination (-t) * inverse +135 (t * realInvValue(directionCross(a, b, c, d))) * crossEq -136 (realInvValue(directionCross(a, b, c, d)) * (y(d) - y(c))) * firstX +137 (realInvValue(directionCross(a, b, c, d)) * (y(d) - y(c))) * secondX +138 (realInvValue(directionCross(a, b, c, d)) * (x(d) - x(c))) * firstY -139 (realInvValue(directionCross(a, b, c, d)) * (x(d) - x(c))) * secondY140 have tIn : t ∈ S141 rw solved142 exact RealSubfield.mul_mem with *143 constructor144 . rw firstX145 exact RealSubfield.add_mem with *146 . rw firstY147 exact RealSubfield.add_mem with *148149-- A line and a circle: substituting p = a + t(b − a) into the circle150-- equation gives αt² + βt + γ = 0 with α = distanceSquared(a, b) > 0.151theorem lineCircle.coordinates_closed (S : Set) (a b c w p : Point)152 (t : Real)153 requires closed : ConstructiblyClosed(S)154 requires aIn : x(a) ∈ S ∧ y(a) ∈ S155 requires bIn : x(b) ∈ S ∧ y(b) ∈ S156 requires cIn : x(c) ∈ S ∧ y(c) ∈ S157 requires wIn : x(w) ∈ S ∧ y(w) ∈ S158 requires distinct : a ≠ b159 requires onLine : OnLine(p, a, b, t)160 requires onCircle : OnCircle(p, c, w)161 shows x(p) ∈ S ∧ y(p) ∈ S162proof163 have field : RealSubfield(S) := closed.left164 rcases aIn with ⟨axIn, ayIn⟩165 rcases bIn with ⟨bxIn, byIn⟩166 rcases cIn with ⟨cxIn, cyIn⟩167 rcases wIn with ⟨wxIn, wyIn⟩168 rcases onLine with ⟨lineX, lineY⟩169 unfold OnCircle at onCircle170 have pointRadius := distanceSquared.coordinates at p, c with *171 have throughRadius := distanceSquared.coordinates at w, c with *172 rw [pointRadius, throughRadius, lineX, lineY] at onCircle173 -- The leading coefficient is the squared length of ab.174 have positive := distanceSquared.positive at a, b with distinct175 have leading := distanceSquared.coordinates at a, b with *176 rw leading at positive177 have nonzero : (x(b) - x(a)) * (x(b) - x(a)) +178 (y(b) - y(a)) * (y(b) - y(a)) ≠ 0179 intro zero180 linarith181 have root : ((x(b) - x(a)) * (x(b) - x(a)) +182 (y(b) - y(a)) * (y(b) - y(a))) * (t * t) +183 ((x(b) - x(a)) * (x(a) - x(c)) + (y(b) - y(a)) * (y(a) - y(c)) +184 ((x(b) - x(a)) * (x(a) - x(c)) + (y(b) - y(a)) * (y(a) - y(c)))) * t +185 ((x(a) - x(c)) * (x(a) - x(c)) + (y(a) - y(c)) * (y(a) - y(c)) -186 ((x(c) - x(w)) * (x(c) - x(w)) + (y(c) - y(w)) * (y(c) - y(w)))) = 0187 linear_combination onCircle188 have αIn : (x(b) - x(a)) * (x(b) - x(a)) +189 (y(b) - y(a)) * (y(b) - y(a)) ∈ S190 exact RealSubfield.add_mem with *191 have βIn : (x(b) - x(a)) * (x(a) - x(c)) + (y(b) - y(a)) * (y(a) - y(c)) +192 ((x(b) - x(a)) * (x(a) - x(c)) + (y(b) - y(a)) * (y(a) - y(c))) ∈ S193 exact RealSubfield.add_mem with *194 have γIn : (x(a) - x(c)) * (x(a) - x(c)) + (y(a) - y(c)) * (y(a) - y(c)) -195 ((x(c) - x(w)) * (x(c) - x(w)) + (y(c) - y(w)) * (y(c) - y(w))) ∈ S196 exact RealSubfield.sub_mem with *197 have tIn := ConstructiblyClosed.quadratic_root198 with closed, αIn, βIn, γIn, nonzero, root199 constructor200 . rw lineX201 exact RealSubfield.add_mem with *202 . rw lineY203 exact RealSubfield.add_mem with *204205-- Two circles: the difference of the circle equations is linear in the206-- coordinates of p with a nonzero coefficient, which eliminates one207-- coordinate and leaves a quadratic over S in the other.208theorem circleCircle.coordinates_closed (S : Set) (c w e v p : Point)209 requires closed : ConstructiblyClosed(S)210 requires cIn : x(c) ∈ S ∧ y(c) ∈ S211 requires wIn : x(w) ∈ S ∧ y(w) ∈ S212 requires eIn : x(e) ∈ S ∧ y(e) ∈ S213 requires vIn : x(v) ∈ S ∧ y(v) ∈ S214 requires centers : c ≠ e215 requires onFirst : OnCircle(p, c, w)216 requires onSecond : OnCircle(p, e, v)217 shows x(p) ∈ S ∧ y(p) ∈ S218proof219 have field : RealSubfield(S) := closed.left220 rcases cIn with ⟨cxIn, cyIn⟩221 rcases wIn with ⟨wxIn, wyIn⟩222 rcases eIn with ⟨exIn, eyIn⟩223 rcases vIn with ⟨vxIn, vyIn⟩224 unfold OnCircle at onFirst225 unfold OnCircle at onSecond226 have firstRadius := distanceSquared.coordinates at p, c with *227 have firstThrough := distanceSquared.coordinates at w, c with *228 have secondRadius := distanceSquared.coordinates at p, e with *229 have secondThrough := distanceSquared.coordinates at v, e with *230 rw [firstRadius, firstThrough] at onFirst231 rw [secondRadius, secondThrough] at onSecond232 -- The radical line of the two circles.233 have line : ((x(e) - x(c)) + (x(e) - x(c))) * x(p) +234 ((y(e) - y(c)) + (y(e) - y(c))) * y(p) =235 (x(c) - x(w)) * (x(c) - x(w)) + (y(c) - y(w)) * (y(c) - y(w)) -236 ((x(e) - x(v)) * (x(e) - x(v)) + (y(e) - y(v)) * (y(e) - y(v))) +237 x(e) * x(e) + y(e) * y(e) - x(c) * x(c) - y(c) * y(c)238 linear_combination onFirst - onSecond239 have XIn : (x(e) - x(c)) + (x(e) - x(c)) ∈ S240 exact RealSubfield.add_mem with *241 have YIn : (y(e) - y(c)) + (y(e) - y(c)) ∈ S242 exact RealSubfield.add_mem with *243 have KIn : (x(c) - x(w)) * (x(c) - x(w)) + (y(c) - y(w)) * (y(c) - y(w)) -244 ((x(e) - x(v)) * (x(e) - x(v)) + (y(e) - y(v)) * (y(e) - y(v))) +245 x(e) * x(e) + y(e) * y(e) - x(c) * x(c) - y(c) * y(c) ∈ S246 exact RealSubfield.sub_mem with *247 have ρIn : (x(c) - x(w)) * (x(c) - x(w)) + (y(c) - y(w)) * (y(c) - y(w)) ∈ S248 exact RealSubfield.add_mem with *249 by_cases degenerate : (x(e) - x(c)) + (x(e) - x(c)) = 0250 . -- The radical line is horizontal, so eliminate x(p) instead.251 have vertical : (y(e) - y(c)) + (y(e) - y(c)) ≠ 0252 intro zero253 apply centers254 have horizontal : x(c) = x(e) by linarith255 have upright : y(c) = y(e) by linarith256 exact point.ext with horizontal, upright257 have swapped : ((y(e) - y(c)) + (y(e) - y(c))) * y(p) +258 ((x(e) - x(c)) + (x(e) - x(c))) * x(p) =259 (x(c) - x(w)) * (x(c) - x(w)) + (y(c) - y(w)) * (y(c) - y(w)) -260 ((x(e) - x(v)) * (x(e) - x(v)) + (y(e) - y(v)) * (y(e) - y(v))) +261 x(e) * x(e) + y(e) * y(e) - x(c) * x(c) - y(c) * y(c)262 linear_combination line263 have circle : (y(c) - y(p)) * (y(c) - y(p)) +264 (x(c) - x(p)) * (x(c) - x(p)) =265 (x(c) - x(w)) * (x(c) - x(w)) + (y(c) - y(w)) * (y(c) - y(w))266 linear_combination onFirst267 obtain ⟨yIn, xIn⟩ := ConstructiblyClosed.circle_line_root268 with closed, YIn, XIn, KIn, cyIn, cxIn, ρIn, vertical, swapped, circle269 exact ⟨xIn, yIn⟩270 . exact ConstructiblyClosed.circle_line_root271 with closed, XIn, YIn, KIn, cxIn, cyIn, ρIn, degenerate, line, onFirst272