module Similarity

/*
  Similar triangles and right triangles, for coordinate geometry (#1191).

  * `tri_similar(A, B, C, D, E, F)`: triangles ABC and DEF are similar,
    with the correspondence A ↔ D, B ↔ E, C ↔ F: there is a scale
    factor k > 0 with DE = k · AB, EF = k · BC, and FD = k · CA, and the
    corresponding angles are congruent (∠A ≅ ∠D, ∠B ≅ ∠E, ∠C ≅ ∠F,
    where ∠A is ∠CAB, ∠B is ∠ABC, and ∠C is ∠BCA, as in
    `tri_congruent`). `tri_similar_parts` unfolds the definition:
    corresponding angles are congruent, and corresponding sides are
    proportional, DE/AB = EF/BC = FD/CA, with the fractions
    cross-multiplied (DE · BC = EF · AB, and so on).

  The similarity theorems of a geometry course: AA (`aa_similarity`),
  SAS (`sas_similarity`), and SSS (`sss_similarity`). Congruent
  triangles are similar (`tri_congruent_similar`), and similarity is
  symmetric and transitive (`tri_similar_symmetric`,
  `tri_similar_trans`). Other theorems: the Triangle Proportionality
  Theorem and its converse (`triangle_proportionality_theorem`: a line parallel to
  one side of a triangle divides the other two sides proportionally),
  the Pythagorean Theorem and its converse (`pythagorean_theorem`,
  `pythagorean_theorem_converse`), with the lengths `dist` (the same
  theorems with squared lengths `dist2` and a dot product are
  `point_pythagoras` and `point_pythagoras_converse` in lib/Point.pf),
  and, for the altitude CD to the hypotenuse of a right triangle ACB,
  the two triangles it forms are similar to ACB (`right_triangle_similarity_theorem`)
  and CD² = AD · DB (`geometric_mean_theorem`).

  How they are proved: the dilation with center A and scale factor
  k > 0 takes B and C to B' and C' on rays AB and AC, and triangle
  AB'C' is similar to ABC: in coordinates, its sides are k times those
  of ABC, and the dot and cross products at its vertices are k² times
  those of ABC, so its angles have the same cosines
  (`angle_measure_eq`). For AA, SAS, and SSS, choose k so that
  DE = k · AB; then AB'C' ≅ DEF by ASA, SAS, or SSS, and so ABC and
  DEF are similar. The Triangle Proportionality Theorem: a point D
  between A and B is the dilation of B with center A and factor
  s = AD/AB, and E is that of C with factor t = AE/AC, so the cross
  product of DE and BC is (t - s)(AB × AC), which is zero exactly when
  s = t, that is, when AD · EC = AE · DB.
  The Pythagorean Theorem and its converse are those of lib/Point.pf,
  and the altitude theorems follow the textbook: AA, with the common
  acute angle and the right angles.
*/

import Base
public import Parallel
import Linear
import Ring

// Triangles ABC and DEF are similar, with A ↔ D, B ↔ E, C ↔ F.
fun tri_similar(A : Point, B : Point, C : Point, D : Point, E : Point, F : Point) {
  (some k:Real. real(+0) < k and dist(D, E) = k * dist(A, B) and dist(E, F) = k * dist(B, C)
                and dist(F, D) = k * dist(C, A))
  and angle_congruent(C, A, B, F, D, E) and angle_congruent(A, B, C, D, E, F)
  and angle_congruent(B, C, A, E, F, D)
}

// The image of P under the dilation with center O and scale factor k.
private fun dilate(O : Point, P : Point, k : Real) {
  point(xcoord(O) + k * (xcoord(P) - xcoord(O)), ycoord(O) + k * (ycoord(P) - ycoord(O)))
}

/*
  Facts about lengths and angles
*/

lemma dist_pos: all A:Point, B:Point. if not (A = B) then real(+0) < dist(A, B)
proof
  arbitrary A:Point, B:Point
  assume ne
  have nz: not (dist(A, B) = real(+0)) by { replace point_dist_zero[A, B]  ne }
  replace real_less_def
  have nz2: not (real(+0) = dist(A, B)) by { assume z  apply nz to symmetric z }
  (point_dist_nonneg[A, B], nz2)
end

// If DE² = k² AB² with k ≥ 0, then DE = k AB.
lemma dist_scale: all A:Point, B:Point, D:Point, E:Point, k:Real.
  if real(+0) ≤ k and dist2(D, E) = (k * k) * dist2(A, B) then dist(D, E) = k * dist(A, B)
proof
  arbitrary A:Point, B:Point, D:Point, E:Point, k:Real
  assume h
  have nn: real(+0) ≤ k * dist(A, B)
    by apply real_mult_nonneg to (conjunct 0 of h), point_dist_nonneg[A, B]
  have sq: (k * dist(A, B)) * (k * dist(A, B)) = dist2(D, E) by {
    have e: (k * dist(A, B)) * (k * dist(A, B)) = (k * k) * (dist(A, B) * dist(A, B)) by algebra
    replace e | point_dist_square[A, B]
    symmetric conjunct 1 of h
  }
  apply point_dist_unique[D, E, k * dist(A, B)] to nn, sq
end

lemma dist_pow: all A:Point, B:Point. dist(A, B) ^ 2 = dist2(A, B)
proof
  arbitrary A:Point, B:Point
  replace symmetric point_dist_square[A, B]
  algebra
end

lemma collinear_at: all A:Point, B:Point, C:Point.
  collinear(A, B, C) = (cross(B, A, C) = real(+0))
proof
  arbitrary A:Point, B:Point, C:Point
  have e: cross(B, A, C) = - cross(A, B, C) by { expand cross  algebra }
  have fwd: if cross(A, B, C) = real(+0) then cross(B, A, C) = real(+0)
    by { assume z  algebra with e | z }
  have bwd: if cross(B, A, C) = real(+0) then cross(A, B, C) = real(+0)
    by { assume z  algebra with e | z }
  expand collinear
  apply iff_equal to fwd, bwd
