1 --- 2 > id: parabola 3 4 x-coordinate-system(x-axis="-5,5,1" y-axis="-3,5,1") 5 6 {.text-center} `y =`${a}{a|1|-5,5,0.1} `x^2+`${b}{b|0|-5,5,0.1} `x+`${c}{c|0|-5,5,0.1}
import {CoordinateSystem, Step} from '../shared/types'; function q(a: number, b: number, c: number) { return (x: number) => (a * x * x + b * x + c); } function zeros(a: number, b: number, c: number) { const disc = b * b - 4 * a * c; if (disc < 0) return []; if (nearlyEquals(disc, 0, 0.1)) return [-b / (2 * a)]; const x1 = (-b + Math.sqrt(disc)) / (2 * a); const x2 = (-b - Math.sqrt(disc)) / (2 * a); return [x1, x2]; } export function parabola($step: Step) { const $chart = $step.$('x-coordinate-system') as CoordinateSystem; $step.model.watch((s: any) => { const fn = q(s.a, s.b, s.c); $chart.setFunctions(fn); $chart.drawPoints(zeros(s.a, s.b, s.c).map(p => new Point(p, fn(p)))); }); }