00001 #ifndef __GRAPH_H__
00002 #define __GRAPH_H__
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 class Graph {
00034 protected:
00035 int W, H;
00036 int cpx, cpy, ccolor, cbkcolor;
00037 int fround( float f ) { return (int)((f<0) ? f-0.5 : f+0.5); }
00038
00039 virtual void initgraph( int WW, int HH );
00040 virtual void closegraph( void ) {}
00041
00042 public:
00043 Graph( void ) { W = H = 2; cpx=cpy=ccolor=cbkcolor=0; }
00044 virtual ~Graph( void ) { closegraph(); }
00045
00046 virtual int getw( void ) { return W; }
00047 virtual int geth( void ) { return H; }
00048
00049 float x2nx( int x ) { return (float)x/(float)(W-1); }
00050 int nx2x( float x ) { return fround(x*(W-1)); }
00051 float x2wx( int x ) { return (float)x/(W-1)*2.0-1; }
00052 int wx2x( float x ) { return fround((x+1)*0.5*(W-1)); }
00053
00054 float y2ny( int y ) { return (float)y/(float)(H-1); }
00055 int ny2y( float y ) { return fround(y*(H-1)); }
00056 float y2wy( int y ) { return -(float)y/(H-1)*2.0+1; }
00057 int wy2y( float y ) { return fround((-y+1)*0.5*(H-1)); }
00058
00059 virtual void clearviewport( int color=-1 ) { clearviewport(0,0,W-1,H-1,color); };
00060 virtual void clearviewport( int x1, int y1, int x2, int y2, int color=-1 ) { bar(x1,y1,x2,y2,(color<0)?cbkcolor:color,color); };
00061 virtual void wclearviewport( float x1, float y1, float x2, float y2, int color=-1 ) { clearviewport(wx2x(x1),wy2y(y1),wx2x(x2),wy2y(y2),color); };
00062 virtual void nclearviewport( float x1, float y1, float x2, float y2, int color=-1 ) { clearviewport(nx2x(x1),ny2y(y1),nx2x(x2),ny2y(y2),color); };
00063
00064 virtual void setcolor( int color ) { ccolor = color; };
00065 virtual void setbkcolor( int bkcolor ) { cbkcolor= bkcolor; };
00066 virtual int getcolor( void ) { return ccolor; };
00067 virtual int getbkcolor( void ) { return cbkcolor; };
00068
00069 virtual int getncolors( void ) { return 0; };
00070 virtual void setcolorrgb( int , int , int , int ) {};
00071 virtual void nsetcolorrgb( int color, double r, double g, double b ) { setcolorrgb(color,int(r*255),int(g*255),int(b*255)); };
00072 virtual void setdefpalette( void );
00073
00074 virtual void putpixel( int , int , int =-1 ) {};
00075 virtual void wputpixel( float x, float y, int color=-1 ) { putpixel(wx2x(x),wy2y(y),color); };
00076 virtual void nutpixel( float x, float y, int color=-1 ) { putpixel(nx2x(x),ny2y(y),color); };
00077
00078 virtual void line( int , int , int , int , int =-1 ) {};
00079 virtual void wline( float x1, float y1, float x2, float y2, int color=-1 ) { line(wx2x(x1),wy2y(y1),wx2x(x2),wy2y(y2),color); };
00080 virtual void nline( float x1, float y1, float x2, float y2, int color=-1 ) { line(nx2x(x1),ny2y(y1),nx2x(x2),ny2y(y2), color); };
00081
00082 virtual void lineto( int x, int y, int color=-1 ) { line(cpx,cpy,x,y,color); cpx=x; cpy=y; };
00083 virtual void wlineto( float x, float y, int color=-1 ) { lineto(wx2x(x),wy2y(y),color); };
00084 virtual void nlineto( float x, float y, int color=-1 ) { lineto(nx2x(x),ny2y(y),color); };
00085
00086 virtual void linerel( int dx, int dy, int color=-1 ) { line(cpx,cpy,cpx+dx,cpy+dy,color); cpx+=dx; cpy+=dy; };
00087 virtual void wlinerel( float dx, float dy, int color=-1 ) { linerel(wx2x(dx),wy2y(dy),color); };
00088 virtual void nlinerel( float dx, float dy, int color=-1 ) { linerel(nx2x(dx),ny2y(dy),color); };
00089
00090 virtual void moveto( int x, int y ) { cpx=x; cpy=y; };
00091 virtual void wmoveto( float x, float y ) { moveto(wx2x(x),wy2y(y)); };
00092 virtual void nmoveto( float x, float y ) { moveto(nx2x(x),ny2y(y)); };
00093
00094 virtual void moverel( int dx, int dy ) { cpx+=dx; cpy+=dy; };
00095 virtual void wmoverel( float dx, float dy ) { wmoverel(wx2x(dx),wy2y(dy)); };
00096 virtual void nmoverel( float dx, float dy ) { wmoverel(nx2x(dx),ny2y(dy)); };
00097
00098 virtual int getx( void ) { return cpx; };
00099 virtual float wgetx( void ) { return x2wx(getx()); };
00100 virtual float ngetx( void ) { return x2nx(getx()); };
00101
00102 virtual int gety( void ) { return cpy; };
00103 virtual float wgety( void ) { return y2wy(gety()); };
00104 virtual float ngety( void ) { return y2ny(gety()); };
00105
00106 virtual void rectangle( int , int , int , int , int =-1 ) {};
00107 virtual void wrectangle( float x1, float y1, float x2, float y2, int color=-1 ) { rectangle(wx2x(x1),wy2y(y1),wx2x(x2),wy2y(y2),color); };
00108 virtual void nrectangle( float x1, float y1, float x2, float y2, int color=-1 ) { rectangle(nx2x(x1),ny2y(y1),nx2x(x2),ny2y(y2),color); };
00109
00110 virtual void bar( int , int , int , int , int =-1, int =-1 ) {};
00111 virtual void wbar( float x1, float y1, float x2, float y2, int color=-1, int bkcolor=-1 ) { bar(wx2x(x1),wy2y(y1),wx2x(x2),wy2y(y2),color,bkcolor); };
00112 virtual void nbar( float x1, float y1, float x2, float y2, int color=-1, int bkcolor=-1 ) { bar(nx2x(x1),ny2y(y1),nx2x(x2),ny2y(y2),color,bkcolor); };
00113
00114 virtual void ellipse( int , int , int , int , int =-1 ) {};
00115 virtual void wellipse( float x, float y, float rx, float ry, int color=-1 ) { ellipse(wx2x(x),wy2y(y),wx2x(rx)-wx2x(0),wy2y(0)-wy2y(ry),color); };
00116 virtual void nellipse( float x, float y, float ry, float rx, int color=-1 ) { ellipse(nx2x(x),ny2y(y),nx2x(rx)-nx2x(0),ny2y(ry)-ny2y(0),color); };
00117
00118 virtual void circle( int x, int y, int r, int color=-1 ) { ellipse(x,y,r,r,color); };
00119 virtual void wcircle( float x, float y, float r, int color=-1 ) { wellipse(x,y,r,r,color); };
00120 virtual void ncircle( float x, float y, float r, int color=-1 ) { nellipse(x,y,r,r,color); };
00121
00122 virtual void bufferon( void ) {};
00123 virtual void bufferflush( void ) {};
00124 virtual void bufferoff( int =0 ) {};
00125
00126 virtual void setfile( const char * ) {};
00127 virtual const char* getfile( void ) { return 0; };
00128 virtual void savefile( const char *=0 ) {};
00129
00130 virtual int kbhit( void ) { return 0; };
00131 virtual int getkey( void ) { return 0; };
00132
00133 friend Graph *newGraph( const char * mode, int W, int H );
00134 friend Graph *newGraph( int W, int H, const char *mode );
00135 friend void deleteGraph( Graph *gr );
00136
00137 };
00138
00139
00140 Graph *newGraph( const char *mode="Auto", int W=0, int H=0 );
00141 inline Graph *newGraph( int W, int H, const char *mode="Auto" ) { return newGraph(mode,W,H); }
00142 void deleteGraph( Graph *gr );
00143
00144
00145
00146 enum colors {
00147 GR_BLACK=0, GR_BLUE, GR_GREEN, GR_CYAN,
00148 GR_RED, GR_MAGENTA, GR_BROWN, GR_LIGHTGRAY,
00149 GR_DARKGRAY, GR_LIGHTBLUE, GR_LIGHTGREEN, GR_LIGHTCYAN,
00150 GR_LIGHTRED, GR_LIGHTMAGENTA, GR_YELLOW, GR_WHITE
00151 };
00152
00153
00154
00155 extern Graph *_graph_gr;
00156
00157 void gr_initgraph( const char *mode="Auto", int W=0, int H=0 );
00158 inline void gr_initgraph( int W, int H, const char *mode="Auto" ) { gr_initgraph(mode,W,H); }
00159 void gr_closegraph( void );
00160
00161 #define gr_getw _graph_gr->getw
00162 #define gr_geth _graph_gr->geth
00163
00164 #define gr_x2nx _graph_gr->x2nx
00165 #define gr_nx2x _graph_gr->nx2x
00166 #define gr_x2wx _graph_gr->x2wx
00167 #define gr_wx2x _graph_gr->wx2x
00168
00169 #define gr_y2ny _graph_gr->y2ny
00170 #define gr_ny2y _graph_gr->ny2y
00171 #define gr_y2wy _graph_gr->y2wy
00172 #define gr_wy2y _graph_gr->wy2y
00173
00174 #define gr_clearviewport _graph_gr->clearviewport
00175 #define gr_wclearviewport _graph_gr->wclearviewport
00176 #define gr_nclearviewport _graph_gr->nclearviewport
00177
00178 #define gr_setcolor _graph_gr->setcolor
00179 #define gr_setbkcolor _graph_gr->setbkcolor
00180 #define gr_getcolor _graph_gr->getcolor
00181 #define gr_getbkcolor _graph_gr->getbkcolor
00182
00183 #define gr_getncolors _graph_gr->getncolors
00184 #define gr_setcolorrgb _graph_gr->setcolorrgb
00185 #define gr_nsetcolorrgb _graph_gr->nsetcolorrgb
00186 #define gr_setdefpalette _graph_gr->setdefpalette
00187
00188 #define gr_putpixel _graph_gr->putpixel
00189 #define gr_wputpixel _graph_gr->wputpixel
00190 #define gr_nputpixel _graph_gr->nputpixel
00191
00192 #define gr_line _graph_gr->line
00193 #define gr_wline _graph_gr->wline
00194 #define gr_nline _graph_gr->nline
00195
00196 #define gr_lineto _graph_gr->lineto
00197 #define gr_wlineto _graph_gr->wlineto
00198 #define gr_nlineto _graph_gr->nlineto
00199
00200 #define gr_linerel _graph_gr->linerel
00201 #define gr_wlinerel _graph_gr->wlinerel
00202 #define gr_nlinerel _graph_gr->nlinerel
00203
00204 #define gr_moveto _graph_gr->moveto
00205 #define gr_wmoveto _graph_gr->wmoveto
00206 #define gr_nmoveto _graph_gr->nmoveto
00207
00208 #define gr_moverel _graph_gr->moverel
00209 #define gr_wmoverel _graph_gr->wmoverel
00210 #define gr_nmoverel _graph_gr->nmoverel
00211
00212 #define gr_getx _graph_gr->getx
00213 #define gr_wgetx _graph_gr->wgetx
00214 #define gr_ngetx _graph_gr->ngetx
00215
00216 #define gr_gety _graph_gr->gety
00217 #define gr_wgety _graph_gr->wgety
00218 #define gr_ngety _graph_gr->ngety
00219
00220 #define gr_rectangle _graph_gr->rectangle
00221 #define gr_wrectangle _graph_gr->wrectangle
00222 #define gr_nrectangle _graph_gr->nrectangle
00223
00224 #define gr_bar _graph_gr->bar
00225 #define gr_wbar _graph_gr->wbar
00226 #define gr_nbar _graph_gr->nbar
00227
00228 #define gr_ellipse _graph_gr->ellipse
00229 #define gr_wellipse _graph_gr->wellipse
00230 #define gr_nellipse _graph_gr->nellipse
00231
00232 #define gr_circle _graph_gr->circle
00233 #define gr_wcircle _graph_gr->wcircle
00234 #define gr_ncircle _graph_gr->ncircle
00235
00236 #define gr_bufferon _graph_gr->bufferon
00237 #define gr_bufferflush _graph_gr->bufferflush
00238 #define gr_bufferoff _graph_gr->bufferoff
00239
00240 #define gr_setfile _graph_gr->setfile
00241 #define gr_getfile _graph_gr->getfile
00242 #define gr_savefile _graph_gr->savefile
00243
00244 #define gr_kbhit _graph_gr->kbhit
00245 #define gr_getkey _graph_gr->getkey
00246
00247
00248
00249 #endif