end

// An angle congruent to an angle of a triangle is an angle of a
// triangle.
lemma triangle_of_angle: all A:Point, B:Point, C:Point, D:Point, E:Point, F:Point.
  if not collinear(A, B, C) and m_angle(A, B, C) = m_angle(D, E, F)
  then not collinear(D, E, F)
proof
  arbitrary A:Point, B:Point, C:Point, D:Point, E:Point, F:Point
  assume h
  have s: deg(+0) < m_angle(A, B, C) and m_angle(A, B, C) < deg(+180)
    by apply angle_measure_strict to conjunct 0 of h
  assume c
  cases apply angle_measure_collinear[D, E, F] to c
  case z { conclude false by algebra with s | z | conjunct 1 of h }
  case st { conclude false by algebra with s | st | conjunct 1 of h }
end

// If the dot and cross products at E are c > 0 times those at B, then
// ∠ABC ≅ ∠DEF: the two angles have the same cosine.
lemma angle_scale: all A:Point, B:Point, C:Point, D:Point, E:Point, F:Point, c:Real.
  if not collinear(A, B, C) and real(+0) < c
     and dot(E, D, F) = c * dot(B, A, C) and cross(E, D, F) = c * cross(B, A, C)
  then angle_congruent(A, B, C, D, E, F)
proof
  arbitrary A:Point, B:Point, C:Point, D:Point, E:Point, F:Point, c:Real
  assume h
  have nc: not collinear(A, B, C) by conjunct 0 of h
  have cp: real(+0) < c by conjunct 1 of h
  have cz: not (c = real(+0)) by { assume z  replace z in cp }
  have q: not (cross(B, A, C) = real(+0)) by { replace symmetric collinear_at[A, B, C]  nc }
  have nd: not collinear(D, E, F) by {
    replace collinear_at[D, E, F] | conjunct 3 of h
    assume z
    apply q to replace apply real_mult_zero_iff[c, cross(B, A, C)] to cz in z
  }
  have ac: abs(c) = c by apply real_abs_nonneg_eq to apply real_less_implies_less_equal to cp
  expand angle_congruent
  replace apply angle_measure_eq[A, B, C, D, E, F] to nc, nd
        | conjunct 2 of h | conjunct 3 of h | real_abs_mult[c, cross(B, A, C)] | ac
  algebra
end

/*
  Dilations
*/

// The dilation with center A and scale factor k > 0 takes triangle ABC
// to a similar triangle AB'C'.
lemma dilate_similar: all A:Point, B:Point, C:Point, k:Real.
  if triangle(A, B, C) and real(+0) < k
  then triangle(A, dilate(A, B, k), dilate(A, C, k))
   and dist(A, dilate(A, B, k)) = k * dist(A, B)
   and dist(dilate(A, B, k), dilate(A, C, k)) = k * dist(B, C)
   and dist(dilate(A, C, k), A) = k * dist(C, A)
   and angle_congruent(C, A, B, dilate(A, C, k), A, dilate(A, B, k))
   and angle_congruent(A, B, C, A, dilate(A, B, k), dilate(A, C, k))
   and angle_congruent(B, C, A, dilate(A, B, k), dilate(A, C, k), A)
proof
  arbitrary A:Point, B:Point, C:Point, k:Real
  assume h
  have t: triangle(A, B, C) by conjunct 0 of h
  have kp: real(+0) < k by conjunct 1 of h
  have kn: real(+0) ≤ k by apply real_less_implies_less_equal to kp
  have kk: real(+0) < k * k by apply real_mult_pos to kp, kp
  have kz: not (k * k = real(+0)) by { assume z  replace z in kk }
  have tq: triangle(A, dilate(A, B, k), dilate(A, C, k)) by {
    have cr: cross(A, dilate(A, B, k), dilate(A, C, k)) = (k * k) * cross(A, B, C)
      by { expand dilate | cross  algebra }
    have q: not (cross(A, B, C) = real(+0)) by expand triangle | collinear in t
    expand triangle | collinear
    replace cr | apply real_mult_zero_iff[k * k, cross(A, B, C)] to kz
    q
  }
  have ab: dist(A, dilate(A, B, k)) = k * dist(A, B) by {
    have e: dist2(A, dilate(A, B, k)) = (k * k) * dist2(A, B) by { expand dilate | dist2  algebra }
    apply dist_scale to kn, e
  }
  have bc: dist(dilate(A, B, k), dilate(A, C, k)) = k * dist(B, C) by {
    have e: dist2(dilate(A, B, k), dilate(A, C, k)) = (k * k) * dist2(B, C)
      by { expand dilate | dist2  algebra }
    apply dist_scale to kn, e
  }
  have ca: dist(dilate(A, C, k), A) = k * dist(C, A) by {
    have e: dist2(dilate(A, C, k), A) = (k * k) * dist2(C, A) by { expand dilate | dist2  algebra }
    apply dist_scale to kn, e
  }
  have nc: not collinear(A, B, C) by expand triangle in t
  have nca: not collinear(C, A, B) by { replace point_collinear_rotate[C, A, B]  nc }
  have nbc: not collinear(B, C, A) by { replace symmetric point_collinear_rotate[A, B, C]  nc }
  have ga: angle_congruent(C, A, B, dilate(A, C, k), A, dilate(A, B, k)) by {
    have d: dot(A, dilate(A, C, k), dilate(A, B, k)) = (k * k) * dot(A, C, B)
      by { expand dilate | dot  algebra }
    have c: cross(A, dilate(A, C, k), dilate(A, B, k)) = (k * k) * cross(A, C, B)
      by { expand dilate | cross  algebra }
    apply angle_scale to nca, kk, d, c
  }
  have gb: angle_congruent(A, B, C, A, dilate(A, B, k), dilate(A, C, k)) by {
    have d: dot(dilate(A, B, k), A, dilate(A, C, k)) = (k * k) * dot(B, A, C)
      by { expand dilate | dot  algebra }
    have c: cross(dilate(A, B, k), A, dilate(A, C, k)) = (k * k) * cross(B, A, C)
      by { expand dilate | cross  algebra }
    apply angle_scale to nc, kk, d, c
  }
  have gc: angle_congruent(B, C, A, dilate(A, B, k), dilate(A, C, k), A) by {
    have d: dot(dilate(A, C, k), dilate(A, B, k), A) = (k * k) * dot(C, B, A)
      by { expand dilate | dot  algebra }
    have c: cross(dilate(A, C, k), dilate(A, B, k), A) = (k * k) * cross(C, B, A)
      by { expand dilate | cross  algebra }
    apply angle_scale to nbc, kk, d, c
  }
  (tq, ab, bc, ca, ga, gb, gc)
