All files / web/src/components/toys/euclid/propositions prop4.ts

73.91% Statements 272/368
100% Branches 0/0
0% Functions 0/2
73.91% Lines 272/368

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 3691x 1x 1x                                                     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                                                                                                             1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
import type { PropositionDef, ConstructionElement, TutorialSubStep } from '../types'
import { BYRNE } from '../types'
 
function getProp4Tutorial(isTouch: boolean): TutorialSubStep[][] {
  const tapHold = isTouch ? 'Tap and hold' : 'Click and hold'

  return [
    // ── Step 0: Draw segment EF (straightedge) ──
    [
      {
        instruction: `${tapHold} point {pt:E}`,
        speech: isTouch
          ? "We're given two triangles with two sides and the included angle equal. Complete triangle DEF by pressing and holding on point E."
          : "We're given two triangles with two sides and the included angle equal. Complete triangle DEF by clicking and holding on point E.",
        hint: { type: 'point', pointId: 'pt-E' },
        advanceOn: null,
      },
    ],
    // ── Step 1: Superposition (drag triangle ABC onto DEF) ──
    [
      {
        instruction: 'Drag triangle ABC onto triangle DEF',
        speech: 'Now let us see if the triangles coincide. Drag the triangle across to the other.',
        hint: { type: 'triangle-highlight', ids: ['pt-A', 'pt-B', 'pt-C'] },
        advanceOn: null,
      },
    ],
  ]
}
 
/**
 * Proposition I.4 — SAS Congruence (Theorem)
 *
 * If two triangles have two sides equal to two sides respectively,
 * and have the angles contained by the equal straight lines equal,
 * then they also have the base equal to the base, the triangle equals
 * the triangle, and the remaining angles equal the remaining angles.
 *
 * Setup: Two triangles ABC (left) and DEF (right).
 *   Given: AB = DE, AC = DF, ∠BAC = ∠EDF
 *   Triangle DEF is missing segment EF.
 *   The user draws EF, and C.N.4 (superposition) derives BC = EF.
 *
 * Coordinates are constructed so that the equalities hold exactly:
 *   DEF is formed by rotating the vectors (B−A) and (C−A) by angle θ
 *   and translating to D.
 */
 
// ── Default positions ──
const DEFAULT_A = { x: -4, y: -0.5 }
const DEFAULT_B = { x: -6.2, y: -1.5 }
const DEFAULT_C = { x: -2.8, y: 1.8 }
const DEFAULT_D = { x: 2.5, y: -0.5 }
const THETA = 0.4 // rotation angle in radians
 
/**
 * Recompute all given elements from current draggable point positions.
 * E and F are derived from A, B, C, D to maintain SAS invariants:
 *   E = D + Rot(θ)·(B−A), F = D + Rot(θ)·(C−A)
 */
export function computeProp4GivenElements(
  positions: Map<string, { x: number; y: number }>
): ConstructionElement[] {
  const A = positions.get('pt-A') ?? DEFAULT_A
  const B = positions.get('pt-B') ?? DEFAULT_B
  const C = positions.get('pt-C') ?? DEFAULT_C
  const D = positions.get('pt-D') ?? DEFAULT_D

  const cosT = Math.cos(THETA)
  const sinT = Math.sin(THETA)

  const E = {
    x: D.x + cosT * (B.x - A.x) - sinT * (B.y - A.y),
    y: D.y + sinT * (B.x - A.x) + cosT * (B.y - A.y),
  }
  const F = {
    x: D.x + cosT * (C.x - A.x) - sinT * (C.y - A.y),
    y: D.y + sinT * (C.x - A.x) + cosT * (C.y - A.y),
  }

  return [
    // Triangle ABC — all 3 points + 3 segments
    { kind: 'point', id: 'pt-A', x: A.x, y: A.y, label: 'A', color: BYRNE.given, origin: 'given' },
    { kind: 'point', id: 'pt-B', x: B.x, y: B.y, label: 'B', color: BYRNE.given, origin: 'given' },
    { kind: 'point', id: 'pt-C', x: C.x, y: C.y, label: 'C', color: BYRNE.given, origin: 'given' },
    {
      kind: 'segment',
      id: 'seg-AB',
      fromId: 'pt-A',
      toId: 'pt-B',
      color: BYRNE.given,
      origin: 'given',
    },
    {
      kind: 'segment',
      id: 'seg-AC',
      fromId: 'pt-A',
      toId: 'pt-C',
      color: BYRNE.given,
      origin: 'given',
    },
    {
      kind: 'segment',
      id: 'seg-BC',
      fromId: 'pt-B',
      toId: 'pt-C',
      color: BYRNE.given,
      origin: 'given',
    },
    // Triangle DEF — 3 points + 2 segments (EF missing — user draws it)
    { kind: 'point', id: 'pt-D', x: D.x, y: D.y, label: 'D', color: BYRNE.given, origin: 'given' },
    { kind: 'point', id: 'pt-E', x: E.x, y: E.y, label: 'E', color: BYRNE.given, origin: 'given' },
    { kind: 'point', id: 'pt-F', x: F.x, y: F.y, label: 'F', color: BYRNE.given, origin: 'given' },
    {
      kind: 'segment',
      id: 'seg-DE',
      fromId: 'pt-D',
      toId: 'pt-E',
      color: BYRNE.given,
      origin: 'given',
    },
    {
      kind: 'segment',
      id: 'seg-DF',
      fromId: 'pt-D',
      toId: 'pt-F',
      color: BYRNE.given,
      origin: 'given',
    },
  ] as ConstructionElement[]
}
 
