var interval = 10; // DO NOT CHANGE THIS VALUE // this is the equation. You can change this // to any equation, but just be sure to express // the equation in terms of y. I _think_ this is // slope intercept form, isn't it? // Be aware, though, that javascript cannot handle y = -2x + 1 // you need to write it with * for multiply, / for divide // so it would be var y = -2 * x + 1 var eq = function(x) { //var y = x*x+1; // funky sample equation //var y = x*x*x/10+1; // funky sample equation var y = -2*x + 1; // CHANGE ME HERE return y; }; // function to draw points of an equation. var equation = function() { // this loop will draw several dots // according to the equation above for(var x = -200; x <= 200; x++) { var y = eq(x); // use the function to calculate what y is stroke(255, 0, 0); // set colour of dots fill(255, 0, 0); // set colour of fill ellipse( 200+x*interval, 200-(y)*interval, 5, 5 ); // draw dot } }; // draw the origin var drawOrigin = function() { line(0,200,400,200); // draw x axis line(200,0,200,400); // draw y axis // loop to draw markers along x axis for(var x = 0; x < 400; x+= interval) { line(x,(200+interval/2),x,(200-interval/2)); } // loop to draw markers along y axis for(var y = 0; y < 400; y+= interval) { line(200-interval/2,y,200+interval/2,y); } }; // fancy schmancy line matching the equation above. // will not work correctly for anything other than // line in slope intercept form. var equationLine = function() { var x = -200; var x2 = 200; var y = eq(x); var y2 = eq(x2); stroke(0, 255, 0); // set line colour line(200+x*interval,200-(y)*interval,200+x2*interval,200-(y2)*interval); }; // draw everything in a nice graph format drawOrigin(); equation(); equationLine();