module Proportion

/*
  Proportional segments in triangles, for coordinate geometry (#1191).

  * The Angle Bisector Theorem (`angle_bisector_theorem`): if D is on
    side BC of triangle ABC and ray AD bisects ∠BAC, then
    BD / DC = AB / AC, written BD · AC = DC · AB.
  * The Geometric Mean (Leg) Theorem (`geometric_mean_leg_theorem`): in
    a right triangle ABC with right angle C and altitude CD, each leg is
    the geometric mean of the hypotenuse and the segment of the
    hypotenuse next to it: AC² = AD · AB and BC² = BD · AB. (The
    Geometric Mean (Altitude) Theorem, CD² = AD · DB, is
    `geometric_mean_theorem` in lib/Similarity.pf.)

  How they are proved: the leg rule from the similar triangles of
  `right_triangle_similarity_theorem`. The Angle Bisector Theorem from
  coordinates, with p = AB × AD and q = AD × AC (twice the areas of
  triangles ABD and ADC): equal angles at A give AB² q² = AC² p² (the
  sines of the two angles are equal), and triangles ABD and ADC have the
  same height from A, so DB² q² = DC² p². Hence DB² AC² = DC² AB².
*/

import Base
public import Similarity
import Linear
import Ring

lemma abs_sq: all x:Real. abs(x) * abs(x) = x * x
proof
  arbitrary x:Real
  replace symmetric real_abs_mult[x, x]
  apply real_abs_nonneg_eq[x * x] to real_square_nonneg[x]
end

// If k ≠ 0 and k x = 0, then x = 0.
lemma cancel_zero: all k:Real, x:Real. if not (k = real(+0)) and k * x = real(+0) then x = real(+0)
proof
  arbitrary k:Real, x:Real
  assume h
  replace apply real_mult_zero_iff[k, x] to conjunct 0 of h in conjunct 1 of h
end

// With G = AD², b = AB², c = AC², e = DB², f = DC², x = AB · AD,
// y = AD · AC, p = AB × AD, q = AD × AC, g = DA · DB, and h = DA · DC
// (and DA × DB = p, DA × DC = -q, DB × DC = 0): if x² q² = p² y²,
// G ≠ 0, and p ≠ 0, then e c = f b.
lemma bisector_scalar: all G:Real, b:Real, c:Real, e:Real, f:Real, x:Real, y:Real,
    p:Real, q:Real, g:Real, h:Real.
  if b * G = x * x + p * p and G * c = y * y + q * q
     and G * e = g * g + p * p and G * f = h * h + q * q
     and g * q + p * h = real(+0)
     and (x * x) * (q * q) = (p * p) * (y * y)
     and not (G = real(+0)) and not (p = real(+0))
  then e * c = f * b
proof
  arbitrary G:Real, b:Real, c:Real, e:Real, f:Real, x:Real, y:Real, p:Real, q:Real,
    g:Real, h:Real
  assume H
  // DB² q² = DC² p²: the triangles ABD and ADC have the same height.
  have gq: g * q = - (p * h) by algebra with conjunct 4 of H
  have s1: e * (q * q) - f * (p * p) = real(+0) by {
    have i: G * (e * (q * q) - f * (p * p)) = (G * e) * (q * q) - (G * f) * (p * p) by algebra
    have j: (g * g + p * p) * (q * q) - (h * h + q * q) * (p * p) = (g * q) * (g * q) - (h * h) * (p * p)
      by algebra
    apply cancel_zero[G, e * (q * q) - f * (p * p)] to conjunct 6 of H, (conclude
      G * (e * (q * q) - f * (p * p)) = real(+0) by {
        replace i | conjunct 2 of H | conjunct 3 of H | j | gq
        algebra
      })
  }
  // AB² q² = AC² p²: the angles at A have equal sines.
  have s2: b * (q * q) - c * (p * p) = real(+0) by {
    have i: G * (b * (q * q) - c * (p * p)) = (b * G) * (q * q) - (G * c) * (p * p) by algebra
    have j: (x * x + p * p) * (q * q) - (y * y + q * q) * (p * p)
            = (x * x) * (q * q) - (p * p) * (y * y) by algebra
    apply cancel_zero[G, b * (q * q) - c * (p * p)] to conjunct 6 of H, (conclude
      G * (b * (q * q) - c * (p * p)) = real(+0) by {
        replace i | conjunct 0 of H | conjunct 1 of H | j | conjunct 5 of H.
      })
  }
  have pp: not (p * p = real(+0)) by {
    assume z
    cases apply real_mult_to_zero[p, p] to z
    case a { apply (conjunct 7 of H) to a }
    case a { apply (conjunct 7 of H) to a }
  }
  have i: (p * p) * (e * c - f * b)
          = e * (c * (p * p) - b * (q * q)) + b * (e * (q * q) - f * (p * p)) by algebra
  have z: (p * p) * (e * c - f * b) = real(+0) by {
    have s2': c * (p * p) - b * (q * q) = real(+0) by algebra with s2
    replace i | s2' | s1.
  }
  have d: e * c - f * b = real(+0) by apply cancel_zero to pp, z
  algebra with d
