module CircleSegments

/*
  Segments of chords, secants, and tangents (the power of a point), for
  coordinate geometry (#1191).

  * The Segments of Chords Theorem (`segments_of_chords_theorem`): if
    chords AB and CD of a circle meet at E, then AE · EB = CE · ED.
  * The Segments of Secants Theorem (`segments_of_secants_theorem`): if
    two secants from E meet the circle at A and B, and at C and D, then
    EA · EB = EC · ED.
  * The Segments of Secants and Tangents Theorem
    (`segments_of_secants_and_tangents_theorem`): if a tangent from E
    touches the circle at A, and a secant from E meets it at C and D,
    then EA² = EC · ED.

  All three are the power of a point: if a line through E meets the
  circle at A and B (A ≠ B), then EA · EB = |EO² - r²|
  (`circle_secant_power`).

  How it is proved: with E as the origin, a = EA, b = EB, and o = EO,
  the circle gives a² - 2 a · o + P = 0 and b² - 2 b · o + P = 0 with
  P = EO² - r², and a, b are parallel (EA × EB = 0). In the frame of a:
  a² (b · o) = (a · b)(a · o) and a² b² = (a · b)². Then
  (a · b - a²)(a · b - P) = 0, and a · b = a² only when a = 0 (as
  A ≠ B), where P = 0 too. So EA · EB = a · b = P up to sign.
*/

import Base
public import Circle
import Linear
import Ring

// x² + z² = 0 only when x = 0.
lemma square_sum_zero: all x:Real, z:Real. if x * x + z * z = real(+0) then x = real(+0)
proof
  arbitrary x:Real, z:Real
  assume h
  have xx: x * x = real(+0) by algebra with h | real_square_nonneg[x] | real_square_nonneg[z]
  cases apply real_mult_to_zero[x, x] to xx
  case a { a }
  case a { a }
end

// With U = EA², V = EB², D = EA · EB, x = EA · EO, y = EB · EO,
// z = EA × EO, k = EA × EB, R = EO², P = EO² - r², and W = AB²:
// if A and B are on the circle, k = 0, and W ≠ 0, then D = P.
lemma power_scalar: all U:Real, V:Real, D:Real, x:Real, y:Real, z:Real, k:Real, R:Real,
    P:Real, W:Real.
  if U - real(+2) * x + P = real(+0) and V - real(+2) * y + P = real(+0)
     and U * y = D * x + k * z and U * V = D * D + k * k and U * R = x * x + z * z
     and k = real(+0) and W = U + V - real(+2) * D and not (W = real(+0))
  then D = P
proof
  arbitrary U:Real, V:Real, D:Real, x:Real, y:Real, z:Real, k:Real, R:Real, P:Real, W:Real
  assume h
  have kz: k * z = real(+0) by { replace conjunct 5 of h. }
  have kk: k * k = real(+0) by { replace conjunct 5 of h. }
  have uy: U * y = D * x by algebra with conjunct 2 of h | kz
  have uv: U * V = D * D by algebra with conjunct 3 of h | kk
  // (D - U)(D - P) = 0.
  have f: (D - U) * (D - P) = real(+0) by {
    have i: (D - U) * (D - P)
            = (D * D - U * V) + U * (V - real(+2) * y + P) - D * (U - real(+2) * x + P)
              + real(+2) * (U * y - D * x) by algebra
    replace i | uv | uy | conjunct 0 of h | conjunct 1 of h.
  }
  cases apply real_mult_to_zero[D - U, D - P] to f
  case du {
    // D = U makes U (V - U) = D² - U² = 0, so U = 0 or W = V - U = 0.
    have d: D = U by algebra with du
    have uvu: U * (V - U) = real(+0) by {
      have i: U * (V - U) = U * V - U * U by algebra
      replace i | uv | d.
    }
    cases apply real_mult_to_zero[U, V - U] to uvu
    case u0 {
      have x0: x = real(+0) by {
        have s: x * x + z * z = real(+0) by { replace symmetric conjunct 4 of h | u0. }
        apply square_sum_zero[x, z] to s
      }
      algebra with conjunct 0 of h | d | u0 | x0
    }
    case vu {
      conclude false by apply (conjunct 7 of h) to (conclude W = real(+0)
        by algebra with conjunct 6 of h | vu | d)
    }
  }
  case dp { algebra with dp }
end