end

// A point D of ray AB is the dilation of B with center A and scale
// factor AD/AB.
lemma ray_dilate: all A:Point, B:Point, D:Point.
  if not (A = B) and on_ray(D, A, B)
  then some s:Real. real(+0) ≤ s and dist(A, D) = s * dist(A, B) and D = dilate(A, B, s)
proof
  arbitrary A:Point, B:Point, D:Point
  assume h
  have ab: not (A = B) by conjunct 0 of h
  have k: real(+0) < dist(A, B) by apply dist_pos to ab
  choose dist(A, D) / dist(A, B)
  have sn: real(+0) ≤ dist(A, D) / dist(A, B) by {
    replace real_div_def
    apply real_mult_nonneg
      to point_dist_nonneg[A, D], (apply real_less_implies_less_equal to apply real_inv_pos to k)
  }
  have ad: dist(A, D) = (dist(A, D) / dist(A, B)) * dist(A, B) by algebra
  have r: on_ray(dilate(A, B, dist(A, D) / dist(A, B)), A, B) by {
    have c: collinear(A, B, dilate(A, B, dist(A, D) / dist(A, B)))
      by { expand collinear | cross | dilate  algebra }
    have d: dot(A, B, dilate(A, B, dist(A, D) / dist(A, B)))
            = (dist(A, D) / dist(A, B)) * dist2(A, B)
      by { expand dot | dilate | dist2  algebra }
    replace apply ray_dot[A, B, dilate(A, B, dist(A, D) / dist(A, B))] to ab | d
    (c, apply real_mult_nonneg to sn, point_dist2_nonneg[A, B])
  }
  have e: dist(A, D) = dist(A, dilate(A, B, dist(A, D) / dist(A, B))) by {
    have e2: dist2(A, dilate(A, B, dist(A, D) / dist(A, B)))
             = ((dist(A, D) / dist(A, B)) * (dist(A, D) / dist(A, B))) * dist2(A, B)
      by { expand dilate | dist2  algebra }
    replace apply dist_scale to sn, e2
    ad
  }
  (sn, ad,
   symmetric apply point_plotting_theorem_unique[A, B, dilate(A, B, dist(A, D) / dist(A, B)), D]
     to ab, r, (conjunct 1 of h), symmetric e)
end

/*
  Similar triangles
*/

lemma similar_intro: all A:Point, B:Point, C:Point, D:Point, E:Point, F:Point, k:Real.
  if real(+0) < k and dist(D, E) = k * dist(A, B) and dist(E, F) = k * dist(B, C)
     and dist(F, D) = k * dist(C, A)
     and angle_congruent(C, A, B, F, D, E) and angle_congruent(A, B, C, D, E, F)
     and angle_congruent(B, C, A, E, F, D)
  then tri_similar(A, B, C, D, E, F)
proof
  arbitrary A:Point, B:Point, C:Point, D:Point, E:Point, F:Point, k:Real
  assume h
  have ex: some r:Real. real(+0) < r and dist(D, E) = r * dist(A, B)
                        and dist(E, F) = r * dist(B, C) and dist(F, D) = r * dist(C, A) by {
    choose k
    have kp: real(+0) < k by { conjunct 0 of h }
    (kp, (conjunct 1 of h), (conjunct 2 of h), (conjunct 3 of h))
  }
  expand tri_similar
  (ex, (conjunct 4 of h), (conjunct 5 of h), (conjunct 6 of h))
end

// Corresponding parts of similar triangles: the corresponding angles
// are congruent and the corresponding sides are proportional,
// DE/AB = EF/BC = FD/CA.
theorem tri_similar_parts: all A:Point, B:Point, C:Point, D:Point, E:Point, F:Point.
  if tri_similar(A, B, C, D, E, F)
  then dist(D, E) * dist(B, C) = dist(E, F) * dist(A, B)
   and dist(E, F) * dist(C, A) = dist(F, D) * dist(B, C)
   and dist(F, D) * dist(A, B) = dist(D, E) * dist(C, A)
   and angle_congruent(C, A, B, F, D, E) and angle_congruent(A, B, C, D, E, F)
   and angle_congruent(B, C, A, E, F, D)
proof
  arbitrary A:Point, B:Point, C:Point, D:Point, E:Point, F:Point
  assume h
  have s: (some k:Real. real(+0) < k and dist(D, E) = k * dist(A, B)
                        and dist(E, F) = k * dist(B, C) and dist(F, D) = k * dist(C, A))
          and angle_congruent(C, A, B, F, D, E) and angle_congruent(A, B, C, D, E, F)
          and angle_congruent(B, C, A, E, F, D) by expand tri_similar in h
  obtain k where hk from conjunct 0 of s
  have p1: dist(D, E) * dist(B, C) = dist(E, F) * dist(A, B)
    by { replace conjunct 1 of hk | conjunct 2 of hk  algebra }
  have p2: dist(E, F) * dist(C, A) = dist(F, D) * dist(B, C)
    by { replace conjunct 2 of hk | conjunct 3 of hk  algebra }
  have p3: dist(F, D) * dist(A, B) = dist(D, E) * dist(C, A)
    by { replace conjunct 3 of hk | conjunct 1 of hk  algebra }
  (p1, p2, p3, (conjunct 1 of s), (conjunct 2 of s), (conjunct 3 of s))
