//************************************************************************************************ // the Plot class draws a plot on the screen //************************************************************************************************ class Plot { int x; // x position of upper left corner of plot int y; // y position of upper left corner of plot int w; // width of plot in pixels int h; // height of plot in pixels float vMax; // maximum value displayed on plot scale float vMin; // minimum value displayed on plot scale Trace trace; // displays trace of recorded data color lineColor; // color of lines around rectangle color plotColor; // background color of plot rectangle color traceColor; // color of plotting trace Plot(int _x, int _y, int _w, int _h, float _vMin, float _vMax, int _NGraphPoint, color _lineColor, color _plotColor, color _traceColor) { x = _x; y = _y; w = _w; h = _h; vMax = _vMax; vMin = _vMin; trace = new Trace(_traceColor, _NGraphPoint); lineColor = _lineColor; plotColor = _plotColor; } void drawPlot() { stroke(lineColor); // set pen color (White) fill(plotColor); rect(x,y,w,h); //draw grid points and scale textFont(fontA, 14); // size font to fit alongside plot textAlign(RIGHT, CENTER); // right align text int horzGrid = 9; // number of horizontal grid points int vertGrid = 11; // number of vertical grid points for (int i = 1; i < vertGrid-1; i++) { float yA = lerp(y+h, y, i/(vertGrid-1.0)); // Interpolate for grid points float vA = lerp(vMin, vMax, i/(vertGrid-1.0)); // Display labels at grid points for (int j = 1; j < horzGrid-1; j++) { float xA = lerp(x, x+w, j/(horzGrid-1.0)); // Interpolate for grid points stroke(#FFFFFF); // grid points are white point(xA, yA); // draw grid points on screen } fill(lineColor); // scale text is line color text(str(int(vA)),x-5,yA); // put scale on left side of graph } // draw trace on graph trace.drawTrace(x, y, w, h, vMin, vMax); } } //************************************************************************************************ // the Trace class stores the datapoints for a trace on a plot //************************************************************************************************ class Trace { color traceColor; // color of trace on plot int nGraphPoint; // number of points in trace float[] tracePoints; // holds datapoints for trace float vMin; // minimum value displayed on plot float vMax; // maximum value displayed on plot Trace(color _traceColor, int _nGraphPoint) { nGraphPoint = _nGraphPoint; tracePoints = new float[nGraphPoint+1]; traceColor = _traceColor; for (int i = 0; i < nGraphPoint+1; i++) { tracePoints[i] = 0; } } void drawTrace(int x, int y, int w, int h, float vMin, float vMax) { stroke(traceColor); strokeWeight(2); for (int i = 0; i < nGraphPoint; i++) { // cycle through number of points to plot int x1 = x + i*w/nGraphPoint; int x2 = x + (i+1)*w/nGraphPoint; int y1 = y + h - int(tracePoints[i]*h/(vMax - vMin)); int y2 = y + h - int(tracePoints[i+1]*h/(vMax - vMin)); line(x1, y1, x2, y2); } strokeWeight(1); } void updateTrace(float newData) { for (int i = 0; i < nGraphPoint; i++) { tracePoints[i] = tracePoints[i + 1]; //Increment Graph value array tracePoints[plot.trace.nGraphPoint] = newData; //Set graph value array pointer to graph value } } }