00001 #ifndef XGRAPH_INCLUDE_CC
00002 #define XGRAPH_INCLUDE_CC
00003 #include "xgraph.h"
00004 CXGraph::CXGraph(int horizsize,int vertsize,int ixposition_in,int iyposition_in){
00005 window_horizsize=horizsize;
00006 window_vertsize=vertsize;
00007 ixposition=ixposition_in;
00008 iyposition=iyposition_in;
00009 theDisplay = XOpenDisplay(NULL);
00010 XSynchronize(theDisplay, True);
00011 theScreen = DefaultScreenOfDisplay(theDisplay);
00012 theWindow = XCreateSimpleWindow(theDisplay, RootWindowOfScreen(theScreen),
00013 ixposition,iyposition,
00014 window_horizsize, window_vertsize, 0,
00015 BlackPixelOfScreen(theScreen),
00016 WhitePixelOfScreen(theScreen));
00017 theGC = XCreateGC(theDisplay, theWindow, 0L, NULL);
00018 XSetForeground(theDisplay, theGC, BlackPixelOfScreen(theScreen));
00019 XMapWindow(theDisplay,theWindow);
00020 XMoveWindow(theDisplay,theWindow,ixposition+50,iyposition+50);
00021 XSetLineAttributes(theDisplay,theGC,2,
00022 LineSolid,CapRound,JoinRound);
00023 screen_colormap = DefaultColormap(theDisplay, DefaultScreen(theDisplay));
00024
00025 rc = XAllocNamedColor(theDisplay, screen_colormap, "red", &red, &red);
00026 if (rc == 0) {
00027 fprintf(stderr, "XAllocNamedColor - failed to allocated 'red' color.\n");
00028 exit(1);
00029 }
00030 rc = XAllocNamedColor(theDisplay, screen_colormap, "brown", &brown, &brown);
00031 if (rc == 0) {
00032 fprintf(stderr, "XAllocNamedColor - failed to allocated 'brown' color.\n");
00033 exit(1);
00034 }
00035 rc = XAllocNamedColor(theDisplay, screen_colormap, "blue", &blue, &blue);
00036 if (rc == 0) {
00037 fprintf(stderr, "XAllocNamedColor - failed to allocated 'blue' color.\n");
00038 exit(1);
00039 }
00040 rc = XAllocNamedColor(theDisplay, screen_colormap, "yellow", &yellow, &yellow);
00041 if (rc == 0) {
00042 fprintf(stderr, "XAllocNamedColor - failed to allocated 'yellow' color.\n");
00043 exit(1);
00044 }
00045 rc = XAllocNamedColor(theDisplay, screen_colormap, "green", &green, &green);
00046 if (rc == 0) {
00047 fprintf(stderr, "XAllocNamedColor - failed to allocated 'green' color.\n");
00048 exit(1);
00049 }
00050 rc = XAllocNamedColor(theDisplay, screen_colormap, "orange", &orange, &orange);
00051 if (rc == 0) {
00052 fprintf(stderr, "XAllocNamedColor - failed to allocated 'orange' color.\n");
00053 exit(1);
00054 }
00055 rc = XAllocNamedColor(theDisplay, screen_colormap, "black", &black, &black);
00056 if (rc == 0) {
00057 fprintf(stderr, "XAllocNamedColor - failed to allocated 'black' color.\n");
00058 exit(1);
00059 }
00060 rc = XAllocNamedColor(theDisplay, screen_colormap, "cyan", &cyan, &cyan);
00061 if (rc == 0) {
00062 fprintf(stderr, "XAllocNamedColor - failed to allocated 'cyan' color.\n");
00063 exit(1);
00064 }
00065 rc = XAllocNamedColor(theDisplay, screen_colormap, "violet", &violet, &violet);
00066 if (rc == 0) {
00067 fprintf(stderr, "XAllocNamedColor - failed to allocated 'violet' color.\n");
00068 exit(1);
00069 }
00070 rc = XAllocNamedColor(theDisplay, screen_colormap, "lightblue", &lightblue, &lightblue);
00071 if (rc == 0) {
00072 fprintf(stderr, "XAllocNamedColor - failed to allocated 'lightblue' color.\n");
00073 exit(1);
00074 }
00075 rc = XAllocNamedColor(theDisplay, screen_colormap, "navy", &navy, &navy);
00076 if (rc == 0) {
00077 fprintf(stderr, "XAllocNamedColor - failed to allocated 'navy' color.\n");
00078 exit(1);
00079 }
00080 rc = XAllocNamedColor(theDisplay, screen_colormap, "pink", &pink, &pink);
00081 if (rc == 0) {
00082 fprintf(stderr, "XAllocNamedColor - failed to allocated 'pink' color.\n");
00083 exit(1);
00084 }
00085 rc = XAllocNamedColor(theDisplay, screen_colormap, "white", &white, &white);
00086 if (rc == 0) {
00087 fprintf(stderr, "XAllocNamedColor - failed to allocated 'white' color.\n");
00088 exit(1);
00089 }
00090
00091
00092 }
00093
00094 CXGraph::~CXGraph(){
00095 XFlush(theDisplay);
00096 }
00097 void CXGraph::flush(){
00098 XFlush(theDisplay);
00099 XCloseDisplay(theDisplay);
00100 }
00101
00102 void CXGraph::setcolor(string color){
00103 if(color=="red"){
00104 XSetForeground(theDisplay, theGC, red.pixel);
00105 }
00106 if(color=="brown"){
00107 XSetForeground(theDisplay, theGC, brown.pixel);
00108 }
00109 if(color=="yellow"){
00110 XSetForeground(theDisplay, theGC, yellow.pixel);
00111 }
00112 if(color=="blue"){
00113 XSetForeground(theDisplay, theGC, blue.pixel);
00114 }
00115 if(color=="green"){
00116 XSetForeground(theDisplay, theGC, green.pixel);
00117 }
00118 if(color=="orange"){
00119 XSetForeground(theDisplay, theGC, orange.pixel);
00120 }
00121 if(color=="black"){
00122 XSetForeground(theDisplay, theGC, black.pixel);
00123 }
00124 if(color=="cyan"){
00125 XSetForeground(theDisplay, theGC, cyan.pixel);
00126 }
00127 if(color=="violet"){
00128 XSetForeground(theDisplay, theGC, violet.pixel);
00129 }
00130 if(color=="lightblue"){
00131 XSetForeground(theDisplay, theGC, lightblue.pixel);
00132 }
00133 if(color=="navy"){
00134 XSetForeground(theDisplay, theGC, navy.pixel);
00135 }
00136 if(color=="pink"){
00137 XSetForeground(theDisplay, theGC, pink.pixel);
00138 }
00139 }
00140
00141 void CXGraph::drawtext(char *charstring,double x,double y){
00142 int i,j;
00143 getij(x,y,&i,&j);
00144 i=i-strlen(charstring)*3;
00145 XDrawString(theDisplay,theWindow,theGC,i,j,charstring,strlen(charstring));
00146 }
00147
00148 CAxesInfo::CAxesInfo(){
00149 xintercept=yintercept=0.0;
00150 nxtics=nytics=4;
00151 }
00152
00153 void CXGraph::drawline(double x1,double y1,double x2,double y2){
00154 int i1,j1,i2,j2;
00155 getij(x1,y1,&i1,&j1);
00156 getij(x2,y2,&i2,&j2);
00157 XDrawLine(theDisplay,theWindow,theGC,i1,j1,i2,j2);
00158 }
00159
00160 void CXGraph::drawarrow(double x1,double y1,double x2,double y2,
00161 double headsize){
00162 int i1,j1,i2,j2,ihead,iperp,jperp,ipar,jpar;
00163 double norm;
00164 getij(x1,y1,&i1,&j1);
00165 getij(x2,y2,&i2,&j2);
00166 XDrawLine(theDisplay,theWindow,theGC,i1,j1,i2,j2);
00167 ihead=int(headsize*0.8*window_horizsize);
00168 norm=sqrt(double((i1-i2)*(i1-i2)+(j1-j2)*(j1-j2)));
00169 ipar=int(ihead*(i1-i2)/norm);
00170 jpar=int(ihead*(j1-j2)/norm);
00171 iperp=jpar;
00172 jperp=-ipar;
00173 i1=i2+ipar+iperp;
00174 j1=j2+jpar+jperp;
00175 XDrawLine(theDisplay,theWindow,theGC,i1,j1,i2,j2);
00176 i1=i1-2*iperp;
00177 j1=j1-2*jperp;
00178 XDrawLine(theDisplay,theWindow,theGC,i1,j1,i2,j2);
00179 }
00180
00181 void CXGraph::drawpoint(double x,double y){
00182 int i,j;
00183 getij(x,y,&i,&j);
00184 XDrawPoint(theDisplay,theWindow,theGC,i,j);
00185 }
00186
00187 void CXGraph::drawcircle(double x,double y,double size){
00188 int i,j,ir;
00189 const double PI=4.0*atan(1.0);
00190 getij(x,y,&i,&j);
00191 ir=1+int(2.0*size*0.8*window_horizsize/sqrt(PI));
00192 XDrawArc(theDisplay,theWindow,theGC,i-ir,j-ir,2*ir,2*ir,0,23040);
00193 }
00194
00195 void CXGraph::drawsquare(double x,double y,double size){
00196 int i1,j1,i2,j2,i,j,h;
00197 getij(x,y,&i,&j);
00198 h=int(size*0.8*window_horizsize);
00199 i1=i-h;
00200 j1=j-h;
00201 i2=i1+2*h;
00202 j2=j1;
00203 XDrawLine(theDisplay,theWindow,theGC,i1,j1,i2,j2);
00204 i2=i1;
00205 j2=j1+2*h;
00206 XDrawLine(theDisplay,theWindow,theGC,i1,j1,i2,j2);
00207 i1=i2+2*h;
00208 j1=j2;
00209 XDrawLine(theDisplay,theWindow,theGC,i1,j1,i2,j2);
00210 i2=i1;
00211 j2=j1-2*h;
00212 XDrawLine(theDisplay,theWindow,theGC,i1,j1,i2,j2);
00213 }
00214
00215 void CXGraph::drawdiamond(double x,double y,double size){
00216 int i1,j1,i2,j2,i,j,h;
00217 h=int(size*0.8*window_horizsize*sqrt(2.0));
00218 getij(x,y,&i,&j);
00219 i1=i;
00220 j1=j+h;
00221 i2=i-h;
00222 j2=j;
00223 XDrawLine(theDisplay,theWindow,theGC,i1,j1,i2,j2);
00224 i2=i+h;
00225 j2=j;
00226 XDrawLine(theDisplay,theWindow,theGC,i1,j1,i2,j2);
00227 i1=i;
00228 j1=j-h;
00229 XDrawLine(theDisplay,theWindow,theGC,i1,j1,i2,j2);
00230 i2=i-h;
00231 j2=j;
00232 XDrawLine(theDisplay,theWindow,theGC,i1,j1,i2,j2);
00233 }
00234
00235 void CXGraph::drawuptriangle(double x,double y,double size){
00236 int i1,j1,i2,j2,i,j,h;
00237 h=int(size*0.8*window_horizsize*sqrt(2.0));
00238 getij(x,y,&i,&j);
00239 i1=i;
00240 j1=j+int(h*(sqrt(3.0)-1.0/sqrt(3.0)));
00241 i2=i-h;
00242 j2=j-int(h/sqrt(3.0));
00243 XDrawLine(theDisplay,theWindow,theGC,i1,j1,i2,j2);
00244 i2=i+h;
00245 XDrawLine(theDisplay,theWindow,theGC,i1,j1,i2,j2);
00246 i1=i-h;
00247 j1=j-int(h/sqrt(3.0));
00248 XDrawLine(theDisplay,theWindow,theGC,i1,j1,i2,j2);
00249 }
00250
00251 void CXGraph::drawdowntriangle(double x,double y,double size){
00252 int i1,j1,i2,j2,i,j,h;
00253 getij(x,y,&i,&j);
00254 h=int(size*0.8*window_horizsize*sqrt(2.0));
00255 i1=i;
00256 j1=j-int(h*(sqrt(3.0)-1.0/sqrt(3.0)));
00257 i2=i-h;
00258 j2=j+int(h/sqrt(3.0));
00259 XDrawLine(theDisplay,theWindow,theGC,i1,j1,i2,j2);
00260 i2=i+h;
00261 XDrawLine(theDisplay,theWindow,theGC,i1,j1,i2,j2);
00262 i1=i-h;
00263 j1=j+int(h/sqrt(3.0));
00264 XDrawLine(theDisplay,theWindow,theGC,i1,j1,i2,j2);
00265 }
00266
00267
00268 void CXGraph::setaxes(double xmin,double ymin,double xmax,double ymax){
00269 axesinfo.xmin=xmin;
00270 axesinfo.ymin=ymin;
00271 axesinfo.xmax=xmax;
00272 axesinfo.ymax=ymax;
00273 axesinfo.xscale =xmax-xmin;
00274 axesinfo.yscale=ymax-ymin;
00275 }
00276
00277 void CXGraph::drawaxes(){
00278 int itic;
00279 double x1,x2,y1,y2;
00280 int i1,j1,i2,j2;
00281 int arrowsize,ticsize;
00282 double xtext,ytext;
00283 char charstring[40];
00284 ticsize=int(0.02*0.8*window_horizsize);
00285 arrowsize=ticsize;
00286
00287 x1=axesinfo.xintercept;
00288 y1=axesinfo.ymin;
00289 x2=x1;
00290 y2=axesinfo.ymax+0.06*axesinfo.yscale;
00291 drawarrow(x1,y1,x2,y2,0.02);
00292 for(itic=0;itic<=axesinfo.nytics;itic++){
00293 y1=axesinfo.ymin+itic*axesinfo.yscale/double(axesinfo.nytics);
00294 x1=axesinfo.xintercept;
00295 getij(x1,y1,&i1,&j1);
00296 i1=i1-ticsize;
00297 i2=i1+2*ticsize;
00298 j2=j1;
00299 XDrawLine(theDisplay,theWindow,theGC,i1,j1,i2,j2);
00300 xtext=x1-30*(axesinfo.xscale)/(0.8*window_vertsize);
00301 ytext=y1-4*(axesinfo.yscale)/(0.8*window_vertsize);
00302 sprintf(charstring,"%g",y1);
00303 drawtext(charstring,xtext,ytext);
00304 }
00305
00306 y1=axesinfo.yintercept;
00307 x1=axesinfo.xmin;
00308 y2=y1;
00309 x2=axesinfo.xmax+0.06*axesinfo.xscale;
00310 drawarrow(x1,y1,x2,y2,.02);
00311 for(itic=0;itic<=axesinfo.nxtics;itic++){
00312 x1=axesinfo.xmin+itic*axesinfo.xscale/double(axesinfo.nxtics);
00313 y1=axesinfo.yintercept;
00314 getij(x1,y1,&i1,&j1);
00315 j1=j1-ticsize;
00316 j2=j1+2*ticsize;
00317 i2=i1;
00318 XDrawLine(theDisplay,theWindow,theGC,i1,j1,i2,j2);
00319 xtext=x1;
00320 ytext=y1-20*(axesinfo.yscale)/(0.8*window_vertsize);
00321 sprintf(charstring,"%g",xtext);
00322 drawtext(charstring,xtext,ytext);
00323 }
00324 }
00325
00326 void CXGraph::getij(double x,double y,int *i,int *j){
00327 *i=int(0.15*window_horizsize
00328 +0.8*window_horizsize*(x-axesinfo.xmin)
00329 /(axesinfo.xmax-axesinfo.xmin));
00330 *j=int(0.9*window_vertsize
00331 -0.8*window_vertsize*(y-axesinfo.ymin)
00332 /(axesinfo.ymax-axesinfo.ymin));
00333 }
00334
00335 void CXGraph::closedisplay(){
00336 XCloseDisplay(theDisplay);
00337 }
00338
00339 void CXGraph::plotline(double *x,double *y,int npts){
00340 double xx,yy,oldx,oldy;
00341 double xmin,ymin,xmax,ymax;
00342 int i;
00343 xmin=axesinfo.xmin;
00344 ymin=axesinfo.ymin;
00345 xmax=axesinfo.xmax;
00346 ymax=axesinfo.ymax;
00347
00348 for(i=1;i<npts;i++){
00349 xx=x[i]; yy=y[i];
00350 oldx=x[i-1]; oldy=y[i-1];
00351 if(xx<=xmax && xx>=xmin && yy<=ymax && yy>=ymin
00352 && oldx<=xmax && oldx>=xmin && oldy <=ymax && oldy>=ymin){
00353 drawline(oldx,oldy,xx,yy);
00354 }
00355 }
00356 }
00357
00358 void CXGraph::plotpoints(double *x,double *y,int npts){
00359 double xx,yy,size;
00360 double xmin,ymin,xmax,ymax;
00361 int i;
00362 xmin=axesinfo.xmin;
00363 ymin=axesinfo.ymin;
00364 xmax=axesinfo.xmax;
00365 ymax=axesinfo.ymax;
00366 size=0.005;
00367
00368 for(i=0;i<npts;i++){
00369 xx=x[i]; yy=y[i];
00370 if(xx<=xmax && xx>=xmin && yy<=ymax && yy>=ymin){
00371 drawsquare(xx,yy,size);
00372 }
00373 }
00374 }
00375
00376 void CXGraph::clear(){
00377 XSetForeground(theDisplay,theGC,white.pixel);
00378 XFillRectangle(theDisplay,theWindow,theGC,0,0,window_horizsize,window_vertsize);
00379 XSetForeground(theDisplay,theGC,black.pixel);
00380 }
00381 #endif