Simple Graph Code

by buechley

Simple Graphing Calculator Code

import processing.pdf.*;

void setup() {
//set the size of the window (& the pdf document)
size(640, 500);
smooth();
noLoop();
stroke(100,100,50); //use a yellow-green color
}

void draw() {
float Y, previousY;
int x;
Y=0;
previousY=Y;
background(255);
for (x=0;x<width;x++)
{
previousY=Y;
//play with this equation
Y=(sin(radians(x))*x/5)+200;
line(x-1, height-previousY, x, height - Y);
}
//close the box
line(x, height-previousY, x, height);
line(x, height, 0, height);
}

void keyPressed() {
//if the 'q' key is pressed, generate a PDF & quit
if (key == 'q') {
String fileName= "graph"+millis()+".pdf"; //create a unique name for your file
beginRecord(PDF, fileName);
draw();
endRecord();
exit();
}
else
{
redraw();
}
}