// The power of a point: if a line through E meets a circle at A and B,
// then EA · EB = |EO² - r²|.
theorem circle_secant_power: all c:Circle, E:Point, A:Point, B:Point.
  if on_circle(A, c) and on_circle(B, c) and not (A = B) and collinear(E, A, B)
  then dist(E, A) * dist(E, B) = abs(dist2(E, center(c)) - radius(c) * radius(c))
proof
  arbitrary c:Circle, E:Point, A:Point, B:Point
  assume h
  have ra: dist2(center(c), A) = radius(c) * radius(c) by apply circle_dist2_radius to conjunct 0 of h
  have rb: dist2(center(c), B) = radius(c) * radius(c) by apply circle_dist2_radius to conjunct 1 of h
  have k: cross(E, A, B) = real(+0) by expand collinear in conjunct 3 of h
  have bb: dot(E, B, B) = dist2(E, B) by { expand dot | dist2  algebra }
  have oo: dot(E, center(c), center(c)) = dist2(E, center(c)) by { expand dot | dist2  algebra }
  have w: not (dist2(A, B) = real(+0)) by { replace point_dist2_zero[A, B]  conjunct 2 of h }
  have d: dot(E, A, B) = dist2(E, center(c)) - radius(c) * radius(c)
    by apply power_scalar[dist2(E, A), dist2(E, B), dot(E, A, B), dot(E, A, center(c)),
                          dot(E, B, center(c)), cross(E, A, center(c)), cross(E, A, B),
                          dist2(E, center(c)), dist2(E, center(c)) - radius(c) * radius(c),
                          dist2(A, B)]
       to (conclude dist2(E, A) - real(+2) * dot(E, A, center(c))
                    + (dist2(E, center(c)) - radius(c) * radius(c)) = real(+0)
             by algebra with ra | point_dist2_dot[E, A, center(c)] | point_dist2_symmetric[A, center(c)]),
          (conclude dist2(E, B) - real(+2) * dot(E, B, center(c))
                    + (dist2(E, center(c)) - radius(c) * radius(c)) = real(+0)
             by algebra with rb | point_dist2_dot[E, B, center(c)] | point_dist2_symmetric[B, center(c)]),
          point_dot_frame[E, A, B, center(c)],
          (replace bb in point_dot_frame[E, A, B, B]),
          (replace oo in point_dot_frame[E, A, center(c), center(c)]),
          k, point_dist2_dot[E, A, B], w
  // EA · EB = |EA · EB|, as EA and EB are parallel.
  have i: (dist(E, A) * dist(E, B)) * (dist(E, A) * dist(E, B))
          = (dist(E, A) * dist(E, A)) * (dist(E, B) * dist(E, B)) by algebra
  have j: abs(dot(E, A, B)) * abs(dot(E, A, B)) = dot(E, A, B) * dot(E, A, B) by {
    replace symmetric real_abs_mult[dot(E, A, B), dot(E, A, B)]
    apply real_abs_nonneg_eq[dot(E, A, B) * dot(E, A, B)] to real_square_nonneg[dot(E, A, B)]
  }
  have lag: dist2(E, A) * dist2(E, B) = dot(E, A, B) * dot(E, A, B) by {
    have f: dist2(E, A) * dist2(E, B)
            = dot(E, A, B) * dot(E, A, B) + cross(E, A, B) * cross(E, A, B)
      by replace bb in point_dot_frame[E, A, B, B]
    have kk: cross(E, A, B) * cross(E, A, B) = real(+0) by { replace k. }
    algebra with f | kk
  }
  replace symmetric d
  apply real_square_injective[dist(E, A) * dist(E, B), abs(dot(E, A, B))]
    to (apply real_mult_nonneg to point_dist_nonneg[E, A], point_dist_nonneg[E, B]),
       real_abs_nonneg[dot(E, A, B)],
       (conclude (dist(E, A) * dist(E, B)) * (dist(E, A) * dist(E, B))
                 = abs(dot(E, A, B)) * abs(dot(E, A, B)) by {
          replace i | point_dist_square[E, A] | point_dist_square[E, B] | j | lag.
        })
end

// The Segments of Chords Theorem: if chords AB and CD meet at E, then
// AE · EB = CE · ED.
theorem segments_of_chords_theorem: all c:Circle, A:Point, B:Point, C:Point, D:Point, E:Point.
  if on_circle(A, c) and on_circle(B, c) and on_circle(C, c) and on_circle(D, c)
     and between(A, E, B) and between(C, E, D)
  then dist(A, E) * dist(E, B) = dist(C, E) * dist(E, D)