end

// If the dilation of ABC with center A and factor k is congruent to
// DEF, then ABC and DEF are similar.
lemma similar_of_dilate: all A:Point, B:Point, C:Point, D:Point, E:Point, F:Point, k:Real.
  if triangle(A, B, C) and real(+0) < k
     and tri_congruent(A, dilate(A, B, k), dilate(A, C, k), D, E, F)
  then tri_similar(A, B, C, D, E, F)
proof
  arbitrary A:Point, B:Point, C:Point, D:Point, E:Point, F:Point, k:Real
  assume h
  have dl: triangle(A, dilate(A, B, k), dilate(A, C, k))
           and dist(A, dilate(A, B, k)) = k * dist(A, B)
           and dist(dilate(A, B, k), dilate(A, C, k)) = k * dist(B, C)
           and dist(dilate(A, C, k), A) = k * dist(C, A)
           and angle_congruent(C, A, B, dilate(A, C, k), A, dilate(A, B, k))
           and angle_congruent(A, B, C, A, dilate(A, B, k), dilate(A, C, k))
           and angle_congruent(B, C, A, dilate(A, B, k), dilate(A, C, k), A)
    by apply dilate_similar to (conjunct 0 of h), (conjunct 1 of h)
  have cg: seg_congruent(A, dilate(A, B, k), D, E)
           and seg_congruent(dilate(A, B, k), dilate(A, C, k), E, F)
           and seg_congruent(dilate(A, C, k), A, F, D)
           and angle_congruent(dilate(A, C, k), A, dilate(A, B, k), F, D, E)
           and angle_congruent(A, dilate(A, B, k), dilate(A, C, k), D, E, F)
           and angle_congruent(dilate(A, B, k), dilate(A, C, k), A, E, F, D)
    by apply cpctc to conjunct 2 of h
  have de: dist(D, E) = k * dist(A, B)
    by transitive (symmetric (replace point_seg_congruent_dist in conjunct 0 of cg)) (conjunct 1 of dl)
  have ef: dist(E, F) = k * dist(B, C)
    by transitive (symmetric (replace point_seg_congruent_dist in conjunct 1 of cg)) (conjunct 2 of dl)
  have fd: dist(F, D) = k * dist(C, A)
    by transitive (symmetric (replace point_seg_congruent_dist in conjunct 2 of cg)) (conjunct 3 of dl)
  have ga: angle_congruent(C, A, B, F, D, E) by {
    expand angle_congruent
    transitive (expand angle_congruent in conjunct 4 of dl) (expand angle_congruent in conjunct 3 of cg)
  }
  have gb: angle_congruent(A, B, C, D, E, F) by {
    expand angle_congruent
    transitive (expand angle_congruent in conjunct 5 of dl) (expand angle_congruent in conjunct 4 of cg)
  }
  have gc: angle_congruent(B, C, A, E, F, D) by {
    expand angle_congruent
    transitive (expand angle_congruent in conjunct 6 of dl) (expand angle_congruent in conjunct 5 of cg)
  }
  apply similar_intro to (conjunct 1 of h), de, ef, fd, ga, gb, gc
end

// The Side-Side-Side Similarity Theorem: if DE = k · AB, EF = k · BC,
// and FD = k · CA for some k > 0, then △ABC ~ △DEF.
theorem sss_similarity: all A:Point, B:Point, C:Point, D:Point, E:Point, F:Point, k:Real.
  if triangle(A, B, C) and real(+0) < k
     and dist(D, E) = k * dist(A, B) and dist(E, F) = k * dist(B, C) and dist(F, D) = k * dist(C, A)
  then tri_similar(A, B, C, D, E, F)
proof
  arbitrary A:Point, B:Point, C:Point, D:Point, E:Point, F:Point, k:Real
  assume h
  have dl: triangle(A, dilate(A, B, k), dilate(A, C, k))
           and dist(A, dilate(A, B, k)) = k * dist(A, B)
           and dist(dilate(A, B, k), dilate(A, C, k)) = k * dist(B, C)
           and dist(dilate(A, C, k), A) = k * dist(C, A)
           and angle_congruent(C, A, B, dilate(A, C, k), A, dilate(A, B, k))
           and angle_congruent(A, B, C, A, dilate(A, B, k), dilate(A, C, k))
           and angle_congruent(B, C, A, dilate(A, B, k), dilate(A, C, k), A)
    by apply dilate_similar to (conjunct 0 of h), (conjunct 1 of h)
  have s1: seg_congruent(A, dilate(A, B, k), D, E) by {
    replace point_seg_congruent_dist
    transitive (conjunct 1 of dl) (symmetric conjunct 2 of h)
  }
  have s2: seg_congruent(dilate(A, B, k), dilate(A, C, k), E, F) by {
    replace point_seg_congruent_dist
    transitive (conjunct 2 of dl) (symmetric conjunct 3 of h)
  }
  have s3: seg_congruent(dilate(A, C, k), A, F, D) by {
    replace point_seg_congruent_dist
    transitive (conjunct 3 of dl) (symmetric conjunct 4 of h)
  }
  apply similar_of_dilate to (conjunct 0 of h), (conjunct 1 of h),
    (apply sss_congruence to s1, s2, s3)
end

// The Side-Angle-Side Similarity Theorem: if ∠A ≅ ∠D, DE = k · AB,
// and DF = k · AC for some k > 0, then △ABC ~ △DEF.
theorem sas_similarity: all A:Point, B:Point, C:Point, D:Point, E:Point, F:Point, k:Real.
  if triangle(A, B, C) and real(+0) < k
     and dist(D, E) = k * dist(A, B) and angle_congruent(C, A, B, F, D, E)
     and dist(D, F) = k * dist(A, C)
  then tri_similar(A, B, C, D, E, F)
