1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
// Jason Rohrer
// Plot.cpp
/**
*
* Scrolling Plot Gui element implementation
*
*
* Created 11-7-99
* Mods:
* Jason Rohrer 11-8-99 Changed to use GraphicBuffer object as screen buffer
*
*/
#include "Plot.h"
Plot::Plot( int x, int y, int w, int h, Color bordC, Color bC, Color lnC ) {
startX = x;
startY = y;
wide = w;
high = h;
innerWide = wide - 2*borderWide;
innerHigh = high - 2*borderWide;
borderC = bordC;
bgC = bC;
lineC = lnC;
imageMap = new unsigned long[high * wide];
mapYOffset = new int[high];
// precalc y offsets into 2d image map
for( int y=0; y<high; y++ ) {
mapYOffset[y] = y*wide;
}
// prepare image map
for( int y=0; y<high; y++ ) {
int yContrib = mapYOffset[y];
for( int x=0; x<wide; x++ ) {
if( y<borderWide || x<borderWide || y>high-borderWide-1 || x>wide-borderWide-1 ) {
imageMap[ yContrib + x ] = borderC.composite; // border
}
else {
imageMap[ yContrib + x ] = bgC.composite; // background
}
}
}
plotVals = new float[innerWide];
for( int i=0; i<innerWide; i++ ) {
plotVals[i] = 0;
}
}
Plot::~Plot() {
delete [] imageMap;
delete [] mapYOffset;
delete [] plotVals;
}
void Plot::addPoint( float p ) {
// scroll plot vals
//memmove( (void *)plotVals, (void *)(&(plotVals[1])), sizeof(float) * innerWide );
for( int i=0; i<innerWide-1; i++ ) {
plotVals[i] = plotVals[i+1];
}
// add new
plotVals[innerWide-1] = p;
float largest = 0;
// find largest
for( int i=0; i<innerWide; i++ ) {
if( largest < plotVals[i] ) {
largest = plotVals[i];
}
}
float invLargest = 1;
if( largest > 0) {
invLargest = 1/largest;
}
// fill plot with bg color
for( int y=borderWide; y<high-borderWide; y++ ) {
int yContrib = mapYOffset[y];
for( int x=borderWide; x<wide-borderWide; x++ ) {
imageMap[ yContrib + x ] = bgC.composite; // background
}
}
// now plot line
for( int x=borderWide; x<wide-borderWide; x++ ) {
int y = (int)(plotVals[x-borderWide] * invLargest * innerHigh);
y = innerHigh - y;
if( y > innerHigh + borderWide ) y = innerHigh + borderWide -1;
if( y < borderWide ) y = borderWide;
imageMap[ mapYOffset[y] + x ] = lineC.composite; // line
}
/* for( int x=0; x<innerWide; x++) {
int y = innerHigh - (int)(plotVals[x] * innerHigh);
imageMap[ mapYOffset[y] + x ] = lineC.composite;
}
*/
}
|