website

source code for the ttmaths.uk website
Log | Files | Refs | README

commit 2f1a6fed8c69d642d799742fa979e8a341a65525
parent 850e939777cc757d2f95ca42fdf1577138fa8eba
Author: fen123456 <fenwthompson@gmail.com>
Date:   Fri, 14 Aug 2026 15:44:55 +0100

Add line formatting

Diffstat:
Msrc/components/questions/DoubleReflectionQuestion.jsx | 4++--
Msrc/scripts/utils.js | 21++++++++++++++++++---
2 files changed, 20 insertions(+), 5 deletions(-)

diff --git a/src/components/questions/DoubleReflectionQuestion.jsx b/src/components/questions/DoubleReflectionQuestion.jsx @@ -1,4 +1,4 @@ -import { getRandomChoice, getRandomInt } from "../../scripts/utils"; +import { formatLine, getRandomChoice, getRandomInt } from "../../scripts/utils"; import Graph from "../graphing/Graph"; import Line from "../graphing/Line"; import Polygon from "../graphing/Polygon"; @@ -57,7 +57,7 @@ export default function DoubleReflectionQuestion({ seed, showAnswer }) { const question = (<> <p> - Reflect in line 1, then line 2. What single transformation describes this? + Reflect in {formatLine(line1)} then {formatLine(line2)}. What single transformation describes this? </p> <Graph viewBox={viewBox} width="300" height="300"> <Polygon points={originalShape} label="P" colour="1" /> diff --git a/src/scripts/utils.js b/src/scripts/utils.js @@ -35,12 +35,27 @@ export function simplifyFraction(num, denom) { return [num / hcf, denom / hcf]; } -export function formatFraction(num, denom, parameters = { showPlus: false, simplify: false }) { +// this thing is probably begging for bugs from simplification and zeroes. +export function formatFraction(num, denom, parameters = { showPlus: false, showOne: true, showZero: false, simplify: false, suffix: '' }) { + if (!parameters.showZero && num == 0) { return ''; } const plus = parameters.showPlus ? '+' : ''; const sign = num / denom < 0 ? '-' : plus; + + [num, denom] = [Math.abs(num), Math.abs(denom)] [num, denom] = parameters.simplify ? [num, denom] : simplifyFraction(num, denom) - if (denom == 1) { return `${sign}${Math.abs(num)}`; } - return `${sign}frac(${num}, ${denom})` + parameters.suffix || (parameters.suffix = '') + + let x = `frac(${num}, ${denom})` + parameters.showOne || num / denom == 1 && (x = ''); + if (denom == 1) { return sign + Math.abs(num) + parameters.suffix; } + return sign + x + parameters.suffix +} + +export function formatLine(line) { + if (line.b == 0) { return `x = ${formatFraction(line.c, line.a, { simplify: true })}` } + const mxTerm = formatFraction(-line.a, line.b, { simplify: true, suffix: 'x' }) + const cTerm = formatFraction(line.c, line.b, { showOne: true, simplify: true }) + return 'y = ' + mxTerm + cTerm } export function getSymmetricBounds(midX, viewBox) {