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

91.37% Statements 413/452
91.66% Branches 22/24
66.66% Functions 2/3
91.37% Lines 413/452

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 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 4531x 1x 1x 1x 1x 1x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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,
  ConstructionState,
  TutorialSubStep,
  EuclidNarrationOptions,
  ExpectedAction,
} from '../types'
import { BYRNE } from '../types'
import type { KidLanguageStyle } from '@/db/schema/player-session-preferences'
import { deriveSteps } from '../engine/recipe/deriveSteps'
import { RECIPE_PROP_2, PROP_2_ANNOTATIONS } from '../engine/recipe/definitions/prop2'
import { RECIPE_REGISTRY } from '../engine/recipe/definitions/registry'
import { recipeToConclusion } from '../engine/recipe/adapters'
import { evaluateRecipe } from '../engine/recipe/evaluate'
import { getPoint } from '../engine/constructionState'
 
const DEFAULT_LANGUAGE_STYLE: KidLanguageStyle = 'standard'
 
const PROP_2_STEP_INSTRUCTIONS: Record<KidLanguageStyle, string[]> = {
  simple: [
    'Draw a line from {pt:A} to {pt:B}',
    'Build an equilateral triangle on {seg:AB} ({prop:1|I.1})',
    'Draw a circle with center {pt:B} through {pt:C}',
    'From {pt:D}, extend past {pt:B} to meet the circle',
    'Draw a circle with center {pt:D} through {pt:E}',
    'From {pt:D}, extend past {pt:A} to meet the circle',
  ],
  standard: [
    'Join point {pt:A} to point {pt:B}',
    'Construct equilateral triangle on {seg:AB} ({prop:1|I.1})',
    'Draw a circle centered at {pt:B} through {pt:C}',
    'From {pt:D}, extend through {pt:B} to meet the circle at {pt:E}',
    'Draw a circle centered at {pt:D} through {pt:E}',
    'From {pt:D}, extend through {pt:A} to meet the circle at {pt:F}',
  ],
  classical: [
    'Join {pt:A} to {pt:B}',
    'Construct the equilateral triangle on {seg:AB} ({prop:1|I.1})',
    'Describe the circle with center {pt:B} and radius {seg:BC}',
    'From {pt:D}, produce past {pt:B} to meet the circle at {pt:E}',
    'Describe the circle with center {pt:D} and radius {seg:DE}',
    'From {pt:D}, produce past {pt:A} to meet the circle at {pt:F}',
  ],
}
 