proof
  arbitrary c:Circle, A:Point, B:Point, C:Point, D:Point, E:Point
  assume h
  have ab: not (A = B) by conjunct 2 of apply between_distinct to conjunct 4 of h
  have cd: not (C = D) by conjunct 2 of apply between_distinct to conjunct 5 of h
  // E is on lines AB and CD.
  have eab: collinear(E, A, B) by {
    replace symmetric point_collinear_swap[E, B, A] | symmetric point_collinear_rotate[A, E, B]
    apply between_collinear to conjunct 4 of h
  }
  have ecd: collinear(E, C, D) by {
    replace symmetric point_collinear_swap[E, D, C] | symmetric point_collinear_rotate[C, E, D]
    apply between_collinear to conjunct 5 of h
  }
  replace point_dist_symmetric[A, E] | point_dist_symmetric[C, E]
  transitive (apply circle_secant_power[c, E, A, B] to conjunct 0 of h, conjunct 1 of h, ab, eab)
             (symmetric apply circle_secant_power[c, E, C, D] to conjunct 2 of h, conjunct 3 of h, cd, ecd)
end

// The Segments of Secants Theorem: if secants from E meet a circle at A
// and B, and at C and D, then EA · EB = EC · ED.
theorem segments_of_secants_theorem: all c:Circle, A:Point, B:Point, C:Point, D:Point, E:Point.
  if on_circle(A, c) and on_circle(B, c) and on_circle(C, c) and on_circle(D, c)
     and between(E, A, B) and between(E, C, D)
  then dist(E, A) * dist(E, B) = dist(E, C) * dist(E, D)
proof
  arbitrary c:Circle, A:Point, B:Point, C:Point, D:Point, E:Point
  assume h
  have ab: not (A = B) by conjunct 1 of apply between_distinct to conjunct 4 of h
  have cd: not (C = D) by conjunct 1 of apply between_distinct to conjunct 5 of h
  transitive (apply circle_secant_power[c, E, A, B]
                to conjunct 0 of h, conjunct 1 of h, ab, apply between_collinear to conjunct 4 of h)
             (symmetric apply circle_secant_power[c, E, C, D]
                to conjunct 2 of h, conjunct 3 of h, cd, apply between_collinear to conjunct 5 of h)
end

// The Segments of Secants and Tangents Theorem: if a tangent from E
// touches a circle at A, and a secant from E meets it at C and D, then
// EA² = EC · ED.
theorem segments_of_secants_and_tangents_theorem:
  all L:Line, c:Circle, A:Point, C:Point, D:Point, E:Point.
  if tangent_at(L, c, A) and on_line(E, L) and on_circle(C, c) and on_circle(D, c)
     and between(E, C, D)
  then dist(E, A) ^ 2 = dist(E, C) * dist(E, D)
proof
  arbitrary L:Line, c:Circle, A:Point, C:Point, D:Point, E:Point
  assume h
  have cd: not (C = D) by conjunct 1 of apply between_distinct to conjunct 4 of h
  have p: dist(E, C) * dist(E, D) = abs(dist2(E, center(c)) - radius(c) * radius(c))
    by apply circle_secant_power[c, E, C, D]
       to conjunct 2 of h, conjunct 3 of h, cd, apply between_collinear to conjunct 4 of h
  // OE² = OA² + AE², since the tangent is perpendicular to the radius.
  have ac: on_circle(A, c) by conjunct 1 of expand tangent_at in conjunct 0 of h
  have py: dist2(center(c), E) = dist2(A, center(c)) + dist2(A, E)
    by apply point_pythagoras[A, center(c), E]
       to apply circle_tangent_dot[L, c, A, E] to conjunct 0 of h, conjunct 1 of h
  have pw: dist2(E, center(c)) - radius(c) * radius(c) = dist2(E, A)
    by algebra with py | apply circle_dist2_radius to ac | point_dist2_symmetric[A, center(c)]
                  | point_dist2_symmetric[E, center(c)] | point_dist2_symmetric[A, E]
  have pa: abs(dist2(E, center(c)) - radius(c) * radius(c)) = dist2(E, A) by {
    replace pw
    apply real_abs_nonneg_eq to point_dist2_nonneg[E, A]
  }
  algebra with p | pa | point_dist_square[E, A]
end