// ── Compute initial positions ──
const cosT = Math.cos(THETA)
const sinT = Math.sin(THETA)
const baDx = DEFAULT_B.x - DEFAULT_A.x
const baDy = DEFAULT_B.y - DEFAULT_A.y
const E = {
  x: DEFAULT_D.x + cosT * baDx - sinT * baDy,
  y: DEFAULT_D.y + sinT * baDx + cosT * baDy,
}
const caDx = DEFAULT_C.x - DEFAULT_A.x
const caDy = DEFAULT_C.y - DEFAULT_A.y
const F = {
  x: DEFAULT_D.x + cosT * caDx - sinT * caDy,
  y: DEFAULT_D.y + sinT * caDx + cosT * caDy,
}
 
export const PROP_4: PropositionDef = {
  id: 4,
  title:
    'If two triangles have two sides and the included angle equal, the triangles are congruent',
  kind: 'theorem',
  resultSegments: [
    { fromId: 'pt-B', toId: 'pt-C' },
    { fromId: 'pt-E', toId: 'pt-F' },
  ],
  givenFacts: [
    {
      left: { a: 'pt-A', b: 'pt-B' },
      right: { a: 'pt-D', b: 'pt-E' },
      statement: 'AB = DE',
    },
    {
      left: { a: 'pt-A', b: 'pt-C' },
      right: { a: 'pt-D', b: 'pt-F' },
      statement: 'AC = DF',
    },
  ],
  givenAngles: [
    { spec: { vertex: 'pt-A', ray1End: 'pt-B', ray2End: 'pt-C' }, color: BYRNE.red },
    { spec: { vertex: 'pt-D', ray1End: 'pt-E', ray2End: 'pt-F' }, color: BYRNE.red },
  ],
  equalAngles: [
    [
      { vertex: 'pt-A', ray1End: 'pt-B', ray2End: 'pt-C' },
      { vertex: 'pt-D', ray1End: 'pt-E', ray2End: 'pt-F' },
    ],
  ],
  givenAngleFacts: [
    {
      left: { vertex: 'pt-A', ray1: 'pt-B', ray2: 'pt-C' },
      right: { vertex: 'pt-D', ray1: 'pt-E', ray2: 'pt-F' },
      statement: '∠BAC = ∠EDF',
    },
  ],
  theoremConclusion: '△ABC = △DEF\n∠ABC = ∠DEF, ∠ACB = ∠DFE',
  superpositionFlash: {
    pairs: [
      { src: 'pt-A', tgt: 'pt-D' },
      { src: 'pt-B', tgt: 'pt-E' },
      { src: 'pt-C', tgt: 'pt-F' },
    ],
    triA: ['pt-A', 'pt-B', 'pt-C'],
    triB: ['pt-D', 'pt-E', 'pt-F'],
  },
  draggablePointIds: ['pt-A', 'pt-B', 'pt-C', 'pt-D'],
  computeGivenElements: computeProp4GivenElements,
  givenElements: [
    // Triangle ABC — all 3 points + 3 segments
    {
      kind: 'point',
      id: 'pt-A',
      x: DEFAULT_A.x,
      y: DEFAULT_A.y,
      label: 'A',
      color: BYRNE.given,
      origin: 'given',
    },
    {
      kind: 'point',
      id: 'pt-B',
      x: DEFAULT_B.x,
      y: DEFAULT_B.y,
      label: 'B',
      color: BYRNE.given,
      origin: 'given',
    },
    {
      kind: 'point',
      id: 'pt-C',
      x: DEFAULT_C.x,
      y: DEFAULT_C.y,
      label: 'C',
      color: BYRNE.given,
      origin: 'given',
    },
    {
      kind: 'segment',
      id: 'seg-AB',
      fromId: 'pt-A',
      toId: 'pt-B',
      color: BYRNE.given,
      origin: 'given',
    },
    {
      kind: 'segment',
      id: 'seg-AC',
      fromId: 'pt-A',
      toId: 'pt-C',
      color: BYRNE.given,
      origin: 'given',
    },
    {
      kind: 'segment',
      id: 'seg-BC',
      fromId: 'pt-B',
      toId: 'pt-C',
      color: BYRNE.given,
      origin: 'given',
    },
    // Triangle DEF — 3 points + 2 segments (EF missing — user draws it)
    {
      kind: 'point',
      id: 'pt-D',
      x: DEFAULT_D.x,
      y: DEFAULT_D.y,
      label: 'D',
      color: BYRNE.given,
      origin: 'given',
    },
    { kind: 'point', id: 'pt-E', x: E.x, y: E.y, label: 'E', color: BYRNE.given, origin: 'given' },
    { kind: 'point', id: 'pt-F', x: F.x, y: F.y, label: 'F', color: BYRNE.given, origin: 'given' },
    {
      kind: 'segment',
      id: 'seg-DE',
      fromId: 'pt-D',
      toId: 'pt-E',
      color: BYRNE.given,
      origin: 'given',
    },
    {
      kind: 'segment',
      id: 'seg-DF',
      fromId: 'pt-D',
      toId: 'pt-F',
      color: BYRNE.given,
      origin: 'given',
    },
  ] as ConstructionElement[],
  steps: [
    {
      instruction: 'Join {pt:E} to {pt:F}',
      expected: { type: 'straightedge', fromId: 'pt-E', toId: 'pt-F' },
      highlightIds: ['pt-E', 'pt-F'],
      tool: 'straightedge',
      citation: 'Post.1',
    },
    {
      instruction: 'Place triangle {tri:ABC} onto triangle {tri:DEF}',
      expected: {
        type: 'superposition',
        src: ['pt-A', 'pt-B', 'pt-C'],
        tgt: ['pt-D', 'pt-E', 'pt-F'],
        mapping: [
          ['pt-A', 'pt-D'],
          ['pt-B', 'pt-E'],
          ['pt-C', 'pt-F'],
        ],
        establishes: {
          congruence: { statement: '△ABC ≅ △DEF' },
          cascade: [
            {
              kind: 'segment-equality',
              params: { leftA: 'pt-B', leftB: 'pt-C', rightA: 'pt-E', rightB: 'pt-F' },
              statement: 'BC = EF',
              justification: 'C.N.4: Triangles coincide by superposition',
            },
            {
              kind: 'angle-equality',
              params: {
                leftVertex: 'pt-B',
                leftRay1: 'pt-A',
                leftRay2: 'pt-C',
                rightVertex: 'pt-E',
                rightRay1: 'pt-D',
                rightRay2: 'pt-F',
              },
              statement: '∠ABC = ∠DEF',
              justification: 'C.N.4: Remaining angles coincide',
            },
            {
              kind: 'angle-equality',
              params: {
                leftVertex: 'pt-C',
                leftRay1: 'pt-A',
                leftRay2: 'pt-B',
                rightVertex: 'pt-F',
                rightRay1: 'pt-D',
                rightRay2: 'pt-E',
              },
              statement: '∠ACB = ∠DFE',
              justification: 'C.N.4: Remaining angles coincide',
            },
          ],
        },
      },
      highlightIds: ['pt-A', 'pt-B', 'pt-C'],
      tool: null,
      citation: 'C.N.4',
    },
  ],
  getTutorial: getProp4Tutorial,
  explorationNarration: {
    introSpeech:
      'You proved two triangles are congruent! Now drag the points to test it with different triangles.',
    pointTips: [
      {
        pointId: 'pt-A',
        speech:
          'See how triangle DEF reshapes to keep matching? The congruence holds for any vertex position.',
      },
      {
        pointId: 'pt-B',
        speech: 'Watch the matching side of DEF change too. Both triangles stay congruent!',
      },
      {
        pointId: 'pt-C',
        speech: 'See how EF always equals BC? The third side always matches.',
      },
      {
        pointId: 'pt-D',
        speech:
          "Watch the congruence hold everywhere. It doesn't matter where the second triangle sits!",
      },
    ],
  },
}