function getProp2Tutorial(isTouch: boolean, options?: EuclidNarrationOptions): TutorialSubStep[][] {
  const style = options?.languageStyle ?? DEFAULT_LANGUAGE_STYLE
  const tap = isTouch ? 'Tap' : 'Click'
  const tapHold = isTouch ? 'Tap and hold' : 'Click and hold'
  const drag = isTouch ? 'Drag' : 'Drag'
  const sweep = isTouch ? 'Sweep your finger' : 'Move your mouse'
  const byStyle = (copy: Record<KidLanguageStyle, string>) => copy[style] ?? copy.standard
 
  return [
    // ── Step 0: Join A to B ──
    [
      {
        instruction: `${drag} from {pt:A} to {pt:B}`,
        speech: byStyle({
          simple: 'Postulate 1 lets us draw a straight line from any point to any point. Draw AB.',
          standard:
            'Postulate 1 lets us draw a straight line from any point to any point. Draw AB.',
          classical: 'By Postulate 1, join A to B.',
        }),
        hint: { type: 'arrow', fromId: 'pt-A', toId: 'pt-B' },
        advanceOn: null,
      },
    ],
    // ── Step 1: Construct equilateral triangle (I.1 macro) ──
    [
      {
        instruction: `${tap} point {pt:A}`,
        speech: isTouch
          ? byStyle({
              simple:
                'Now use Proposition I.1 to build an equilateral triangle on AB. Tap A to start.',
              standard:
                'Now use Proposition I.1 to construct an equilateral triangle on AB. Tap A to start.',
              classical:
                'Use Proposition I.1 to construct the equilateral triangle on AB. Tap A to begin.',
            })
          : byStyle({
              simple:
                'Now use Proposition I.1 to build an equilateral triangle on AB. Click A to start.',
              standard:
                'Now use Proposition I.1 to construct an equilateral triangle on AB. Click A to start.',
              classical:
                'Use Proposition I.1 to construct the equilateral triangle on AB. Click A to begin.',
            }),
        hint: { type: 'point', pointId: 'pt-A' },
        advanceOn: { kind: 'macro-select', index: 0 },
      },
      {
        instruction: `${tap} point {pt:B}`,
        speech: isTouch
          ? byStyle({
              simple: 'Tap B to finish. In an equilateral triangle, DA equals DB.',
              standard: 'Tap B to finish. Because the triangle is equilateral, DA equals DB.',
              classical: 'Tap B to finish. Since the triangle is equilateral, DA equals DB.',
            })
          : byStyle({
              simple: 'Click B to finish. In an equilateral triangle, DA equals DB.',
              standard: 'Click B to finish. Because the triangle is equilateral, DA equals DB.',
              classical: 'Click B to finish. Since the triangle is equilateral, DA equals DB.',
            }),
        hint: { type: 'point', pointId: 'pt-B' },
        advanceOn: null,
      },
    ],
    // ── Step 2: Circle at B through C ──
    [
      {
        instruction: `${tapHold} point {pt:B}`,
        speech: isTouch
          ? byStyle({
              simple:
                'Now draw a circle with center B and radius BC (Postulate 3). Press and hold on B.',
              standard:
                'Now describe a circle with center B and radius BC (Postulate 3). Press and hold on B.',
              classical: 'By Postulate 3, describe the circle with center B and radius BC.',
            })
          : byStyle({
              simple:
                'Now draw a circle with center B and radius BC (Postulate 3). Click and hold on B.',
              standard:
                'Now describe a circle with center B and radius BC (Postulate 3). Click and hold on B.',
              classical: 'By Postulate 3, describe the circle with center B and radius BC.',
            }),
        hint: { type: 'point', pointId: 'pt-B' },
        advanceOn: { kind: 'compass-phase', phase: 'center-set' },
      },
      {
        instruction: `${drag} to point {pt:C}`,
        speech: byStyle({
          simple: 'Drag to C to set the radius. Every point on this circle is BC away from B.',
          standard: 'Drag to C to set the radius. Every point on this circle is BC away from B.',
          classical:
            'Drag to C to set the radius. Every point on this circle is at distance BC from B.',
        }),
        hint: { type: 'arrow', fromId: 'pt-B', toId: 'pt-C' },
        advanceOn: { kind: 'compass-phase', phase: 'radius-set' },
      },
      {
        instruction: `${sweep} around`,
        speech: isTouch
          ? byStyle({
              simple: 'Sweep around to draw the circle centered at B.',
              standard: 'Sweep around to draw the circle centered at B.',
              classical: 'Carry the circle around.',
            })
          : byStyle({
              simple: 'Move around to draw the circle centered at B.',
              standard: 'Move around to draw the circle centered at B.',
              classical: 'Carry the circle around.',
            }),
        hint: { type: 'sweep', centerId: 'pt-B', radiusPointId: 'pt-C' },
        advanceOn: null,
      },
    ],
    // ── Step 3: Extend DB past B to E (Postulate 2) ──
    [
      {
        instruction: `${drag} from {pt:D} toward {pt:B}`,
        speech: byStyle({
          simple: 'Now extend line DB past B using Postulate 2. Drag from D toward B.',
          standard: 'Now extend line DB past B using Postulate 2. Drag from point D toward B.',
          classical: 'By Postulate 2, produce DB beyond B. Drag from D toward B.',
        }),
        hint: { type: 'point', pointId: 'pt-D' },
        advanceOn: { kind: 'extend-phase' as const, phase: 'extending' as const },
      },
      {
        instruction: `Extend through the circle to place {pt:E} where the line meets it`,
        speech: byStyle({
          simple:
            'Keep going past B and release to place point E where the line meets the circle. Then BE equals BC.',
          standard:
            'Continue past B and release to place E where the line meets the circle. Because E is on the circle centered at B, BE equals BC.',
          classical:
            'Continue past B and release at E, where it meets the circle. Since E is on the circle with center B, BE equals BC.',
        }),
        hint: { type: 'none' as const },
        advanceOn: null,
      },
    ],
    // ── Step 4: Circle at D through E ──
    [
      {
        instruction: `${tapHold} point {pt:D}`,
        speech: isTouch
          ? byStyle({
              simple: 'Now draw a circle with center D and radius DE. Press and hold on D.',
              standard: 'Now describe a circle with center D and radius DE. Press and hold on D.',
              classical: 'Describe the circle with center D and radius DE.',
            })
          : byStyle({
              simple: 'Now draw a circle with center D and radius DE. Click and hold on D.',
              standard: 'Now describe a circle with center D and radius DE. Click and hold on D.',
              classical: 'Describe the circle with center D and radius DE.',
            }),
        hint: { type: 'point', pointId: 'pt-D' },
        advanceOn: { kind: 'compass-phase', phase: 'center-set' },
      },
      {
        instruction: `${drag} to point {pt:E}`,
        speech: byStyle({
          simple: 'Drag to E to set the radius. Every point on this circle is DE away from D.',
          standard: 'Drag to E to set the radius. Every point on this circle is DE away from D.',
          classical:
            'Drag to E to set the radius. Every point on this circle is at distance DE from D.',
        }),
        hint: { type: 'arrow', fromId: 'pt-D', toId: 'pt-E' },
        advanceOn: { kind: 'compass-phase', phase: 'radius-set' },
      },
      {
        instruction: `${sweep} around`,
        speech: isTouch
          ? byStyle({
              simple: 'Sweep around to draw the circle centered at D.',
              standard: 'Sweep all the way around to draw the circle centered at D.',
              classical: 'Carry the circle around.',
            })
          : byStyle({
              simple: 'Move around to draw the circle centered at D.',
              standard: 'Move all the way around to draw the circle centered at D.',
              classical: 'Carry the circle around.',
            }),
        hint: { type: 'sweep', centerId: 'pt-D', radiusPointId: 'pt-E' },
        advanceOn: null,
      },
    ],
    // ── Step 5: Extend DA past A to F (Postulate 2) ──
    [
      {
        instruction: `${drag} from {pt:D} toward {pt:A}`,
        speech: byStyle({
          simple: 'Now extend line DA past A. Drag from D toward A.',
          standard: 'Now extend line DA past A using Postulate 2. Drag from point D toward A.',
          classical: 'By Postulate 2, produce DA beyond A. Drag from D toward A.',
        }),
        hint: { type: 'point', pointId: 'pt-D' },
        advanceOn: { kind: 'extend-phase' as const, phase: 'extending' as const },
      },
      {
        instruction: `Extend through the circle to place {pt:F} where the line meets it`,
        speech: byStyle({
          simple:
            'Keep going past A and release to place point F. Then DF equals DE. Subtract equals from equals to get AF = BE, and since BE = BC, AF = BC.',
          standard:
            'Continue past A and release to place F where the line meets the circle. DF equals DE, and since DA equals DB, subtract equals from equals to get AF = BE. Because BE = BC, AF = BC.',
          classical:
            'Continue past A and release at F. Then DF equals DE. Subtract equals from equals: AF = BE. And since BE = BC, AF = BC.',
        }),
        hint: { type: 'none' as const },
        advanceOn: null,
      },
    ],
  ]
}
 