end

// The Angle Bisector Theorem: if D is on side BC of triangle ABC and ray
// AD bisects angle BAC, then BD / DC = AB / AC.
theorem angle_bisector_theorem: all A:Point, B:Point, C:Point, D:Point.
  if between(B, D, C) and angle_bisects(D, B, A, C)
  then dist(B, D) * dist(A, C) = dist(D, C) * dist(A, B)
proof
  arbitrary A:Point, B:Point, C:Point, D:Point
  assume hyp
  have bis: angle_interior(D, B, A, C) and m_angle(B, A, D) = m_angle(D, A, C)
    by expand angle_bisects in conjunct 1 of hyp
  have inr: same_side(D, C, line(A, B)) and same_side(D, B, line(A, C))
    by expand angle_interior in conjunct 0 of bis
  have o1: not on_line(D, line(A, B)) and not on_line(C, line(A, B))
    by apply line_same_side_off to conjunct 0 of inr
  have o2: not on_line(D, line(A, C)) and not on_line(B, line(A, C))
    by apply line_same_side_off to conjunct 1 of inr
  have ab: not (A = B) by {
    assume e
    apply (conjunct 1 of o2) to (conclude on_line(B, line(A, C)) by { replace symmetric e  line_on_left[A, C] })
  }
  have ac: not (A = C) by {
    assume e
    apply (conjunct 1 of o1) to (conclude on_line(C, line(A, B)) by { replace symmetric e  line_on_left[A, B] })
  }
  have ad: not (A = D) by {
    assume e
    apply (conjunct 0 of o1) to (conclude on_line(D, line(A, B)) by { replace symmetric e  line_on_left[A, B] })
  }
  // p = AB × AD ≠ 0 and q = AD × AC ≠ 0.
  have pz: not (cross(A, B, D) = real(+0)) by {
    assume z
    apply (conjunct 0 of o1)
      to (conclude on_line(D, line(A, B)) by { replace apply line_on_collinear[A, B, D] to ab  expand collinear  z })
  }
  have qz: not (cross(A, D, C) = real(+0)) by {
    have e: cross(A, D, C) = - cross(A, C, D) by { expand cross  algebra }
    assume z
    apply (conjunct 0 of o2)
      to (conclude on_line(D, line(A, C)) by {
            replace apply line_on_collinear[A, C, D] to ac
            expand collinear
            algebra with e | z
          })
  }
  // The equal angles: (AB · AD) |q| = |p| (AD · AC), so x² q² = p² y².
  have ang: dot(A, B, D) * abs(cross(A, D, C)) = abs(cross(A, B, D)) * dot(A, D, C) by {
    have c1: cross(A, B, D) = - cross(B, A, D) by { expand cross  algebra }
    have n1: not collinear(B, A, D) by {
      expand collinear
      assume z
      apply pz to (conclude cross(A, B, D) = real(+0) by algebra with c1 | z)
    }
    have n2: not collinear(D, A, C) by {
      have c3: cross(A, D, C) = - cross(D, A, C) by { expand cross  algebra }
      expand collinear
      assume z
      apply qz to (conclude cross(A, D, C) = real(+0) by algebra with c3 | z)
    }
    replace symmetric apply angle_measure_eq[B, A, D, D, A, C] to n1, n2
    conjunct 1 of bis
  }
  have sq: (dot(A, B, D) * dot(A, B, D)) * (cross(A, D, C) * cross(A, D, C))
           = (cross(A, B, D) * cross(A, B, D)) * (dot(A, D, C) * dot(A, D, C)) by {
    have i: (dot(A, B, D) * abs(cross(A, D, C))) * (dot(A, B, D) * abs(cross(A, D, C)))
            = (dot(A, B, D) * dot(A, B, D)) * (abs(cross(A, D, C)) * abs(cross(A, D, C))) by algebra
    have j: (abs(cross(A, B, D)) * dot(A, D, C)) * (abs(cross(A, B, D)) * dot(A, D, C))
            = (abs(cross(A, B, D)) * abs(cross(A, B, D))) * (dot(A, D, C) * dot(A, D, C)) by algebra
    replace symmetric abs_sq[cross(A, D, C)] | symmetric abs_sq[cross(A, B, D)] | symmetric i
          | symmetric j | ang.
  }
  // B, D, and C are collinear.
  have bdc: cross(B, D, C) = real(+0) by expand collinear in apply between_collinear to conjunct 0 of hyp
  have dd: dot(A, D, D) = dist2(A, D) by { expand dot | dist2  algebra }
  have cc: dot(A, C, C) = dist2(A, C) by { expand dot | dist2  algebra }
  have bb: dot(D, B, B) = dist2(D, B) by { expand dot | dist2  algebra }
  have dcc: dot(D, C, C) = dist2(D, C) by { expand dot | dist2  algebra }
  have da: dist2(D, A) = dist2(A, D) by point_dist2_symmetric[D, A]
  have xp: cross(D, A, B) = cross(A, B, D) by { expand cross  algebra }
  have xq: cross(D, A, C) = - cross(A, D, C) by { expand cross  algebra }
  have xb: cross(D, B, C) = real(+0) by {
    have e: cross(D, B, C) = - cross(B, D, C) by { expand cross  algebra }
    replace e | bdc.
  }
  have gz: not (dist2(A, D) = real(+0)) by { replace point_dist2_zero[A, D]  ad }
  have s: dist2(D, B) * dist2(A, C) = dist2(D, C) * dist2(A, B)
    by apply bisector_scalar[dist2(A, D), dist2(A, B), dist2(A, C), dist2(D, B), dist2(D, C),
                             dot(A, B, D), dot(A, D, C), cross(A, B, D), cross(A, D, C),
                             dot(D, A, B), dot(D, A, C)]
       to (replace dd in point_dot_frame[A, B, D, D]),
          (replace cc in point_dot_frame[A, D, C, C]),
          (replace da | bb | xp in point_dot_frame[D, A, B, B]),
          (conclude dist2(A, D) * dist2(D, C)
                    = dot(D, A, C) * dot(D, A, C) + cross(A, D, C) * cross(A, D, C) by {
             have i: (- cross(A, D, C)) * (- cross(A, D, C)) = cross(A, D, C) * cross(A, D, C)
               by algebra
             replace symmetric i | symmetric xq | symmetric dcc | symmetric da
             point_dot_frame[D, A, C, C]
           }),
          (conclude dot(D, A, B) * cross(A, D, C) + cross(A, B, D) * dot(D, A, C) = real(+0) by {
             have fr: dist2(A, D) * real(+0)
                      = dot(D, A, B) * (- cross(A, D, C)) - cross(A, B, D) * dot(D, A, C)
               by replace da | xb | xq | xp in point_cross_frame[D, A, B, C]
             have i: dot(D, A, B) * (- cross(A, D, C)) = - (dot(D, A, B) * cross(A, D, C)) by algebra
             algebra with fr | i
           }),
          sq, gz, pz
  // Take square roots.
  have i: (dist(B, D) * dist(A, C)) * (dist(B, D) * dist(A, C))
          = (dist(B, D) * dist(B, D)) * (dist(A, C) * dist(A, C)) by algebra
  have j: (dist(D, C) * dist(A, B)) * (dist(D, C) * dist(A, B))
          = (dist(D, C) * dist(D, C)) * (dist(A, B) * dist(A, B)) by algebra
  apply real_square_injective[dist(B, D) * dist(A, C), dist(D, C) * dist(A, B)]
    to (apply real_mult_nonneg to point_dist_nonneg[B, D], point_dist_nonneg[A, C]),
       (apply real_mult_nonneg to point_dist_nonneg[D, C], point_dist_nonneg[A, B]),
       (conclude (dist(B, D) * dist(A, C)) * (dist(B, D) * dist(A, C))
                 = (dist(D, C) * dist(A, B)) * (dist(D, C) * dist(A, B)) by {
          replace i | j | point_dist_square[B, D] | point_dist_square[A, C]
                | point_dist_square[D, C] | point_dist_square[A, B] | point_dist2_symmetric[B, D]
          s
        })