proof
  arbitrary A:Point, B:Point, C:Point, D:Point, E:Point, F:Point, k:Real
  assume h
  have dl: triangle(A, dilate(A, B, k), dilate(A, C, k))
           and dist(A, dilate(A, B, k)) = k * dist(A, B)
           and dist(dilate(A, B, k), dilate(A, C, k)) = k * dist(B, C)
           and dist(dilate(A, C, k), A) = k * dist(C, A)
           and angle_congruent(C, A, B, dilate(A, C, k), A, dilate(A, B, k))
           and angle_congruent(A, B, C, A, dilate(A, B, k), dilate(A, C, k))
           and angle_congruent(B, C, A, dilate(A, B, k), dilate(A, C, k), A)
    by apply dilate_similar to (conjunct 0 of h), (conjunct 1 of h)
  have s1: seg_congruent(A, dilate(A, B, k), D, E) by {
    replace point_seg_congruent_dist
    transitive (conjunct 1 of dl) (symmetric conjunct 2 of h)
  }
  have ga: angle_congruent(dilate(A, C, k), A, dilate(A, B, k), F, D, E) by {
    expand angle_congruent
    transitive (symmetric expand angle_congruent in conjunct 4 of dl)
               (expand angle_congruent in conjunct 3 of h)
  }
  have s2: seg_congruent(A, dilate(A, C, k), D, F) by {
    replace point_seg_congruent_dist | point_dist_symmetric[A, dilate(A, C, k)] | conjunct 3 of dl
          | point_dist_symmetric[C, A]
    symmetric conjunct 4 of h
  }
  apply similar_of_dilate to (conjunct 0 of h), (conjunct 1 of h),
    (apply sas_congruence to (conjunct 0 of dl), s1, ga, s2)
end

// The Angle-Angle Similarity Postulate: if ∠A ≅ ∠D and ∠B ≅ ∠E, then
// △ABC ~ △DEF.
theorem aa_similarity: all A:Point, B:Point, C:Point, D:Point, E:Point, F:Point.
  if triangle(A, B, C) and angle_congruent(C, A, B, F, D, E) and angle_congruent(A, B, C, D, E, F)
  then tri_similar(A, B, C, D, E, F)
proof
  arbitrary A:Point, B:Point, C:Point, D:Point, E:Point, F:Point
  assume h
  have t: triangle(A, B, C) by conjunct 0 of h
  have ab: not (A = B) by conjunct 0 of apply triangle_distinct to t
  have de: not (D = E) by {
    have nc: not collinear(C, A, B) by { replace point_collinear_rotate[C, A, B]  expand triangle in t }
    have nf: not collinear(F, D, E)
      by apply triangle_of_angle to nc, (expand angle_congruent in conjunct 1 of h)
    have t2: triangle(D, E, F) by { expand triangle  replace symmetric point_collinear_rotate[F, D, E]  nf }
    conjunct 0 of apply triangle_distinct to t2
  }
  have pab: real(+0) < dist(A, B) by apply dist_pos to ab
  have pde: real(+0) < dist(D, E) by apply dist_pos to de
  have kp: real(+0) < dist(D, E) / dist(A, B) by {
    replace real_div_def
    apply real_mult_pos to pde, (apply real_inv_pos to pab)
  }
  have dl: triangle(A, dilate(A, B, dist(D, E) / dist(A, B)), dilate(A, C, dist(D, E) / dist(A, B)))
           and dist(A, dilate(A, B, dist(D, E) / dist(A, B))) = (dist(D, E) / dist(A, B)) * dist(A, B)
           and dist(dilate(A, B, dist(D, E) / dist(A, B)), dilate(A, C, dist(D, E) / dist(A, B)))
               = (dist(D, E) / dist(A, B)) * dist(B, C)
           and dist(dilate(A, C, dist(D, E) / dist(A, B)), A) = (dist(D, E) / dist(A, B)) * dist(C, A)
           and angle_congruent(C, A, B, dilate(A, C, dist(D, E) / dist(A, B)), A,
                               dilate(A, B, dist(D, E) / dist(A, B)))
           and angle_congruent(A, B, C, A, dilate(A, B, dist(D, E) / dist(A, B)),
                               dilate(A, C, dist(D, E) / dist(A, B)))
           and angle_congruent(B, C, A, dilate(A, B, dist(D, E) / dist(A, B)),
                               dilate(A, C, dist(D, E) / dist(A, B)), A)
    by apply dilate_similar to t, kp
  have s1: seg_congruent(A, dilate(A, B, dist(D, E) / dist(A, B)), D, E) by {
    have e: (dist(D, E) / dist(A, B)) * dist(A, B) = dist(D, E) by algebra
    replace point_seg_congruent_dist | conjunct 1 of dl
    e
  }
  have ga: angle_congruent(dilate(A, C, dist(D, E) / dist(A, B)), A,
                           dilate(A, B, dist(D, E) / dist(A, B)), F, D, E) by {
    expand angle_congruent
    transitive (symmetric expand angle_congruent in conjunct 4 of dl)
               (expand angle_congruent in conjunct 1 of h)
  }
  have gb: angle_congruent(A, dilate(A, B, dist(D, E) / dist(A, B)),
                           dilate(A, C, dist(D, E) / dist(A, B)), D, E, F) by {
    expand angle_congruent
    transitive (symmetric expand angle_congruent in conjunct 5 of dl)
               (expand angle_congruent in conjunct 2 of h)
  }
  apply similar_of_dilate to t, kp, (apply asa_congruence to (conjunct 0 of dl), ga, s1, gb)
end

// Congruent triangles are similar, with scale factor 1.
theorem tri_congruent_similar: all A:Point, B:Point, C:Point, D:Point, E:Point, F:Point.
  if tri_congruent(A, B, C, D, E, F) then tri_similar(A, B, C, D, E, F)