/**
 * Proposition I.2: To place at a given point a straight line equal
 * to a given straight line.
 *
 * Given: Point A (the point), segment BC (the line to copy)
 *
 * Construction (using I.1 as a macro):
 * 1. Join A to B                         (Post.1)
 * 2. Construct equilateral △ ABD on AB   (I.1 macro)
 * 3. Circle at B through C               (Post.3) — meets extension of DB at E
 * 4. Mark E (Euclid's G)
 * 5. Circle at D through E               (Post.3) — meets extension of DA at F
 * 6. Mark F (Euclid's L)
 *
 * Result: AF = BC
 *
 * Element IDs at each step (given: pt-A, pt-B, pt-C, seg-BC):
 *   Step 1 → seg-2 (AB)         [seg-BC is seg count 1]
 *   Step 2 (macro I.1):
 *            pt-D  (apex — circles are internal to the proven tool)
 *            seg-3 (DA)
 *            seg-4 (DB)
 *   Step 3 → cir-1 (circle B,C)
 *   Step 4 → pt-E  (on extension of DB beyond B)
 *   Step 5 → cir-2 (circle D,E)
 *   Step 6 → pt-F  (on extension of DA beyond A)
 */
export const PROP_2: PropositionDef = {
  id: 2,
  title: 'Place a line equal to a given line at a given point',
  draggablePointIds: ['pt-A', 'pt-B', 'pt-C'],
  // completionMessage derived from proof engine at runtime
  resultSegments: [{ fromId: 'pt-A', toId: 'pt-F' }],
  givenElements: [
    {
      kind: 'point',
      id: 'pt-A',
      x: -1.5,
      y: 1.5,
      label: 'A',
      color: BYRNE.given,
      origin: 'given',
    },
    {
      kind: 'point',
      id: 'pt-B',
      x: 0,
      y: 0,
      label: 'B',
      color: BYRNE.given,
      origin: 'given',
    },
    {
      kind: 'point',
      id: 'pt-C',
      x: 1.5,
      y: 0,
      label: 'C',
      color: BYRNE.given,
      origin: 'given',
    },
    {
      kind: 'segment',
      id: 'seg-BC',
      fromId: 'pt-B',
      toId: 'pt-C',
      color: BYRNE.given,
      origin: 'given',
    },
  ] as ConstructionElement[],
  steps: deriveSteps(RECIPE_PROP_2, PROP_2_ANNOTATIONS),
  stepInstructionsByStyle: PROP_2_STEP_INSTRUCTIONS,
  getTutorial: getProp2Tutorial,
  explorationNarration: {
    introSpeech:
      'You copied a distance using only postulates, definitions, and common notions. Drag points A, B, or C to see the equalities still follow by logic.',
    pointTips: [
      {
        pointId: 'pt-A',
        speech:
          'AF always equals BC because the circles and subtractions enforce it, not because of measurement.',
      },
      {
        pointId: 'pt-B',
        speech: 'The construction moves, but DA = DB and BE = BC still hold, so AF = BC.',
      },
      {
        pointId: 'pt-C',
        speech: 'BC sets the circle radius at B, so the copied length always matches BC.',
      },
    ],
  },
  explorationNarrationByStyle: {
    simple: {
      introSpeech:
        'You copied the length using circles and straight lines. Drag A, B, or C to see AF stay equal to BC.',
      pointTips: [
        {
          pointId: 'pt-A',
          speech: 'AF stays equal to BC because the construction forces it.',
        },
        {
          pointId: 'pt-B',
          speech: 'DA = DB and BE = BC still hold, so AF = BC.',
        },
        {
          pointId: 'pt-C',
          speech: 'BC sets the circle radius at B, so the copy always matches BC.',
        },
      ],
    },
    standard: {
      introSpeech:
        'You copied a distance using only postulates, definitions, and common notions. Drag points A, B, or C to see the equalities still follow by logic.',
      pointTips: [
        {
          pointId: 'pt-A',
          speech:
            'AF always equals BC because the circles and subtractions enforce it, not because of measurement.',
        },
        {
          pointId: 'pt-B',
          speech: 'The construction moves, but DA = DB and BE = BC still hold, so AF = BC.',
        },
        {
          pointId: 'pt-C',
          speech: 'BC sets the circle radius at B, so the copied length always matches BC.',
        },
      ],
    },
    classical: {
      introSpeech:
        'You placed a line equal to the given line by postulates and common notions. Drag A, B, or C to see the equalities persist.',
      pointTips: [
        {
          pointId: 'pt-A',
          speech: 'Because DF = DE and DA = DB, the subtraction gives AF = BE.',
        },
        {
          pointId: 'pt-B',
          speech: 'Since BE = BC, and AF = BE, we have AF = BC.',
        },
        {
          pointId: 'pt-C',
          speech: 'BC fixes the radius, so the copied length always equals BC.',
        },
      ],
    },
  },
  deriveConclusion: recipeToConclusion(RECIPE_PROP_2),
  resolveStep(
    stepIndex: number,
    state: ConstructionState,
    _stepData: Map<number, Record<string, unknown>>
  ): { expected?: ExpectedAction; instruction?: string; highlightIds?: string[] } | null {
    if (stepIndex !== 3 && stepIndex !== 5) return null

    // Evaluate the full recipe using the geometry engine to get authoritative positions
    const ptB = getPoint(state, 'pt-B')
    const ptC = getPoint(state, 'pt-C')
    const ptA = getPoint(state, 'pt-A')
    if (!ptB || !ptC || !ptA) return null

    const trace = evaluateRecipe(RECIPE_PROP_2, [ptB, ptC, ptA], RECIPE_REGISTRY)
    if (!trace) return null

    // Step 3: extend DB past B → E at circle(B, BC) intersection
    if (stepIndex === 3) {
      const ptE = trace.pointMap.get('E')
      const through = getPoint(state, 'pt-B')
      if (!ptE || !through) return null
      const dist = Math.sqrt((ptE.x - through.x) ** 2 + (ptE.y - through.y) ** 2)
      return {
        expected: { type: 'extend', baseId: 'pt-D', throughId: 'pt-B', distance: dist, label: 'E' },
      }
    }

    // Step 5: extend DA past A → F at circle(D, DE) intersection
    if (stepIndex === 5) {
      const ptF = trace.pointMap.get('F')
      const through = getPoint(state, 'pt-A')
      if (!ptF || !through) return null
      const dist = Math.sqrt((ptF.x - through.x) ** 2 + (ptF.y - through.y) ** 2)
      return {
        expected: { type: 'extend', baseId: 'pt-D', throughId: 'pt-A', distance: dist, label: 'F' },
      }
    }

    return null
  },
}