00001 #include "vplot.h"
00002 #include "graph.h"
00003 #include "uti.h"
00004
00005
00006
00007
00008
00009 #define SUPPORT_SMOOTHCLEAR
00010
00011 template <class T>
00012 void gr_xplot1D( int x1, int y1, int x2, int y2,
00013 T vec[], int n, KVStrList& opts )
00014
00015
00016 {
00017 T min, max;
00018 int i,clip;
00019 #ifdef SUPPORT_SMOOTHCLEAR
00020 int smooth;
00021 int ox=0;
00022 #endif
00023
00024 gr_bufferon();
00025
00026 if (opts.contains("max")) max = (T)opts.dval("max");
00027 else for (i=1, max=vec[0]; i<n; i++) if (max<vec[i]) max=vec[i];
00028 if (opts.contains("min")) min = (T)opts.dval("min");
00029 else for (i=1, min=vec[0]; i<n; i++) if (min>vec[i]) min=vec[i];
00030
00031 if (opts.contains("bg")) gr_setbkcolor(opts.ival("bg"));
00032 if (opts.contains("fg")) gr_setcolor(opts.ival("fg"));
00033 #ifdef SUPPORT_SMOOTHCLEAR
00034 smooth = opts.bbval("smoothclear");
00035 #endif
00036 if (opts.bbval("clear")) {
00037 #ifdef SUPPORT_SMOOTHCLEAR
00038 if (!smooth) gr_bar(x1,y1,x2,y2,gr_getbkcolor(),gr_getbkcolor());
00039 #else
00040 gr_bar(x1,y1,x2,y2,gr_getbkcolor(),gr_getbkcolor());
00041 #endif
00042 }
00043 #ifdef SUPPORT_SMOOTHCLEAR
00044 else smooth=0;
00045 #endif
00046 if (opts.bbval("clip")) clip=1; else clip=0;
00047 if (opts.bbval("frame"))
00048 gr_rectangle(x1-1,y1-1,x2+1,y2+1);
00049 if (min==max) return;
00050
00051 for (i=0; i<n; i++) {
00052 int x = (int)fround(linterpol(i,0,n-1,x1,x2));
00053 int y = (int)fround(linterpol(vec[i],min,max,y2,y1));
00054 if (clip) {
00055 if (y>y2) y=y2;
00056 if (y<y1) y=y1;
00057 }
00058 if (!i) {
00059 #ifdef SUPPORT_SMOOTHCLEAR
00060 if (smooth) {
00061 gr_line(x,y1,x,y2,gr_getbkcolor());
00062 ox=x;
00063 };
00064 #endif
00065 gr_moveto(x,y);
00066 }
00067 else {
00068 #ifdef SUPPORT_SMOOTHCLEAR
00069 if (smooth && (x!=ox)) {
00070 gr_line(x,y1,x,y2,gr_getbkcolor());
00071 ox=x;
00072 };
00073 #endif
00074 gr_lineto(x,y);
00075 }
00076 }
00077
00078 gr_bufferoff();
00079 }
00080
00081
00082
00083 void gr_plot1D( int x1, int y1, int x2, int y2,
00084 double vec[], int n, const KVStrList & opts )
00085 {
00086 gr_xplot1D(x1,y1,x2,y2,vec,n,(KVStrList&)opts);
00087 }
00088
00089 void gr_plot1D( int x1, int y1, int x2, int y2,
00090 int vec[], int n, const KVStrList & opts )
00091 {
00092 gr_xplot1D(x1,y1,x2,y2,vec,n,(KVStrList&)opts);
00093 }
00094
00095 void gr_plot1D( int x1, int y1, int x2, int y2,
00096 short vec[], int n, const KVStrList & opts )
00097 {
00098 gr_xplot1D(x1,y1,x2,y2,vec,n,(KVStrList&)opts);
00099 }
00100
00101 void gr_plot1D( int x1, int y1, int x2, int y2,
00102 float vec[], int n, const KVStrList & opts )
00103 {
00104 gr_xplot1D(x1,y1,x2,y2,vec,n,(KVStrList&)opts);
00105 }
00106
00107 void gr_plot1D( int x1, int y1, int x2, int y2,
00108 long vec[], int n, const KVStrList & opts )
00109 {
00110 gr_xplot1D(x1,y1,x2,y2,vec,n,(KVStrList&)opts);
00111 }
00112
00113
00114
00115 void gr_xplot1D( int x1, int y1, int x2, int y2,
00116 double func(double), double xi, double xf, int n, const KVStrList & opts )
00117 {
00118 #define F(i) func(linterpol(i,0,n-1,xi,xf))
00119 double min, max;
00120 int i, clip;
00121 #ifdef SUPPORT_SMOOTHCLEAR
00122 int smooth;
00123 int ox=0;
00124 #endif
00125
00126 gr_bufferon();
00127
00128 if (!n) n=(x2-x1+1);
00129
00130 if (opts.contains("max")) max = opts.dval("max");
00131 else for (i=1, max=F(0); i<n; i++) if (max<F(i)) max=F(i);
00132 if (opts.contains("min")) min = opts.dval("min");
00133 else for (i=1, min=F(0); i<n; i++) if (min>F(i)) min=F(i);
00134
00135 if (opts.contains("bg")) gr_setbkcolor(opts.ival("bg"));
00136 if (opts.contains("fg")) gr_setcolor(opts.ival("fg"));
00137 #ifdef SUPPORT_SMOOTHCLEAR
00138 smooth = opts.bbval("smoothclear");
00139 #endif
00140 if (opts.bbval("clear")) {
00141 #ifdef SUPPORT_SMOOTHCLEAR
00142 if (!smooth) gr_bar(x1,y1,x2,y2,gr_getbkcolor(),gr_getbkcolor());
00143 #else
00144 gr_bar(x1,y1,x2,y2,gr_getbkcolor(),gr_getbkcolor());
00145 #endif
00146 }
00147 #ifdef SUPPORT_SMOOTHCLEAR
00148 else smooth=0;
00149 #endif
00150 if (opts.bbval("clip")) clip=1; else clip=0;
00151 if (opts.bbval("frame")) gr_rectangle(x1,y1,x2,y2);
00152 if (min==max) return;
00153
00154 for (i=0; i<n; i++) {
00155 int x = (int)fround(linterpol(i,0,n-1,x1,x2));
00156 int y = (int)fround(linterpol(F(i),min,max,y2,y1));
00157 if (clip) {
00158 if (y>y2) y=y2;
00159 if (y<y1) y=y1;
00160 }
00161
00162 if (!i) {
00163 #ifdef SUPPORT_SMOOTHCLEAR
00164 if (smooth) {
00165 gr_line(x,y1,x,y2,gr_getbkcolor());
00166 ox=x;
00167 };
00168 #endif
00169 gr_moveto(x,y);
00170 }
00171 else {
00172 #ifdef SUPPORT_SMOOTHCLEAR
00173 if (smooth && (x!=ox)) {
00174 gr_line(x,y1,x,y2,gr_getbkcolor());
00175 ox=x;
00176 };
00177 #endif
00178 gr_lineto(x,y);
00179 }
00180 }
00181
00182 gr_bufferoff();
00183 }
00184