proof
  arbitrary A:Point, B:Point, C:Point, D:Point, E:Point, F:Point
  assume h
  have cg: seg_congruent(A, B, D, E) and seg_congruent(B, C, E, F) and seg_congruent(C, A, F, D)
           and angle_congruent(C, A, B, F, D, E) and angle_congruent(A, B, C, D, E, F)
           and angle_congruent(B, C, A, E, F, D) by apply cpctc to h
  have de: dist(A, B) = dist(D, E) by replace point_seg_congruent_dist in conjunct 0 of cg
  have ef: dist(B, C) = dist(E, F) by replace point_seg_congruent_dist in conjunct 1 of cg
  have fd: dist(C, A) = dist(F, D) by replace point_seg_congruent_dist in conjunct 2 of cg
  have one: real(+0) < real(+1) by algebra
  apply similar_intro[A, B, C, D, E, F, real(+1)]
    to one, (conclude dist(D, E) = real(+1) * dist(A, B) by algebra with de),
       (conclude dist(E, F) = real(+1) * dist(B, C) by algebra with ef),
       (conclude dist(F, D) = real(+1) * dist(C, A) by algebra with fd),
       (conjunct 3 of cg), (conjunct 4 of cg), (conjunct 5 of cg)
end

theorem tri_similar_symmetric: all A:Point, B:Point, C:Point, D:Point, E:Point, F:Point.
  if tri_similar(A, B, C, D, E, F) then tri_similar(D, E, F, A, B, C)
proof
  arbitrary A:Point, B:Point, C:Point, D:Point, E:Point, F:Point
  assume h
  have s: (some k:Real. real(+0) < k and dist(D, E) = k * dist(A, B)
                        and dist(E, F) = k * dist(B, C) and dist(F, D) = k * dist(C, A))
          and angle_congruent(C, A, B, F, D, E) and angle_congruent(A, B, C, D, E, F)
          and angle_congruent(B, C, A, E, F, D) by expand tri_similar in h
  obtain k where hk from conjunct 0 of s
  have kp: real(+0) < k by conjunct 0 of hk
  have ab: dist(A, B) = inv(k) * dist(D, E) by { replace conjunct 1 of hk  algebra }
  have bc: dist(B, C) = inv(k) * dist(E, F) by { replace conjunct 2 of hk  algebra }
  have ca: dist(C, A) = inv(k) * dist(F, D) by { replace conjunct 3 of hk  algebra }
  apply similar_intro[D, E, F, A, B, C, inv(k)]
    to (apply real_inv_pos to kp), ab, bc, ca,
       (conclude angle_congruent(F, D, E, C, A, B) by {
          expand angle_congruent  symmetric expand angle_congruent in conjunct 1 of s }),
       (conclude angle_congruent(D, E, F, A, B, C) by {
          expand angle_congruent  symmetric expand angle_congruent in conjunct 2 of s }),
       (conclude angle_congruent(E, F, D, B, C, A) by {
          expand angle_congruent  symmetric expand angle_congruent in conjunct 3 of s })
end

theorem tri_similar_trans: all A:Point, B:Point, C:Point, D:Point, E:Point, F:Point,
                               G:Point, H:Point, I:Point.
  if tri_similar(A, B, C, D, E, F) and tri_similar(D, E, F, G, H, I)
  then tri_similar(A, B, C, G, H, I)
proof
  arbitrary A:Point, B:Point, C:Point, D:Point, E:Point, F:Point, G:Point, H:Point, I:Point
  assume h
  have s1: (some k:Real. real(+0) < k and dist(D, E) = k * dist(A, B)
                         and dist(E, F) = k * dist(B, C) and dist(F, D) = k * dist(C, A))
           and angle_congruent(C, A, B, F, D, E) and angle_congruent(A, B, C, D, E, F)
           and angle_congruent(B, C, A, E, F, D) by expand tri_similar in conjunct 0 of h
  have s2: (some k:Real. real(+0) < k and dist(G, H) = k * dist(D, E)
                         and dist(H, I) = k * dist(E, F) and dist(I, G) = k * dist(F, D))
           and angle_congruent(F, D, E, I, G, H) and angle_congruent(D, E, F, G, H, I)
           and angle_congruent(E, F, D, H, I, G) by expand tri_similar in conjunct 1 of h
  obtain k1 where h1 from conjunct 0 of s1
  obtain k2 where h2 from conjunct 0 of s2
  have gh: dist(G, H) = (k2 * k1) * dist(A, B)
    by { replace conjunct 1 of h2 | conjunct 1 of h1. }
  have hi: dist(H, I) = (k2 * k1) * dist(B, C)
    by { replace conjunct 2 of h2 | conjunct 2 of h1. }
  have ig: dist(I, G) = (k2 * k1) * dist(C, A)
    by { replace conjunct 3 of h2 | conjunct 3 of h1. }
  apply similar_intro[A, B, C, G, H, I, k2 * k1]
    to (apply real_mult_pos to (conjunct 0 of h2), (conjunct 0 of h1)), gh, hi, ig,
       (conclude angle_congruent(C, A, B, I, G, H) by {
          expand angle_congruent
          transitive (expand angle_congruent in conjunct 1 of s1) (expand angle_congruent in conjunct 1 of s2) }),
       (conclude angle_congruent(A, B, C, G, H, I) by {
          expand angle_congruent
          transitive (expand angle_congruent in conjunct 2 of s1) (expand angle_congruent in conjunct 2 of s2) }),
       (conclude angle_congruent(B, C, A, H, I, G) by {
          expand angle_congruent
          transitive (expand angle_congruent in conjunct 3 of s1) (expand angle_congruent in conjunct 3 of s2) })
end

/*
  The Triangle Proportionality Theorem
*/

// The Triangle Proportionality Theorem and its converse: if D is
// between A and B, and E is between A and C, then DE ∥ BC exactly when
// AD/DB = AE/EC (cross-multiplied: AD · EC = AE · DB).
theorem triangle_proportionality_theorem: all A:Point, B:Point, C:Point, D:Point, E:Point.
  if triangle(A, B, C) and between(A, D, B) and between(A, E, C)
  then parallel(line(D, E), line(B, C)) = (dist(A, D) * dist(E, C) = dist(A, E) * dist(D, B))
