xxxxxxxxxx
36
let spacing = 20;
function setup() {
createCanvas(400, 200);
}
function draw() {
background(255);
let w = width / 2;
let h = height / 2;
noLoop();
translate(w, h);
scale(1,-1);
stroke(0);
line(-w, 0, w, 0);
line(0, -h, 0, h);
for (let i = -w; i <= w; i += spacing) {
line(i, -5, i, 5);
}
for (let j = -h; j <= h; j += spacing) {
line(-5, j, 5, j);
}
// Draw the line y = 2x - 1
stroke(0);
strokeWeight(4);
beginShape();
for (let x = -w; x <= w; x++) {
let y = 0.5 * (x / spacing) - 1;
vertex(x, y * spacing);
}
endShape();
}