File size: 454 Bytes
04ec17f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | export function linearScale(domain, range, clamp = true) {
const [d0, d1] = domain;
const [r0, r1] = range;
const slope = (r1 - r0) / (d1 - d0);
return (x) => {
const result = r0 + slope * (x - d0);
if (!clamp)
return result;
if (result > Math.max(r0, r1))
return Math.max(r0, r1);
if (result < Math.min(r0, r1))
return Math.min(r0, r1);
return result;
};
}
|