proof
  arbitrary A:Point, B:Point, C:Point, D:Point, E:Point
  assume h
  have t: triangle(A, B, C) by conjunct 0 of h
  have adb: between(A, D, B) by conjunct 1 of h
  have aec: between(A, E, C) by conjunct 2 of h
  have ds: not (A = B) and not (B = C) and not (A = C) by apply triangle_distinct to t
  have q: not (cross(A, B, C) = real(+0)) by expand triangle | collinear in t
  // D = A + s (B - A) and E = A + u (C - A).
  obtain s where hs from apply ray_dilate[A, B, D]
    to (conjunct 0 of ds), (conclude on_ray(D, A, B) by { expand on_ray | on_segment  adb })
  obtain u where hu from apply ray_dilate[A, C, E]
    to (conjunct 2 of ds), (conclude on_ray(E, A, C) by { expand on_ray | on_segment  aec })
  have pad: real(+0) < dist(A, D) by apply dist_pos to conjunct 0 of apply between_distinct to adb
  have sz: not (s = real(+0)) by {
    assume z
    conclude false by algebra with pad | (replace z in conjunct 1 of hs)
  }
  have de: not (D = E) by {
    assume e
    have c1: cross(A, D, C) = s * cross(A, B, C)
      by { replace conjunct 2 of hs  expand dilate | cross  algebra }
    have c2: cross(A, E, C) = real(+0) by expand collinear in (apply between_collinear to aec)
    have c3: s * cross(A, B, C) = real(+0) by { replace symmetric c1 | e  c2 }
    apply q to replace apply real_mult_zero_iff[s, cross(A, B, C)] to sz in c3
  }
  have sc: seg_cross(D, E, B, C) = (u - s) * cross(A, B, C) by {
    replace conjunct 2 of hs | conjunct 2 of hu
    expand seg_cross | dilate | cross
    algebra
  }
  // Segment addition: DB = AB - AD and EC = AC - AE.
  have db: dist(D, B) = dist(A, B) - dist(A, D)
    by algebra with apply segment_addition_postulate to adb
  have ec: dist(E, C) = dist(A, C) - dist(A, E)
    by algebra with apply segment_addition_postulate to aec
  have pab: real(+0) < dist(A, B) by apply dist_pos to conjunct 0 of ds
  have pac: real(+0) < dist(A, C) by apply dist_pos to conjunct 2 of ds
  have abc: not (dist(A, B) * dist(A, C) = real(+0)) by {
    assume z
    have p: real(+0) < dist(A, B) * dist(A, C) by apply real_mult_pos to pab, pac
    replace z in p
  }
  replace apply line_parallel_seg_cross[D, E, B, C] to de, (conjunct 1 of ds) | sc
  have fwd: if (u - s) * cross(A, B, C) = real(+0)
            then dist(A, D) * dist(E, C) = dist(A, E) * dist(D, B) by {
    assume z
    have z2: cross(A, B, C) * (u - s) = real(+0) by { replace symmetric z  algebra }
    have us: u = s by algebra with (replace apply real_mult_zero_iff[cross(A, B, C), u - s] to q in z2)
    replace ec | db | conjunct 1 of hs | conjunct 1 of hu | us
    algebra
  }
  have bwd: if dist(A, D) * dist(E, C) = dist(A, E) * dist(D, B)
            then (u - s) * cross(A, B, C) = real(+0) by {
    assume p
    have p2: (s * dist(A, B)) * (dist(A, C) - u * dist(A, C))
             = (u * dist(A, C)) * (dist(A, B) - s * dist(A, B))
      by replace ec | db | conjunct 1 of hs | conjunct 1 of hu in p
    have m: (dist(A, B) * dist(A, C)) * (s - u) = real(+0) by {
      have e: (dist(A, B) * dist(A, C)) * (s - u)
              = (s * dist(A, B)) * (dist(A, C) - u * dist(A, C))
                - (u * dist(A, C)) * (dist(A, B) - s * dist(A, B)) by algebra
      replace e | p2.
    }
    have us: u = s
      by algebra with (replace apply real_mult_zero_iff[dist(A, B) * dist(A, C), s - u] to abc in m)
    replace us.
  }
  apply iff_equal to fwd, bwd
end

/*
  The Pythagorean Theorem
*/

// The Pythagorean Theorem: if ∠C is a right angle, then
// BC² + AC² = AB², that is, a² + b² = c².
theorem pythagorean_theorem: all A:Point, B:Point, C:Point.
  if right_angle(A, C, B) then dist(B, C) ^ 2 + dist(A, C) ^ 2 = dist(A, B) ^ 2
proof
  arbitrary A:Point, B:Point, C:Point
  assume r
  have d: not (A = C) and not (C = B) and not (A = B)
    by apply triangle_distinct to apply triangle_right to r
  have bc: not (B = C) by { assume e  apply (conjunct 1 of d) to symmetric e }
  have p: dist2(A, B) = dist2(C, A) + dist2(C, B)
    by apply point_pythagoras[C, A, B]
       to replace apply angle_right_dot[A, C, B] to (conjunct 0 of d), bc in r
  replace dist_pow[B, C] | dist_pow[A, C] | dist_pow[A, B]
  algebra with p | point_dist2_symmetric[C, A] | point_dist2_symmetric[C, B]
end

// Its converse: if BC² + AC² = AB², then ∠C is a right angle.
theorem pythagorean_theorem_converse: all A:Point, B:Point, C:Point.
  if not (A = C) and not (B = C) and dist(B, C) ^ 2 + dist(A, C) ^ 2 = dist(A, B) ^ 2
  then right_angle(A, C, B)