end

// The Geometric Mean (Leg) Theorem: in a right triangle ABC with right
// angle C and altitude CD, AC² = AD · AB and BC² = BD · AB.
theorem geometric_mean_leg_theorem: all A:Point, B:Point, C:Point, D:Point.
  if right_angle(A, C, B) and between(A, D, B) and right_angle(C, D, A)
  then dist(A, C) ^ 2 = dist(A, D) * dist(A, B) and dist(B, C) ^ 2 = dist(B, D) * dist(A, B)
proof
  arbitrary A:Point, B:Point, C:Point, D:Point
  assume h
  have s: tri_similar(A, C, B, A, D, C) and tri_similar(B, C, A, B, D, C)
    by apply right_triangle_similarity_theorem to h
  // AC / AB = AD / AC and BC / BA = BD / BC.
  have e1: dist(C, A) * dist(A, C) = dist(A, D) * dist(B, A)
    by conjunct 2 of apply tri_similar_parts to conjunct 0 of s
  have e2: dist(C, B) * dist(B, C) = dist(B, D) * dist(A, B)
    by conjunct 2 of apply tri_similar_parts to conjunct 1 of s
  have a: dist(A, C) ^ 2 = dist(A, D) * dist(A, B) by {
    have e: dist(A, C) * dist(A, C) = dist(A, D) * dist(A, B)
      by replace point_dist_symmetric[C, A] | point_dist_symmetric[B, A] in e1
    algebra with e
  }
  have b: dist(B, C) ^ 2 = dist(B, D) * dist(A, B) by {
    have e: dist(B, C) * dist(B, C) = dist(B, D) * dist(A, B)
      by replace point_dist_symmetric[C, B] in e2
    algebra with e
  }
  (a, b)
end