Line.jsx (839B)
1 import { useContext } from 'preact/hooks'; 2 import { PlotContext } from '../../scripts/contexts.js'; 3 import { getColourClass } from "../../scripts/graphUtils" 4 5 export default function Line({ line, label=null, colour='auto' }) { 6 const p = useContext(PlotContext); 7 8 const xPairs = [p.minX, p.maxX].map((x) => [x, (-line.c - line.a*x)/line.b]) 9 const yPairs = [p.minY, p.maxY].map((y) => [(-line.c - line.b*y)/line.a, y]) 10 11 // important to do this for straight (y = c or x = c) lines. 12 let points = [] 13 xPairs.map((point) => { (point[1] >= p.minY && point[1] <= p.maxY) && points.push(point) }) 14 yPairs.map((point) => { (point[0] > p.minX && point[0] < p.maxX) && points.push(point) }) 15 16 // points will always be two. 17 const [i, f] = points 18 return <line x1={i[0]} y1={i[1]} x2={f[0]} y2={f[1]} class={getColourClass(colour)} /> 19 }