proof
  arbitrary A:Point, B:Point, C:Point
  assume h
  have e: dist2(B, C) + dist2(A, C) = dist2(A, B)
    by replace dist_pow[B, C] | dist_pow[A, C] | dist_pow[A, B] in conjunct 2 of h
  replace apply angle_right_dot[A, C, B] to (conjunct 0 of h), (conjunct 1 of h)
  apply point_pythagoras_converse[C, A, B]
    to (conclude dist2(A, B) = dist2(C, A) + dist2(C, B)
          by algebra with e | point_dist2_symmetric[C, A] | point_dist2_symmetric[C, B])
end

/*
  The altitude to the hypotenuse
*/

// The Right Triangle Similarity Theorem: if ∠ACB is a right angle and
// CD is the altitude to the hypotenuse AB, then the two triangles it forms are similar to the original
// one: △ACB ~ △ADC and △BCA ~ △BDC.
theorem right_triangle_similarity_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 tri_similar(A, C, B, A, D, C) and tri_similar(B, C, A, B, D, C)
proof
  arbitrary A:Point, B:Point, C:Point, D:Point
  assume h
  have rc: m_angle(A, C, B) = deg(+90) by expand right_angle in conjunct 0 of h
  have adb: between(A, D, B) by conjunct 1 of h
  have t: triangle(A, C, B) by apply triangle_right to conjunct 0 of h
  have ds: not (A = D) and not (D = B) and not (A = B) by apply between_distinct to adb
  have da: not (D = A) by { assume e  apply (conjunct 0 of ds) to symmetric e }
  have ba: not (B = A) by { assume e  apply (conjunct 2 of ds) to symmetric e }
  // ∠ADC is a right angle, and so is ∠CDB, its linear pair.
  have rda: right_angle(A, D, C) by {
    expand right_angle
    replace angle_measure_symmetric[A, D, C]
    expand right_angle in conjunct 2 of h
  }
  have lp: m_angle(A, D, C) + m_angle(C, D, B) = deg(+180)
    by apply linear_pair_postulate[A, D, B, C]
       to adb, (expand triangle in apply triangle_right to rda)
  // △ACB ~ △ADC, with the common angle A.
  have s1: tri_similar(A, C, B, A, D, C) by {
    have ga: angle_congruent(B, A, C, C, A, D) by {
      have r: m_angle(D, A, C) = m_angle(B, A, C)
        by apply angle_measure_ray[B, A, C, D] to ba, da, (conclude on_ray(D, A, B) by {
             expand on_ray | on_segment  adb })
      expand angle_congruent
      replace symmetric r
      angle_measure_symmetric[D, A, C]
    }
    have gc: angle_congruent(A, C, B, A, D, C) by {
      expand angle_congruent
      replace rc
      symmetric expand right_angle in rda
    }
    apply aa_similarity to t, ga, gc
  }
  // △BCA ~ △BDC, with the common angle B.
  have s2: tri_similar(B, C, A, B, D, C) by {
    have t2: triangle(B, C, A) by { replace symmetric triangle_rotate[A, B, C] | triangle_swap[A, B, C]  t }
    have gb: angle_congruent(A, B, C, C, B, D) by {
      have r: m_angle(D, B, C) = m_angle(A, B, C)
        by apply angle_measure_ray[A, B, C, D] to (conjunct 2 of ds), (conjunct 1 of ds),
             (conclude on_ray(D, B, A) by {
                expand on_ray | on_segment
                replace symmetric between_symmetric[A, D, B]
                adb
              })
      expand angle_congruent
      replace symmetric r
      angle_measure_symmetric[D, B, C]
    }
    have gc: angle_congruent(B, C, A, B, D, C) by {
      expand angle_congruent
      algebra with rc | lp | expand right_angle in rda
                  | angle_measure_symmetric[B, C, A] | angle_measure_symmetric[B, D, C]
    }
    apply aa_similarity to t2, gb, gc
  }
  (s1, s2)
end

// The Geometric Mean (Altitude) Theorem: the altitude to the
// hypotenuse is the geometric mean of the segments of the
// hypotenuse, CD² = AD · DB.
theorem geometric_mean_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(C, D) ^ 2 = dist(A, D) * dist(D, 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
  // AD/AC = DC/CB and BD/BC = DC/CA, by the two similarities.
  have e1: dist(A, D) * dist(C, B) = dist(D, C) * dist(A, C)
    by conjunct 0 of apply tri_similar_parts to conjunct 0 of s
  have e2: dist(B, D) * dist(C, A) = dist(D, C) * dist(B, C)
    by conjunct 0 of apply tri_similar_parts to conjunct 1 of s
  have ds: not (A = C) and not (C = B) and not (A = B)
    by apply triangle_distinct to apply triangle_right to conjunct 0 of h
  have pa: real(+0) < dist(C, A)
    by apply dist_pos to (conclude not (C = A) by { assume e  apply (conjunct 0 of ds) to symmetric e })
  have pb: real(+0) < dist(C, B) by apply dist_pos to conjunct 1 of ds
  have m: (dist(C, B) * dist(C, A)) * (dist(A, D) * dist(B, D) - dist(D, C) * dist(D, C)) = real(+0) by {
    have i: (dist(C, B) * dist(C, A)) * (dist(A, D) * dist(B, D) - dist(D, C) * dist(D, C))
            = (dist(A, D) * dist(C, B)) * (dist(B, D) * dist(C, A))
              - (dist(D, C) * dist(A, C)) * (dist(D, C) * dist(B, C)) by {
      replace point_dist_symmetric[A, C] | point_dist_symmetric[B, C]
      algebra
    }
    replace i | e1 | e2.
  }
  have nz: not (dist(C, B) * dist(C, A) = real(+0)) by {
    assume z
    have p: real(+0) < dist(C, B) * dist(C, A) by apply real_mult_pos to pb, pa
    replace z in p
  }
  have g: dist(A, D) * dist(B, D) - dist(D, C) * dist(D, C) = real(+0)
    by replace apply real_mult_zero_iff[dist(C, B) * dist(C, A),
                                        dist(A, D) * dist(B, D) - dist(D, C) * dist(D, C)] to nz in m
  replace point_dist_symmetric[C, D] | point_dist_symmetric[D, B]
  algebra with g
end