/************************************************************************** G R A P H I C . C ***************************************************************************/ /* Filename : graphic.c * purpose : Some graphics routines ***************************************************************************/ #include #include #include #include #include #include "piece.h" #include "isthdefs.h" extern int inverse_isthmus; extern int i; WINDOW *drwin, *stdscr; int result; int drwdone; int pt1x, pt1y, pt2x,pt2y; int lncolor; /************************************************************************** D R A W L I N E ***************************************************************************/ drawline(pt1x,pt1y,pt2x,pt2y,color) int pt1x,pt1y,pt2x,pt2y,color; { int x,y,deltax,deltay; float b,k, slope; float m,n; int i,l,p, vert; vert = 0; l = (pt2x - pt1x); p = (pt2y - pt1y); if (l == 0) { vert = 1; slope = 0; } else slope = (float)p / (float)l;; if ((pt2x - pt1x) < 0) deltax = -1 * (pt2x - pt1x); else deltax = pt2x - pt1x; b = pt1y - (slope * pt1x); if ((pt2y - pt1y) < 0) deltay = -1 * (pt2y - pt1y); else deltay = (pt2y - pt1y); if ((slope >= 1) || (slope <= -1) || (vert)) for (i = 0; i <= deltay; i++) if (pt1y - pt2y >= 0) { if (deltax == 0) x = pt1x; else x = (int)(((pt1y - i) - b)/ slope); /* WAS ROUND HERE */ VIS$SET_PIXEL(x,pt1y-i,color); } else { if (deltax == 0) x = pt1x; else x = (int)(((pt1y + i) - b) / slope); VIS$SET_PIXEL(x,pt1y+i,color); } else for (i = 0; i <= deltax; i++) if (pt1x - pt2x >= 0) { l = pt1x - i; m = slope * l; k = m + b ; y = (int)(k); VIS$SET_PIXEL(pt1x-i,y,color); } else { l = pt1x + i; m = slope * l; k = m + b; y = (int)(k); VIS$SET_PIXEL(pt1x+i,y,color); } } /* end drawline */ /************************************************************************** G E T P E R P L I N E ***************************************************************************/ getperpline(pt1x,pt1y,pt2x,pt2y,color, linex, liney) int pt1x,pt1y,pt2x,pt2y,color, *linex, *liney; { int grey; int x,y,deltax,deltay; float b,k, slope; float m,n; int i,l,p, vert; vert = 0; l = (pt2x - pt1x); p = (pt2y - pt1y); if (l == 0) { vert = 1; slope = 0; } else slope = (float)p / (float)l;; if ((pt2x - pt1x) < 0) deltax = -1 * (pt2x - pt1x); else deltax = pt2x - pt1x; b = pt1y - (slope * pt1x); if ((pt2y - pt1y) < 0) deltay = -1 * (pt2y - pt1y); else deltay = (pt2y - pt1y); if ((slope >= 1) || (slope <= -1) || (vert)) for (i = 0; i <= deltay; i++) if (pt1y - pt2y >= 0) { if (deltax == 0) x = pt1x; else x = round(((pt1y - i) - b)/ slope); grey = VIS$GET_PIXEL(x,pt1y-i); if (grey == 254) { *linex = x; *liney = pt1y-i; } if (grey == 0) return(0); /* stop the whole show */ VIS$SET_PIXEL(x,pt1y-i,color); } else { if (deltax == 0) x = pt1x; else x = round(((pt1y + i) - b) / slope); grey = VIS$GET_PIXEL(x,pt1y+i); if (grey == 254) { *linex = x; *liney = pt1y+i; } if (grey == 0) return(0); /* stop the whole show */ VIS$SET_PIXEL(x,pt1y+i,color); } else for (i = 0; i <= deltax; i++) if (pt1x - pt2x >= 0) { l = pt1x - i; m = slope * l; k = m + b ; y = round(k); grey = VIS$GET_PIXEL(pt1x-i,y); if (grey == 254) { *linex = pt1x-i; *liney = y; } if (grey == 0) return(0); /* stop the whole show */ VIS$SET_PIXEL(pt1x-i,y,color); } else { l = pt1x + i; m = slope * l; k = m + b; y = round(k); grey = VIS$GET_PIXEL(pt1x+i,y); if (grey == 254) { *linex = pt1x+i; *liney = y; } if (grey == 0) return(0); /* stop the whole show */ VIS$SET_PIXEL(pt1x+i,y,color); } } /* end getperpline */ /************************************************************************** G E T L I N E ***************************************************************************/ getline(pt1x,pt1y,pt2x,pt2y, color, linex, liney) int pt1x,pt1y,pt2x,pt2y,color, *linex, *liney; { int grey; int x,y,deltax,deltay; float b,k, slope; float m,n; int i,l,p, vert; vert = 0; l = (pt2x - pt1x); p = (pt2y - pt1y); if (l == 0) { vert = 1; slope = 0; } else slope = (float)p / (float)l;; if ((pt2x - pt1x) < 0) deltax = -1 * (pt2x - pt1x); else deltax = pt2x - pt1x; b = pt1y - (slope * pt1x); if ((pt2y - pt1y) < 0) deltay = -1 * (pt2y - pt1y); else deltay = (pt2y - pt1y); if ((slope >= 1) || (slope <= -1) || (vert)) for (i = 0; i <= deltay; i++) if (pt1y - pt2y >= 0) { if (deltax == 0) x = pt1x; else x = round(((pt1y - i) - b)/ slope); grey = VIS$GET_PIXEL(x,pt1y-i); if (grey == color) { *linex = x; *liney = pt1y-i; return(1); /* stop the whole show */ } /* VIS$SET_PIXEL(x,pt1y-i,color); */ } else { if (deltax == 0) x = pt1x; else x = round(((pt1y + i) - b) / slope); grey = VIS$GET_PIXEL(x,pt1y+i); if (grey == color) { *linex = x; *liney = pt1y+i; return(1); /* stop the whole show */ } /* VIS$SET_PIXEL(x,pt1y+i,color); */ } else for (i = 0; i <= deltax; i++) if (pt1x - pt2x >= 0) { l = pt1x - i; m = slope * l; k = m + b ; y = round(k); grey = VIS$GET_PIXEL(pt1x-i,y); if (grey == color) { *linex = pt1x-i; *liney = y; return(1); /* stop the whole show */ } /*VIS$SET_PIXEL(pt1x-i,y,color); */ } else { l = pt1x + i; m = slope * l; k = m + b; y = round(k); grey = VIS$GET_PIXEL(pt1x+i,y); if (grey == color) { *linex = pt1x+i; *liney = y; return(1); /* stop the whole show */ } /*VIS$SET_PIXEL(pt1x+i,y,color); */ } return(0); /* didn't find elevation */ } /* end getperpline */ /********************************************************************** S C A L E P O L Y ***********************************************************************/ scalepoly(scalex,scaley,pnm) float scalex,scaley; int pnm; { int i; for(i=1; i<= piece[pnm].pathcnt; i++) { piece[pnm].avgx[i] = piece[pnm].avgx[i] * scalex; piece[pnm].avgy[i] = piece[pnm].avgy[i] * scaley; } } /*********************************************************************** S H O W S T R I N G ************************************************************************/ showstring(string,x,y,color) char *string; int x,y,color; { char ch, let; int index, k; int character[10][8],smallbuf[10][8]; for(k=0;*string != '\0'; string++) { index = getindex(*string); showletter(index,x,y,color); x += 10; } } /**************************************************************************** G E T I N D E X *****************************************************************************/ getindex(ch) char ch; { int index; if(ch >= 48 && ch <=90) index = ch - '/'; else index = 0; return(index); } /***************************************************************************** S H O W L E T T E R ******************************************************************************/ showletter(lindex,x,y,color) int lindex,x,y,color; { unsigned i, j, mask, shift, lcolor; int doit = 0; static int alphabet[44][11] = { {' ',0,0,0,0,0,0,0,0,0,0}, {'0',24,36,66,66,66,66,66,66,36,24}, {'1',8,24,8,8,8,8,8,8,8,62}, {'2',28,34,65,1,2,12,16,32,64,127}, {'3',28,34,65,2,28,2,1,65,34,28}, {'4',2,6,10,18,34,127,2,2,2,7}, {'5',127,64,64,124,2,1,1,65,34,28}, {'6',28,34,65,64,92,98,65,65,34,28}, {'7',127,65,1,2,2,4,4,8,8,8}, {'8',28,34,65,34,28,34,65,65,34,28}, {'9',28,34,65,65,35,29,1,65,34,28}, {':',0,0,0,8,0,0,8,0,0,0}, {';',0,0,0,8,0,0,8,16,0,0}, {'<',0,0,2,4,8,16,8,4,2,0}, {'=',0,0,0,0,62,0,62,0,0,0}, {'>',0,0,32,16,8,4,8,16,32,0}, {'?',12,18,33,1,2,4,8,0,0,8}, {'@',127,127,127,127,127,127,127,127,127,127}, {'A',8,20,34,65,65,127,65,65,65,65}, {'B',124,66,66,66,124,66,65,65,65,126}, {'C',28,34,66,64,64,64,64,66,34,28}, {'D',124,66,65,65,65,65,65,65,66,124}, {'E',127,64,64,64,127,64,64,64,64,127}, {'F',127,64,64,64,126,64,64,64,64,64}, {'G',62,65,64,64,79,73,65,65,65,62}, {'H',65,65,65,65,127,65,65,65,65,65}, {'I',28,8,8,8,8,8,8,8,8,28}, {'J',2,2,2,2,2,2,66,66,66,60}, {'K',65,66,68,72,96,80,72,68,66,65}, {'L',64,64,64,64,64,64,64,64,64,127}, {'M',65,99,85,73,73,65,65,65,65,65}, {'N',65,65,97,81,73,69,67,67,65,65}, {'O',62,65,65,65,65,65,65,65,65,62}, {'P',126,65,65,65,126,64,64,64,64,64}, {'Q',62,65,65,65,65,65,73,69,67,62}, {'R',124,66,65,66,124,72,68,66,65,65}, {'S',28,34,64,64,60,2,1,65,34,28}, {'T',127,8,8,8,8,8,8,8,8,8}, {'U',65,65,65,65,65,65,65,65,65,62}, {'V',65,65,65,65,65,65,65,34,20,8}, {'W',65,65,65,65,73,85,85,99,99,65}, {'X',65,65,34,20,8,20,34,65,65,65}, {'Y',65,65,65,34,20,8,8,8,8,8}, {'Z',127,1,2,4,8,16,32,64,64,127} }; for(i=0;i<=9;i++) for(j=0;j<=7;j++) { mask = 128; shift = mask >> j; doit = (shift & (alphabet[lindex][i+1])); if( doit > 0 ) VIS$SET_PIXEL(x+j,y+i,color); } } /***************************************************************************** C E N T E R *****************************************************************************/ center(pcnum) int pcnum; { double x0 = 0.0; double y0 = 0.0; int i; for(i=1;i<=piece[pcnum].pathcnt;i++) { x0 += (double)piece[pcnum].avgx[i] / (double)piece[pcnum].pathcnt; y0 += (double)piece[pcnum].avgy[i] / (double)piece[pcnum].pathcnt; } piece[pcnum].centx = (float)x0; piece[pcnum].centy = (float)y0; } /**************************************************************************** T R A N S P O L Y *****************************************************************************/ transpoly(pcnum,deltax,deltay) int pcnum; float deltax,deltay; { int i; for(i=1;i<=piece[pcnum].pathcnt;i++) { piece[pcnum].avgx[i] += deltax; piece[pcnum].avgy[i] += deltay; } for(i=1;i<=piece[pcnum].pos_skelx[0];i++) { piece[pcnum].pos_skely[i] += (int)deltay; piece[pcnum].pos_skelx[i] += (int)deltax; } for(i=1;i<=piece[pcnum].inv_skelx[0];i++) { piece[pcnum].inv_skely[i] += (int)deltay; piece[pcnum].inv_skelx[i] += (int)deltax; } } /**************************************************************************** R O T A T E P O L Y *****************************************************************************/ rotatepoly(pcnum,theta) int pcnum; float theta; { int i; float x,radeg; double cs,sn; radeg = (3.14 / 180.0) * theta; cs = cos((double)radeg); sn = sin((double)radeg); center(pcnum); /* **************************** */ /* rotate the border points */ /* **************************** */ for(i=1;i<=piece[pcnum].pathcnt;i++) { x = piece[pcnum].centx + (cs*(piece[pcnum].avgx[i] - piece[pcnum].centx)) - (sn*(piece[pcnum].avgy[i] - piece[pcnum].centy)); piece[pcnum].avgy[i] = piece[pcnum].centy + (sn*(piece[pcnum].avgx[i] - piece[pcnum].centx))+ (cs*(piece[pcnum].avgy[i] - piece[pcnum].centy)); piece[pcnum].avgx[i] = x; } /* **************************** */ /* rotate the skeleton points */ /* **************************** */ for(i=1;i<=piece[pcnum].pos_skelx[0];i++) { x = piece[pcnum].centx + (cs*(piece[pcnum].pos_skelx[i] - piece[pcnum].centx)) - (sn*(piece[pcnum].pos_skely[i] - piece[pcnum].centy)); piece[pcnum].pos_skely[i] = piece[pcnum].centy + (sn*(piece[pcnum].pos_skelx[i] - piece[pcnum].centx))+ (cs*(piece[pcnum].pos_skely[i] - piece[pcnum].centy)); piece[pcnum].pos_skelx[i] = x; } for(i=1;i<=piece[pcnum].inv_skelx[0];i++) { x = piece[pcnum].centx + (cs*(piece[pcnum].inv_skelx[i] - piece[pcnum].centx)) - (sn*(piece[pcnum].inv_skely[i] - piece[pcnum].centy)); piece[pcnum].inv_skely[i] = piece[pcnum].centy + (sn*(piece[pcnum].inv_skelx[i] - piece[pcnum].centx))+ (cs*(piece[pcnum].inv_skely[i] - piece[pcnum].centy)); piece[pcnum].inv_skelx[i] = x; } center(pcnum); } /**************************************************************************** D R A W D I S P *****************************************************************************/ drawdisp() { int p1x,p1y,p2x,p2y,clr; drwin = subwin(stdscr,22,66,0,13); clearok(drwin,TRUE); wclear(drwin); wrefresh(drwin); box(drwin,VERTCH,HORZCH); wrefresh(drwin); echo(); wmove(drwin,1,25); wstandout(drwin); waddstr(drwin," Draw A Line "); wstandend(drwin); wrefresh(drwin); wmove(drwin,5,20); waddstr(drwin," Enter Point 1 X => : "); wrefresh(drwin); wscanw(drwin,"%d",&p1x); wrefresh(drwin); wmove(drwin,7,20); waddstr(drwin," Enter Y => : "); wrefresh(drwin); wscanw(drwin,"%d",&p1y); wrefresh(drwin); wmove(drwin,9,20); waddstr(drwin," Enter Point 2 X => : "); wrefresh(drwin); wscanw(drwin,"%d",&p2x); wrefresh(drwin); wmove(drwin,11,20); waddstr(drwin," Enter Y => : "); wrefresh(drwin); wscanw(drwin,"%d",&p2y); wrefresh(drwin); wmove(drwin,13,20); waddstr(drwin," Enter Color of line "); wrefresh(drwin); wscanw(drwin,"%d",&clr); wrefresh(drwin); wclear(drwin); wrefresh(drwin); drawline(p1x,p1y,p2x,p2y,clr); noecho(); } view() { /*VIS$GRAB(-1); */ VIS$VIEW(); } freeze() { /*VIS$GRAB(1); */ VIS$SNAP(); } /************************************************************************** EUCLID_DISTANCE ***************************************************************************/ float euclid_dist(x1,y1,x2,y2) int x1,y1,x2,y2; { float a, b; a = (float)(x2 - x1); b = (float)(y2 - y1); a = a*a; b = b*b; return ( (float)sqrt((double)(a+b)) ); } /************************************************************************** CHESSBOARD_DISTANCE ***************************************************************************/ chessboard_dist(x1,y1,x2,y2) int x1,y1,x2,y2; { return ( max( abs(x1-x2), abs (y2-y1)) ); } /************************************************************************** CITY_BLOCK_DISTANCE ***************************************************************************/ city_block_dist(x1,y1, x2,y2) int x1,y1, x2,y2; { int x; x = abs(x2-x1) + abs(y2-y1); return ( x ); } /************************************************************************** D I S T A N C E ***************************************************************************/ float distance(x1,y1,x2,y2) int x1,y1,x2,y2; { float a,b; float q; double c; a = (float)(x2 - x1); b = (float)(y2 - y1); a = a*a; b = b*b; #ifdef RWW return ( (float)sqrt((double)(a+b)) ); #endif return ( (float)(a+b) ); } /************************************************************************** G E T _ M A X _ X Y ***************************************************************************/ get_max_xy (x, y, length, minx, miny, maxx, maxy) int *x, *y, length, *minx, *miny, *maxx, *maxy; { int i; *minx = *miny = 512; *maxx = *maxy = 0; for (i=1; i<=length; i++) { if (x[i] > *maxx) *maxx = x[i]; if (x[i] < *minx) *minx = x[i]; /* */ if (y[i] > *maxy) *maxy = y[i]; if (y[i] < *miny) *miny = y[i]; } /*printf("minx: %d maxx: %d miny: %d maxy: %d \n",*minx,*maxx,*miny,*maxy); */ } mark_centriod(pcnum, color) int pcnum, color; { accenthoriz ((int)piece[pcnum].centx, (int)piece[pcnum].centy, color); } /**************************************************************************** FINDMIDPOINT ****************************************************************************/ findmidpoint(x1,y1,x2,y2,midx,midy) int x1,y1,x2,y2,*midx,*midy; { *midx = round( ((float) x1 + (float) x2) / 2.0 ); *midy = round( ((float) y1 + (float) y2) / 2.0 ); } /************************************************************************ D R A W A L L I S T H M I I *************************************************************************/ draw_all_isthmii(pcnum,accentclr, lineclr, wait) int pcnum, accentclr, lineclr, wait; { int i, index1, index2, x1, y1, x2, y2; int numisth, p; if (inverse_isthmus) { numisth = piece[pcnum].inv_isthnum; for(i=1; i<=numisth; i++) { index1 = piece[pcnum].inv_isth[i].indexbeg; index2 = piece[pcnum].inv_isth[i].indexend; x1 = (int)piece[pcnum].avgx[index1]; y1 = (int)piece[pcnum].avgy[index1]; x2 = (int)piece[pcnum].avgx[index2]; y2 = (int)piece[pcnum].avgy[index2]; accenthoriz (x1, y1, accentclr); accenthoriz (x2, y2, accentclr); drawline(x1, y1, x2, y2, lineclr); printstat(piece[pcnum].inv_isth[i].lenseg, piece[pcnum].inv_isth[i].skeldist, piece[pcnum].inv_isth[i].index_dist); if (wait) if (numisth > 1) waitforkey(); #ifdef ROGER #endif } } else { p = piece[pcnum].pathcnt; numisth = piece[pcnum].isthnum; for(i=1; i<=numisth; i++) { index1 = piece[pcnum].dmtisth[i].indexbeg; index2 = piece[pcnum].dmtisth[i].indexend % p; x1 = (int)piece[pcnum].avgx[index1]; y1 = (int)piece[pcnum].avgy[index1]; x2 = (int)piece[pcnum].avgx[index2]; y2 = (int)piece[pcnum].avgy[index2]; accenthoriz (x1, y1, accentclr); accenthoriz (x2, y2, accentclr); drawline(x1, y1, x2, y2, lineclr); printstat(piece[pcnum].dmtisth[i].lenseg, piece[pcnum].dmtisth[i].skeldist, piece[pcnum].dmtisth[i].index_dist); if (wait) if (numisth > 1) waitforkey(); } }/* end else */ } /* end function */ /************************************************************************ DRAW_SKELETON *************************************************************************/ draw_skeleton(pcnum, color) int pcnum, color; { int x,y; /*printf ("totalskel: %d\n", piece[pcnum].pos_skelx[0]); */ for (i=1; i < piece[pcnum].pos_skelx[0]; i++) { x = piece[pcnum].pos_skelx[i]; y = piece[pcnum].pos_skely[i]; VIS$SET_PIXEL (x, y, color); } } /* end function */ /************************************************************************ DRAW_INV_SKELETON *************************************************************************/ draw_inv_skeleton(pcnum, color) int pcnum, color; { int x,y; /*printf ("totalskel: %d\n", piece[pcnum].inv_skelx[0]); */ for (i=1; i < piece[pcnum].inv_skelx[0]; i++) { x = piece[pcnum].inv_skelx[i]; y = piece[pcnum].inv_skely[i]; VIS$SET_PIXEL (x, y, color); } } /* end function */