xxxxxxxxxx
52
let spacing = 20;
let x = 4;
let y = 3;
function setup() {
createCanvas(400, 200);
pixelDensity(2);
}
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(2);
beginShape();
for (let x = -w; x <= w; x++) {
let y = f(x / spacing);
vertex(x, y * spacing);
}
endShape();
strokeWeight(8);
stroke(0);
point(x * spacing, y * spacing);
strokeWeight(8);
stroke(0);
point(x * spacing, f(x) * spacing);
}
function f(x) {
return 0.5 * x - 1;
}