function drawCosineOneDim(n, context) {
const { width, height } = context.canvas;
const xStart = 70;
const xEnd = width - 70;
const yHeight = 120;
const xAxis = [[xStart, yHeight], [xEnd, yHeight]];
const yAxis = [[xStart, 70], [xStart, height - 70]];
const scale = 100;
const f = x => Math.cos(n * Math.PI * x);
context.lineWidth = 4;
context.beginPath();
context.moveTo(xStart, yHeight - scale * f(0));
for (let i = 0; i <= 1; i += 0.001) {
context.lineTo((1 - i) * xStart + i * xEnd, yHeight - scale * f(i));
}
context.strokeStyle = 'rgb(50, 50, 50)';
context.stroke();
context.fillStyle = 'rgb(255, 0, 0)';
context.strokeStyle = 'rgb(255, 0, 0)';
const cosSamples = [];
for (let i = 0; i <= 1; i += 1 / 7) {
cosSamples.push(f(i));
}
context.lineWidth = 1;
const l = xEnd - xStart;
const [boxW, boxH] = [l / 7 - 4, 20];
for (let i = 0; i < cosSamples.length; ++i) {
context.fillStyle = cosValueToRgb(cosSamples[i]);
context.fillRect(20 + i * boxW, height - 30, boxW, boxH);
context.strokeStyle = 'rgb(255,0,0)';
context.strokeRect(20 + i * boxW, height - 30, boxW, boxH);
}
}