/********Joshua G. Schafrath Computer Graphics************/ #include #include #include "mygraph.h" #include "point.h" #include "Xroutines.h" dc_draw_line ( line aline, environment * env) { int dx,dy,y,d,incE,incNE,inc,xsign,ysign; point pt; printf("line %d %d %d %d\n",aline.e1.x,aline.e1.y,aline.e2.x,aline.e2.y); fflush(stdout); dy = aline.e1.y - aline.e2.y; /***if the line is vertical, just loop thru the y's for x***/ if((dx=aline.e1.x-aline.e2.x)==0){ pt.x= aline.e1.x; if(dy >= 0){ /*upward line*/ for(pt.y=aline.e1.y; pt.y > (aline.e2.y-1); pt.y--){ printf("%d %d\n",pt.x,pt.y); fflush(stdout); dc_draw_point(pt, env); } } else{ /*downward line*/ for(pt.y=aline.e1.y; pt.y < (1+aline.e2.y); pt.y++){ printf("%d %d\n",pt.x,pt.y); fflush(stdout); dc_draw_point(pt, env); } } } /***if the line is horizontal, just loop thru the x's for y***/ else if(dy==0){ pt.y= aline.e1.y; if(dx >= 0){ /*left line*/ for(pt.x=aline.e1.x; pt.x > (aline.e2.x-1); pt.x--){ printf("%d %d\n",pt.x,pt.y); fflush(stdout); dc_draw_point(pt, env); } } else{ /*right line*/ for(pt.x=aline.e1.x; pt.x < (1+aline.e2.x); pt.x++){ printf("%d %d\n",pt.x,pt.y); fflush(stdout); dc_draw_point(pt, env); } } } /***if the line is a diagonal, just loop changing x and y***/ else if((abs(dy)) == (abs(dx))){ pt.y= aline.e1.y; if(dx >= 0){ /*left line*/ if(dy >= 0) inc=-1; /*upward line*/ else inc=1; /*downward line*/ for(pt.x=aline.e1.x; pt.x > (aline.e2.x-1); pt.x--){ printf("%d %d\n",pt.x,pt.y); fflush(stdout); dc_draw_point(pt, env); pt.y= pt.y + inc; } } else{ /*right line*/ if(dy >= 0) inc=-1; /*upward line*/ else inc=1; /*downward line*/ for(pt.x=aline.e1.x; pt.x < (1+aline.e2.x); pt.x++){ printf("%d %d\n",pt.x,pt.y); fflush(stdout); dc_draw_point(pt, env); pt.y= pt.y + inc; } } } else{ /* not a vertical, horizontal, or diagonal line */ if((abs(dx)) > (abs(dy))){ /*sections 4,5,1,and8*/ if(dy < 0){ /* down */ inc=1;xsign=1;ysign=-1; if(dx > 0) xsign=-1; /*left*/ } else{ /* up */ inc=-1;xsign=1;ysign=1; if(dx > 0) xsign=-1; /*left*/ } d = (2 * (ysign * dy)) + (xsign * dx); incE = 2 * (ysign * dy); incNE = 2 * ((ysign * dy) + (xsign *dx)); pt.y = aline.e1.y; if (dx < 0){ /*section 1 and 8*/ for (pt.x=aline.e1.x; pt.x < (1+aline.e2.x); pt.x++){ printf("%d %d %d\n",pt.x,pt.y,d); fflush(stdout); dc_draw_point(pt, env); if (d > 0){ d = d + incNE; pt.y = pt.y + inc; }else{ d = d + incE; } } } else{ /*section 4 and 5*/ for (pt.x=aline.e1.x; pt.x > (aline.e2.x-1); pt.x--){ printf("%d %d %d\n",pt.x,pt.y,d); fflush(stdout); dc_draw_point(pt, env); if (d > 0){ d = d + incNE; pt.y = pt.y + inc; }else{ d = d + incE; } } } } else{ /* sections 2,3,6,and7 */ if(dx < 0){ /* right */ inc=1;ysign=1;xsign=-1; if(dy > 0) ysign=-1; /*up*/ } else{ /* left */ inc=-1;ysign=1;xsign=1; if(dy > 0) ysign=-1; /*down*/ } d = (2 * (xsign * dx)) + (ysign * dy); incE = 2 * (xsign * dx); incNE = 2 * ((xsign * dx) + (ysign *dy)); pt.x = aline.e1.x; if (dy < 0){ /*section 6 and 7*/ for (pt.y=aline.e1.y; pt.y < (1+aline.e2.y); pt.y++){ printf("%d %d %d\n",pt.x,pt.y,d); fflush(stdout); dc_draw_point(pt, env); if (d > 0){ d = d + incNE; pt.x = pt.x + inc; }else{ d = d + incE; } } } else{ /*section 2 and 3*/ for (pt.y=aline.e1.y; pt.y > (aline.e2.y-1); pt.y--){ printf("%d %d %d\n",pt.x,pt.y,d); fflush(stdout); dc_draw_point(pt, env); if (d > 0){ d = d + incNE; pt.x = pt.x + inc; }else{ d = d + incE; } } } } } }