/* James Williams Maxflow network graphical simulator functions. April 1993 */ /* Graphically displays a network to the screen. */ /* DOS and Borland C++ specific function calls */ #include #include #include #include #include #include "net.h" #include "getnet.c" #include "displayl.h" #define DELAY 100 /* millisecond delay in animation */ /* graph window coordinates */ int gx1 = 0; int gy1 = 0; int gx2 = 479; int gy2 = 479; /* stats window coordinates */ int sx1 = 480; int sy1 = 0; int sx2 = 639; int sy2 = 479; Graph_type loc; void init_graphics( void ) { /* request autodetection */ int gdriver = DETECT, gmode, errorcode; /* initialize graphics and local variables */ initgraph(&gdriver, &gmode, ""); /* read result of initialization */ errorcode = graphresult(); if (errorcode != grOk) /* an error occurred */ { printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); /* terminate with an error code */ } setbkcolor( WHITE ); } /* init_graphics */ void terminate_graphics( void ) { /* restore default values for everything */ graphdefaults(); /* clear the screen */ cleardevice(); /* clean up */ closegraph(); } /* terminate_graphics */ static float Max( float a, float b ) { if ( a > b ) return a; else return b; } /* Max */ int worldToScreen( float num, float wmin, int smin, float wdiff, int sdiff ) { return (int) ((((num - wmin)/wdiff)*sdiff) + smin); } /* worldToScreen */ void connectNodes( Graph_type loc, int A, int B ) { setcolor( GREEN ); line( loc.node[A].x, loc.node[A].y, loc.node[B].x, loc.node[B].y ); } /* connectNodes */ void display_graph( Network_type * net ) { int i, j; /* Graph_type loc; made GLOBAL */ float maxlong = 0, maxlat = 0, minlong = 999, minlat = 999; float diff; setcolor( GREEN ); rectangle( gx1, gy1, gx2, gy2); loc.netsize = net->netsize; /* find max. and min. lat. and longitude */ for ( i = 1; i <= loc.netsize; i++ ) { if ( net->node_list[i].latitude < minlat ) minlat = net->node_list[i].latitude; if ( net->node_list[i].latitude > maxlat ) maxlat = net->node_list[i].latitude; if ( net->node_list[i].longitude < minlong ) minlong = net->node_list[i].longitude; if ( net->node_list[i].longitude > maxlong ) maxlong = net->node_list[i].longitude; } /* for i */ diff = Max( maxlong - minlong, maxlat - minlat ); /* calculate screen coordinates and plot nodes */ for ( i = 1; i <= loc.netsize; i++ ) { loc.node[i].x = worldToScreen( net->node_list[i].longitude, minlong, gx1+20, diff, gx2 - gx1 - 40 ); loc.node[i].y = worldToScreen( net->node_list[i].latitude, minlat, gy1+20, diff, gy2 - gy1 - 40 ); } /* for i */ /* plot paths */ for ( i = 1; i <= loc.netsize; i++ ) { for ( j = 1; j <= net->node_list[i].link_count; j++ ) { if ( net->node_list[i].link_list[j].spare > 0 ) connectNodes( loc, i, net->node_list[i].link_list[j].dest_node ); } /* for j */ } /* for i */ for ( i = 1; i <= loc.netsize; i++ ) { setcolor( BLUE ); circle( loc.node[i].x, loc.node[i].y, 12 ); setcolor( RED ); outtextxy( loc.node[i].x - 10, loc.node[i].y - 3, net->node_list[i].name ); } /* for i */ } /* display_graph */ void display_stats( Network_type * net ) { int i, j; int x, y; char buffer[81]; settextstyle( SMALL_FONT, HORIZ_DIR, 4 ); setcolor( BLUE ); rectangle( sx1, sy1, sx2, sy2); line( 559, sy1, 559, sy2 ); outtextxy( sx1 + 10, sy1 + 5, net->netname ); sprintf(buffer,"Maxflow Path Finding Simulator by Jim Williams, 4/93"); outtextxy( 25, 0, buffer ); y = sy1 + 30; x = sx1 + 5; for ( i = 1; i <= net->netsize; i++ ) { outtextxy( x, y, net->node_list[i].name ); x += 7; for ( j = 1; j <= net->node_list[i].link_count; j++ ) { y += 10; if ( y>(sy2-10) ){ y = sy1 + 30; x += 80; } sprintf( buffer, "%s %2d(%2d)", net->node_list[net->node_list[i].link_list[j].dest_node].name, net->node_list[i].link_list[j].work, net->node_list[i].link_list[j].spare ); outtextxy( x, y, buffer ); } /* for j */ y += 10; if ( y > (sy2-10) ) { y = sy1 + 30; x += 80; } x -= 7; } /* for i */ } /* display_stats */ void display_relabel( Graph_type loc, int node, int num ) { char buffer[81]; sprintf( buffer, "%d", num ); setcolor( WHITE ); outtextxy( loc.node[node].x-5, loc.node[node].y+1, "ллл" ); setcolor( BLUE ); outtextxy( loc.node[node].x-5, loc.node[node].y+1, buffer ); } /* display_relabel */ /* Graphically shows pushes from node i to node j. */ void display_pushIJ( Graph_type loc, int A, int B, int num ) { void *background; unsigned int size; int x1, y1, x2, y2; int numSteps = 20; float deltaX, deltaY; float x1float, y1float; int i; char buffer[81]; int offset = 7; x1 = loc.node[A].x; y1 = loc.node[A].y; x2 = loc.node[B].x; y2 = loc.node[B].y; x1float = x1; y1float = y1; deltaX = ((float) (x1 - x2)) / ((float) numSteps); deltaY = ((float) (y1 - y2)) / ((float) numSteps); setcolor( RED ); /* calculate the size of the image */ size = imagesize( x1-offset, y1-offset, x1+offset, y1+offset ); /* allocate memory to hold the background */ background = malloc( size ); /* repeat until a key is pressed */ for ( i = 0; i < numSteps; i++ ) { /* grab the background */ getimage( x1-offset, y1-offset, x1+offset, y1+offset, background ); rectangle( x1-offset, y1-offset, x1+offset, y1+offset ); sprintf( buffer, "%d", num ); outtextxy( x1-offset+2, y1-offset, buffer ); /* pause so user can see movement */ delay(DELAY); /* restore the background */ putimage( x1-offset, y1-offset, background, COPY_PUT ); x1float -= deltaX; y1float -= deltaY; x1 = x1float; y1 = y1float; } /* clean up */ free( background ); } /* display_pushIJ */ void display_connectNodes( Graph_type loc, int A, int B ) { setcolor( RED ); line( loc.node[A].x, loc.node[A].y, loc.node[B].x, loc.node[B].y ); } /* connectNodes */ void display_init( char *filename ) { Network_type *net; net = load_net( filename ); init_graphics(); display_graph( net ); display_stats( net ); delete_net( net ); } /* display_init */ void display_delete( void ) { getch(); terminate_graphics(); } /* display_delete */ /* DEBUG void main( int argc, char *argv[] ) { Network_type *net; if ( argc <= 1 ) { printf("Network Display by Jim Williams\n"); printf("Usage: display filename.ext\n"); exit(0); } net = load_net(argv[1]); init_graphics(); display_graph( net ); display_stats( net ); pushIJ( loc, 1, 2 ); pushIJ( loc, 2, 3 ); pushIJ( loc, 3, 4 ); /* wait until a key is pressed * getch(); terminate_graphics(); delete_net( net ); } /* main */