The code so far:
class Plot {
float lowX,hiX,lowY,hiY;
float startLowX,startHiX,startLowY,startHiY;
int border;
float xTickInterval, yTickInterval;
String xLabel,yLabel,title;
float[] xdata;
float[] ydata;
Plot(float templowX, float temphiX, float templowY, float temphiY, String xLabel, String yLabel, String title) {
this(templowX,temphiX,templowY,temphiY);
this.xLabel=xLabel;
this.yLabel=yLabel;
this.title=title;
}
Plot(float templowX, float temphiX, float templowY, float temphiY) {
lowX=templowX;
hiX=temphiX;
lowY=templowY;
hiY=temphiY;
border=50;
xTickInterval=(hiX-lowX)/10;
yTickInterval=(hiY-lowY)/10;
startLowX=lowX;
startHiX=hiX;
startLowY=lowY;
startHiY=hiY;
xLabel="X";
yLabel="Y";
title="";
}
//accessor methods to find out what the range of x and y values are
float getXLow() {
return lowX;
}
float getXHi() {
return hiX;
}
float getYLow() {
return lowY;
}
float getYHi() {
return hiY;
}
void addData(float[] x, float[] y){
xdata=x;
ydata=y;
}
void drawFunc() {
for(int i=0;i<xdata.length;i++){
float x=remapX(xdata[i]);
float y=remapY(ydata[i]);
ellipse(x,y,5,5);
}
}
//draws the axes, the function, the lines connecting mouse to axes, and the labels on the screen
void draw() {
//drawFunc();
//draw axes
drawAxes();
//draw function
drawFunc();
//clear off edges
fill(255);
noStroke();
rect(0,0,width,border);
rect(0,0,border,height);
rect(0,height-border+1,width,border);
rect(width-border+1,0,border,height);
//draw border labels
fill(0);
stroke(0);
textSize(18);
//textAlign(CENTER);
text(xLabel,width/2-textWidth(xLabel)/2,height-border/2);
//draw border values
//textAlign(LEFT);
text(lowX,border,height-border/2);
textAlign(RIGHT);
text(hiX,width-border,height-border/2);
//textAlign(CENTER);
pushMatrix();
translate(border/2,height/2);
rotate(radians(-90));
textAlign(LEFT);
text(yLabel,-textWidth(yLabel)/2,0);
popMatrix();
pushMatrix();
translate(border/2,border);
rotate(radians(-90));
textAlign(RIGHT);
text(hiY,0,0);
popMatrix();
pushMatrix();
translate(border/2,height-border);
rotate(radians(-90));
textAlign(LEFT);
text(lowY,0,0);
popMatrix();
int titleSize=32;
textSize(titleSize);
//textAlign(CENTER);
text(title,width/2-textWidth(title)/2,border/2+titleSize/2);
}
//draws the axes on the screen
void drawAxes() {
textSize(10);
//background color of graph
fill(220);
stroke(220);
rect(0,0,width,height);
stroke(0);
noFill();
float axisSize=1.0;
strokeWeight(axisSize);
//outline
rect(border,border,width-2*border,height-2*border);
strokeWeight(2);
//x axis
float screenX1=remapX(lowX);
float screenX2=remapX(hiX);
float screenY=remapY(0);
if (screenY>=border && screenY<=height-border-2*axisSize) {
line(screenX1,screenY,screenX2,screenY);
}
//add ticks for x axis
fill(0);
textAlign(CENTER);
for(float x=0;x<=hiX;x+=xTickInterval) {
float tickX=remapX(x);
float tickY=(screenY>=border&&screenY<=(height-border))?remapY(0):height-border-2*axisSize;
line(tickX,tickY-2*axisSize,tickX,tickY+2*axisSize);
if (x!=0) {
fill(0,0,0);
text(x,tickX,tickY-2*axisSize-2);
}
}
for(float x=0;x>=lowX;x-=xTickInterval) {
float tickX=remapX(x);
float tickY=(screenY>=border&&screenY<=(height-border))?remapY(0):height-border-2*axisSize;
line(tickX,tickY-2*axisSize,tickX,tickY+2*axisSize);
if (x!=0) text(x,tickX,tickY-2*axisSize-2);
}
//y axis
float screenX=remapX(0);
float screenY1=remapY(lowY);
float screenY2=remapY(hiY);
if (screenX>=border && screenX<=width-border) {
line(screenX,screenY1,screenX,screenY2);
}
//add ticks for y axis
textAlign(LEFT);
for(float y=0;y<=hiY;y+=yTickInterval) {
float tickY=remapY(y);
float tickX=(screenX>=border&&screenX<=(width-border))?remapX(0):border+2*axisSize;
line(tickX-2*axisSize,tickY,tickX+2*axisSize,tickY);
if (y!=0) text(y,tickX+2*axisSize,tickY);
}
for(float y=0;y>=lowY;y-=yTickInterval) {
float tickY=remapY(y);
float tickX=(screenX>=border&&screenX<=(width-border))?remapX(0):border+2*axisSize;
line(tickX-2*axisSize,tickY,tickX+2*axisSize,tickY);
if (y!=0) text(y,tickX+2*axisSize,tickY);
}
}
//change from our coordinate system to screen coordinate system
float remapX(float x) {
return map(x,lowX,hiX,border,width-border);
}
float remapY(float y) {
return map(y,lowY,hiY,height-border,border);
}
//change from screen coordinate system to our coordinate system
float unMapX(float x) {
return map(x,border,width-border,lowX,hiX);
}
float unMapY(float y) {
return map(y,height-border,border,lowY,hiY);
}
}