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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 23x 23x 1x 1x 1x 1x 1x 1x 1x 100x 100x 100x 301x 266x 266x 301x 100x 100x 1x 1x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 49x 23x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 1x 1x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 1x 1x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 1x 1x 1x 1x 1x 1x 1x 1x 1x 621x 621x 1x 1x 16x 16x 1x 1x 4x 4x 1x 1x 6x 6x 6x 6x 6x 6x 6x 1x 1x 2x 2x 1x 1x 82x 82x 1x 1x 76x 76x | import type {
ConstructionState,
ConstructionElement,
ConstructionPoint,
ConstructionCircle,
ConstructionSegment,
ElementOrigin,
} from '../types'
import { BYRNE, BYRNE_CYCLE } from '../types'
// ── Label generation ───────────────────────────────────────────────
const LABELS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
function labelAt(index: number): string {
if (index < LABELS.length) return LABELS[index]
// Beyond Z: A₂, B₂, ...
const cycle = Math.floor(index / LABELS.length) + 1
const ch = LABELS[index % LABELS.length]
return `${ch}${cycle}`
}
// ── Pure state functions ───────────────────────────────────────────
export function createInitialState(): ConstructionState {
return { elements: [], nextLabelIndex: 0, nextColorIndex: 0 }
}
/** Add a fully-formed set of given elements (for proposition setup) */
export function initializeGiven(givenElements: ConstructionElement[]): ConstructionState {
let nextLabel = 0
const nextColor = 0
for (const el of givenElements) {
if (el.kind === 'point') {
nextLabel = Math.max(nextLabel, LABELS.indexOf(el.label) + 1)
}
}
return { elements: [...givenElements], nextLabelIndex: nextLabel, nextColorIndex: nextColor }
}
export function addPoint(
state: ConstructionState,
x: number,
y: number,
origin: ElementOrigin,
explicitLabel?: string
): { state: ConstructionState; point: ConstructionPoint } {
const label = explicitLabel ?? labelAt(state.nextLabelIndex)
const color =
origin === 'given'
? BYRNE.given
: origin === 'free'
? BYRNE.red
: BYRNE_CYCLE[state.nextColorIndex % BYRNE_CYCLE.length]
const point: ConstructionPoint = {
kind: 'point',
id: `pt-${label}`,
x,
y,
label,
color,
origin,
}
// When an explicit label is used, ensure nextLabelIndex advances past it
const labelIndex = explicitLabel ? LABELS.indexOf(explicitLabel) : -1
const nextLabelIndex = explicitLabel
? Math.max(state.nextLabelIndex, labelIndex + 1)
: state.nextLabelIndex + 1
return {
state: {
...state,
elements: [...state.elements, point],
nextLabelIndex,
nextColorIndex:
origin === 'given' || origin === 'free' ? state.nextColorIndex : state.nextColorIndex + 1,
},
point,
}
}
export function addCircle(
state: ConstructionState,
centerId: string,
radiusPointId: string
): { state: ConstructionState; circle: ConstructionCircle } {
const color = BYRNE_CYCLE[state.nextColorIndex % BYRNE_CYCLE.length]
const circle: ConstructionCircle = {
kind: 'circle',
id: `cir-${state.elements.filter((e) => e.kind === 'circle').length + 1}`,
centerId,
radiusPointId,
color,
origin: 'compass',
}
return {
state: {
...state,
elements: [...state.elements, circle],
nextColorIndex: state.nextColorIndex + 1,
},
circle,
}
}
export function addSegment(
state: ConstructionState,
fromId: string,
toId: string
): { state: ConstructionState; segment: ConstructionSegment } {
const color = BYRNE_CYCLE[state.nextColorIndex % BYRNE_CYCLE.length]
const segment: ConstructionSegment = {
kind: 'segment',
id: `seg-${state.elements.filter((e) => e.kind === 'segment').length + 1}`,
fromId,
toId,
color,
origin: 'straightedge',
}
return {
state: {
...state,
elements: [...state.elements, segment],
nextColorIndex: state.nextColorIndex + 1,
},
segment,
}
}
/** Advance label/color indices as if a point had been created, without actually creating one.
* Used when an intersection step must be skipped (geometry no longer intersects)
* to keep subsequent point labels stable during drag replay. */
export function skipPointLabel(
state: ConstructionState,
explicitLabel?: string
): ConstructionState {
const labelIndex = explicitLabel ? LABELS.indexOf(explicitLabel) : -1
return {
...state,
nextLabelIndex: explicitLabel
? Math.max(state.nextLabelIndex, labelIndex + 1)
: state.nextLabelIndex + 1,
nextColorIndex: state.nextColorIndex + 1,
}
}
// ── Lookups ────────────────────────────────────────────────────────
export function getPoint(state: ConstructionState, id: string): ConstructionPoint | undefined {
return state.elements.find((e): e is ConstructionPoint => e.kind === 'point' && e.id === id)
}
export function getCircle(state: ConstructionState, id: string): ConstructionCircle | undefined {
return state.elements.find((e): e is ConstructionCircle => e.kind === 'circle' && e.id === id)
}
export function getSegment(state: ConstructionState, id: string): ConstructionSegment | undefined {
return state.elements.find((e): e is ConstructionSegment => e.kind === 'segment' && e.id === id)
}
export function getRadius(state: ConstructionState, circleId: string): number {
const circle = getCircle(state, circleId)
if (!circle) return 0
const center = getPoint(state, circle.centerId)
const radiusPt = getPoint(state, circle.radiusPointId)
if (!center || !radiusPt) return 0
return Math.sqrt((center.x - radiusPt.x) ** 2 + (center.y - radiusPt.y) ** 2)
}
export function getAllPoints(state: ConstructionState): ConstructionPoint[] {
return state.elements.filter((e): e is ConstructionPoint => e.kind === 'point')
}
export function getAllCircles(state: ConstructionState): ConstructionCircle[] {
return state.elements.filter((e): e is ConstructionCircle => e.kind === 'circle')
}
export function getAllSegments(state: ConstructionState): ConstructionSegment[] {
return state.elements.filter((e): e is ConstructionSegment => e.kind === 'segment')
}
|