COMPUTER GRAPHICS PROGRAMS | |
|
5 a. Program
to implement Bresenhm's circle drawing algorithm.
5 b.Call the above routine to generate a cardiod pattern. (Draw successive circles).
6 b.Program to draw a spirial for the specified centre of the spiral.
/* PROGRAM #1 - Bresenhan's Polyline */ #include#include int x[20],y[20],dx,dy,maxx,maxy; void exchange(int *,int *); void setpixel(int,int,int); void linebres(int,int,int,int); void exchange(int *v1,int *v2) { int temp; temp = *v1; *v1 = *v2; *v2 = temp; } void setpixel(int x,int y,int sgrtr1) { if(sgrtr1) exchange(&x,&y); putpixel(x,y,15); } void linebres(int xa,int ya,int xb,int yb) { int slope_type,sgrtr1=0,p; dx= abs(xa - xb); dy= abs(ya - yb); if(dy dx) sgrtr1 = 1; if(sgrtr1) { exchange(&dx,&dy); exchange(&xa,&ya); exchange(&xb,&yb); } p = 2 * dy - dx; if(xa xb) /* Make (Xb,Yb) as the starting point */ { exchange(&xa,&xb); exchange(&ya,& yb); } slope_type = (yb ya) ? 1 : -1; setpixel(xa,ya,sgrtr1); while(xa <0) p = p + 2 * dy; /* Next point is (Xi+1,Yi) */ else { ya = ya + slope_type; p = p + 2 * (dy - dx); /* Next point is (Xi+1,Yi+1) */ } setpixel(xa,ya,sgrtr1); } /* End While construct */ } /* End Function */ void main() { int gd = DETECT,gm,i,n; initgraph(&gd, &gm, ""); cleardevice(); printf("Enter the number of points : "); scanf("%d",&n); for(i = 0; i >< n; i++) { printf("Enter the co-ordinates of point %d : ",i+1); scanf("%d %d",&x[i],&y[i]); } cleardevice(); maxx = getmaxx() / 2; maxy = getmaxy() / 2; line(0,maxy,getmaxx(),maxy); /* Draws X axis */ line(maxx,0,maxx,getmaxy()); /* Draws Y axis */ if(n == 1) putpixel(maxx + x[0],maxy - y[0],4); else for(i = 0; i < n-1; i++) linebres(maxx + x[i],maxy - y[i],maxx + x[i + 1],maxy - y[i + 1]); getch(); } /* PROGRAM #2 - Bresenham's Line Thickness */ #include #include int x[20],y[20],dx,dy; void exchange(int *,int *); void setpixel(int,int,int); void linebres(int,int,int,int); void exchange(int *v1,int *v2) { int temp; temp = *v1; *v1 = *v2; *v2 = temp; } void setpixel(int x,int y,int sgrtr1) { if (sgrtr1) exchange(&x,&y); putpixel(x,y,15); } void linebres(int xa,int ya,int xb,int yb) { int slope_type,sgrtr1 = 0,p; dx = abs(xa - xb); dy = abs(ya - yb); if(dy dx) sgrtr1 = 1; if(sgrtr1) { exchange(&dx,&dy); exchange(&xa,&ya); exchange(&xb,&yb); } p = 2 * dy - dx; if(xa xb) /* Make (Xb,Yb) as the starting point */ { exchange(&xa,&xb); exchange(&ya,& yb); } slope_type = (yb ya) ? 1 : -1; setpixel(xa,ya,sgrtr1); while(xa <0) p = p + 2 * dy; /* Next point is (Xi+1,Yi) */ else { ya = ya + slope_type; p = p + 2 * (dy - dx); /* Next point is (Xi+1,Yi+1) */ } setpixel(xa,ya,sgrtr1); } /* End While construct */ } /* End_function */ void main() { int gd = DETECT, gm,i,n,thick,j,maxx,maxy; initgraph(&gd, &gm, ""); cleardevice(); printf("Enter the number of points : "); scanf("%d",&n); for(i = 0; i >< n; i++) { printf("Enter the co-ordinates of point %d : ",i+1); scanf("%d %d",&x[i],&y[i]); } printf("Enter the thickness of the line :"); scanf("%d",&thick); cleardevice(); maxx = getmaxx() / 2; maxy = getmaxy() / 2; line(0,maxy,getmaxx(),maxy); /* Draws X axis */ line(maxx,0,maxx,getmaxy()); /* Draws Y axis */ if(n == 1) putpixel(maxx + x[0],maxy - y[0],4); else for(j = 1; j <= thick; j++) for(i = 0;i < n-1; i++) linebres(maxx+x[i]+j,maxy-y[i]+j,maxx+x[i+1]+j,maxy-y[i+1]+j); getch(); /* Add 'j' to all the coordinates */ } /* PROGRAM #3 - Bar Graph */ #include #include #define lr 0 #define lc 0 #define hr 479 #define hc 639 struct eachvalue { int val,barhigh; char label[20]; } item[100]; void Bar(int sc,int sr,int ec,int er) { line(sc,sr,sc,er); line(sc,sr,ec,sr); line(ec,sr,ec,er); } void showgraph(int n,int neg) { int i,hor,bstart = 80,thick = 450 / n; char str[80],temp[80]; hor = (neg) ? 226 : 452; /* If the graph has - ve values then */ setcolor(GREEN); /* draw the X axis in the middle of the screen.*/ line(15,hor,hc - 15,hor); /* Draws the X axis */ line(bstart - 15,lr + 15,bstart - 15,hr - 15); /* Draws Y axis */ for (i=0;i<n;i++) { itoa(item[i].val,str,10); /* Stores values in 'str' */ setcolor(RED); Bar(bstart,hor - item[i].barhigh,bstart + thick,hor); /* Draws bar */ setcolor(WHITE); settextstyle(2,1,6); if (item[i].val = 0) outtextxy(bstart+(thick-20)/2,hor-10-10*strlen(item[i].label),item[i].label); else /* Displays Label */ outtextxy(bstart+(thick-20)/2,hor+10,item[i].label); settextstyle(2,0,6); setcolor(YELLOW); outtextxy(bstart+2,hor-item[i].barhigh-((item[i].val=0) ? 20 : 0),str); bstart += thick + 3; /* Displays values of each bar */ } /* End for loop construct */ settextstyle(2,0,5); setcolor(MAGENTA); itoa(0,str,10); outtextxy(lc+20,hor,str); /* Displays the Origin */ } void main() { int n,gd = DETECT,gm,i; int scale,maxval = 0,neg = 0,npar; clrscr(); printf("\nEnter the number of entries : "); scanf("%d",&n); for (i=0;i<n;i++) { printf("Enter the Value and Label for item %d :\n",i+1); scanf("%d %s",&item[i].val,&item[i].label); if (abs(item[i].val) maxval) maxval = abs(item[i].val); if (item[i].val <0) neg = 1; /* Indicates Bars with -ve height are present */ } npar = (neg) ? 5 : 10; scale = (maxval / npar) + ((maxval % 10) ? 1 : 0); for (i=0;i><n;i++) item[i].barhigh = ((float) item[i].val / scale) * 40; initgraph(&gd,&gm,""); /* Finds the barheight using the values */ cleardevice(); /* entered and does scaling. */ showgraph(n,neg); getch(); closegraph(); } /* PROGRAM #4 - Line Graph */ #include #include #define lr 0 #define lc 0 #define hr 479 #define hc 639 struct eachvalue { int val,barhigh; char label[20]; } item[100]; void polyline(int x[],int y[],int n,int color) { int i; setcolor(color); if (n == 1) putpixel( *x, *y,color); else for (i=0;i<n-1;i++) line(x[i],y[i],x[i+1],y[i+1]); } void showgraph(int n,int neg) { int i,hor,bstart = 80,thick = 450 / n,x[10],y[10]; char str[80],temp[80]; hor = (neg) ? 226 : 452; /* If the graph has - ve values then */ setcolor(GREEN); /* draw the X axis in the middle of the screen.*/ line(15,hor,hc-15,hor); /* Draws the X axis */ line(bstart-15,lr+15,bstart-15,hr-15); /* Draws Y axis */ for (i=0;i<n;i++) { itoa(item[i].val,str,10); x[i] = bstart + thick / 2; y[i] = hor - item[i].barhigh; setcolor(5); outtextxy(x[i]-3,y[i]-6,"*"); setcolor(WHITE); settextstyle(2,1,4); /* Displays the Label */ if (item[i].val = 0) outtextxy(bstart+(thick-20)/2,hor-1*strlen(item[i].label),item[i].label); else outtextxy(bstart+(thick-20)/2,hor+10,item[i].label); settextstyle(2,0,6); /* Displays the values */ setcolor(YELLOW); outtextxy(bstart+2,hor-item[i].barhigh-((item[i].val=0)?20:0),str); bstart += thick + 3; } /* End For Loop construct */ polyline(x,y,n,11); settextstyle(2,0,5); setcolor(MAGENTA); itoa(0,str,10); outtextxy(lc + 20,hor,str); /* Displays the Origin */ } void main() { int n,gd = DETECT,gm,i; int scale,maxval = 0,neg = 0,npar; clrscr(); printf("Enter the number of entries : "); scanf("%d",&n); for (i=0;i<n;i++) { printf("Enter the value and label for item %d :\n",i+1); scanf("%d %s",&item[i].val,&item[i].label); if (abs(item[i].val) maxval) maxval = abs(item[i].val); if (item[i].val <0) neg = 1; /* Indicates Bars with -ve value are present */ } npar = (neg) ? 5 : 10; scale = (maxval / npar) + ((maxval % 10) ? 1 : 0); for (i=0;i><n;i++) item[i].barhigh = ((float) item[i].val / scale) * 40; initgraph(&gd,&gm,""); /* Finds the barheight using the values */ cleardevice(); /* entered and does scaling. */ showgraph(n,neg); getch(); closegraph(); } /* PROGRAM # 5A - Circle */ #include #include void plot_circle_points(int x,int y,int xc,int yc) { putpixel(xc + x,yc + y, 15); putpixel(xc - x,yc + y, 15); putpixel(xc + x,yc - y, 15); putpixel(xc - x,yc - y, 15); putpixel(xc + y,yc + x, 15); putpixel(xc - y,yc + x, 15); putpixel(xc + y,yc - x, 15); putpixel(xc - y,yc - x, 15); } void bres_circle(int xc,int yc,int radius) { int x = 0,y = radius,p = 3 - 2 * radius; /* Select first position (Xi,Yi) as (0,r) */ while(x <0) p += 4 * x + 6; /* Next point is (Xi+1,Yi) */ else { y--; p += 4 * (x - y) + 10; /* Next point is (Xi+1,Yi-1) */ } } /* End While construct */ if(x == y) plot_circle_points(x,y,xc,yc); } void main (void) { int x,y,r,gd = DETECT,gm,maxx,maxy; initgraph(&gd,&gm,""); printf("Enter (xc,yc) and radius:\n"); scanf("%d %d %d",&x,&y,&r); cleardevice(); maxx = getmaxx() / 2; maxy = getmaxy() / 2; line(0,maxy,getmaxx(),maxy); /* Draws X axis */ line(maxx,0,maxx,getmaxy()); /* Draws Y axis */ bres_circle(x + maxx,maxy - y,r); putpixel(x + maxx,maxy - y,14); /* Draws the centre point of circle */ getch(); closegraph(); } > /* PROGRAM #5B - Cardioid */ #include #include #include void plot_circle_points(int x,int y,int xc,int yc) { putpixel(xc + x,yc + y, 14); putpixel(xc - x,yc + y, 14); putpixel(xc + x,yc - y, 14); putpixel(xc - x,yc - y, 14); putpixel(xc + y,yc + x, 14); putpixel(xc - y,yc + x, 14); putpixel(xc + y,yc - x, 14); putpixel(xc - y,yc - x, 14); } void bres_circle(int xc,int yc,int radius) { int x = 0,y = radius,p = 3 - 2 * radius; /* Select first position (Xi,Yi) as (0,r) */ while(x <0) p += 4 * x + 6; /* Next point is (Xi+1,Yi) */ else { y--; p += 4 * (x - y) + 10; /* Next point is (Xi+1,Yi-1) */ } } /* End While construct */ if(x == y) plot_circle_points(x,y,xc,yc); } void main (void) { int x,y,r,gd = DETECT,gm,maxx,maxy; float s,t; /* Note : 's' & 't' must always be declared as float */ initgraph(&gd,&gm,""); printf("Enter (xc,yc),radius:"); scanf("%d %d %d",&x,&y,&r); cleardevice(); maxx = getmaxx() / 2; maxy = getmaxy() / 2; line(0,maxy,getmaxx(),maxy); /* Draws X axis */ line(maxx,0,maxx,getmaxy()); /* Draws Y axis */ for(t = 0; t ><= 6.28; t += 0.01) { /* s=r*t; for spiral */ s = r * (1 + cos(t)); /* s=r*cos(3*t); for 3 leaf */ bres_circle(maxx + x + s * cos(t),maxy - y + s * sin(t),s); delay(15); } getch(); closegraph(); } /* PROGRAM #6A - Ellipse */ #include #include void plotpoints(float x,float y, int cx,int cy) { if (x <1) x = 0; if (y ><1) y = 0; putpixel(cx + x,cy - y, 15); putpixel(cx - x,cy - y, 15); putpixel(cx + x,cy + y, 15); putpixel(cx - x,cy + y, 15); } void Ellipse(int cx,int cy,float rx,float ry) { float x,y,p,endv,k; x = 0; /* Select first point as (0,ry) */ y = ry; k = (ry * ry) / (rx * rx); p = 2 * k - 2 * ry + 1; /* Initial decision parameter */ while(x > <-1) break; if(p ><0) p += 4 * k * x + 6 * k; /* Next point is (Xi+1,Yi) */ else { p += 4 * k * x - 4 * y + 6 * k * 4; /* Next point is (Xi+1,Yi-1) */ y--; } x++; } /* End While construct 1 */ endv = y; y = 0; x = rx; k = (rx * rx) / (ry * ry); while(y > <0) p += 4 * k * y + 6 * k; else { p += 4 * k * y - 4 * x + 6* k + 4; x--; } y++; } /* End While construct 2 */ } void main (void) { int ex,ey,gd = DETECT,gm,sx,sy; float rx,ry,r; initgraph(&gd,&gm,""); cleardevice(); printf("\nEnter the radii(Major & Minor):\n"); scanf("%f %f",&rx,&ry); printf("Enter the co-ords. of the centre:\n"); scanf("%d %d",&ex,&ey); cleardevice(); Ellipse(ex,480 - ey,rx,ry); getch(); closegraph(); } > /* PROGRAM #6B - Spiral */ #include #include #include #define PI 3.14159654 void spiral(int sx,int sy,float r) { float t,s; for(t = 0; t <= 6 * PI; t += 0.01) { s = r * (t / (6 * PI)); putpixel(sx + s * cos(t),sy + s * sin(t),15); } } void main (void) { int gd = DETECT,gm,sx,sy; float r; initgraph(&gd,&gm,""); cleardevice(); printf("\nEnter the spiral radius:"); scanf("%f",&r); printf("Enter the co-ords. of the centre(Xc,Yc) :\n"); scanf("%d %d",&sx,&sy); cleardevice(); spiral(sx,480 - sy,r); getch(); closegraph(); } /* PROGRAM #7 - -rotate-translate / translate-rotate & X,Y shear */ #include #include #include int ps[4][2]; void translate(int q[4][2],int tx,int ty) { int i; for(i=0;i<4;i++) { q[i][0] += tx; /* X' = X + Tx */ q[i][1] += ty; /* Y' = Y + Ty */ } } void rotate( int q[4][2],float angle) { int i; /* X' = Xr+(X-Xr)*Cos(t) - (Y-Yr)*Sin(t) */ /* Y' = Yr+(X-Xr)*Sin(t) + (Y-Yr)*Cos(t) */ angle /= (180 / 3.1415); for(i=0;i<4;i++) { q[i][0] = ps[3][0] + (ps[i][0]-ps[3][0])*cos(angle) - (ps[i][1]-ps[3][1])*sin(angle); q[i][1] = ps[3][0] + (ps[i][0]-ps[3][0])*sin(angle) + (ps[i][1]-ps[3][1])*cos(angle); } } void xshear(int p[4][2],float sh) { p[2][0] += sh * ps[2][1]; p[3][0] += sh * ps[3][1]; } void yshear(int p[4][2],float sh) { p[1][1] += sh * ps[1][0]; p[2][1] += sh * ps[2][0]; } void draw(int p[4][2]) { int i; /* Draws the Rectangle */ for(i=0;i<3;i++) line(p[i][0],480-p[i][1],p[i+1][0],480-p[i+1][1]); line(p[0][0],480-p[0][1],p[3][0],480-p[3][1]); getch(); } void reset(int q[4][2]) { int i; for(i=0;i<4;i++) { q[i][0] = ps[i][0]; q[i][1] = ps[i][1]; } } void main(void) { int q[4][2],i,tx,ty,gd = DETECT,gm; float rotn,xs,ys; initgraph(&gd,&gm,""); printf("\nEnter Top left (x1,y1) and Bottom right (x2,y2) :\n"); scanf("%d %d",&ps[0][0],&ps[0][1]); scanf("%d %d",&ps[2][0],&ps[2][1]); ps[1][0] = ps[2][0]; ps[1][1] = ps[0][1]; ps[3][0] = ps[0][0]; ps[3][1] = ps[2][1]; printf("\nEnter the translation value across x and y axes (tx,ty) :\n"); scanf("%d %d",&tx,&ty); printf("\nEnter the rotation angle in degrees:\n"); scanf("%f",&rotn); printf("\nEnter the shear values across x and y axes:\n"); scanf("%f %f",&xs,&ys); cleardevice(); outtext("Rotation followed by translation"); draw(ps); reset(q); rotate(q,rotn); draw(q); translate(q,tx,ty); draw(q); cleardevice(); reset(q); outtext("Translation followed by Rotation"); draw(ps); translate(ps,tx,ty); reset(q); draw(q); rotate(q,rotn); draw(q); cleardevice(); reset(q); outtext("X and Y direction shear"); draw(q); xshear(q,xs); draw(q); yshear(q,ys); draw(q); closegraph(); } /* PROGRAM #8 - Scaling w.r.t origin,w.r.t point;Reflect about y=x */ #include #include #include int ps[13][2]; void scale(int p[13][2],int fx,int fy,float sx,float sy) { int i; /* X' = Sx * X + Xr * (1 - Sx) */ /* Y' = Sy * Y + Yr * (1 - Sy) */ for (i=0;i<13;i++) { p[i][0] = sx * ps[i][0] + fx * (1 - sx); p[i][1] = sy * ps[i][1] + fy * (1 - sy); } } void reflectxy(int p[13][2]) { /* Exchange Xi with Yi */ int i,temp; for (i=0;i<13;i++) { temp = p[i][0]; p[i][0] = p[i][1]; p[i][1] = temp; } } void draw(int p[13][2]) { int i; /* Draws a house like figure */ for (i = 0; i < 12; i++) line(p[i][0],480 - p[i][1],p[i+1][0],480 - p[i+1][1]); line(p[0][0],480 - p[0][1],p[12][0],480 - p[12][1]); getch(); } void reset(int q[13][2]) { int i; for (i=0;i<13;i++) { q[i][0] = ps[i][0]; q[i][1] = ps[i][1]; } cleardevice(); } void main() { int gd = DETECT,gm,x,y,i,q[13][2]; float sx,sy; initgraph(&gd,&gm,""); ps[0][0] = 50; ps[0][1] = 20; ps[6][0] = 250; ps[6][1] = 70; ps[1][0] = 140; ps[1][1] = 20; ps[7][0] = 190; ps[7][1] = 70; ps[2][0] = 140; ps[2][1] = 40; ps[8][0] = 190; ps[8][1] = 90; ps[3][0] = 160; ps[3][1] = 40; ps[9][0] = 150; ps[9][1] = 130; ps[4][0] = 160; ps[4][1] = 20; ps[10][0] = 110; ps[10][1] = 90; ps[5][0] = 250; ps[5][1] = 20; ps[11][0] = 110; ps[11][1] = 70; ps[12][0] = 50; ps[12][1] = 70; printf("Enter the co-ordinates of any arbitrary point :\n"); scanf("%d %d",&x,&y); printf("Enter the scaling values (Sx,Sy) along the X & Y axis :\n"); scanf("%f %f",&sx,&sy); reset(q); draw(q); scale(q,50,20,sx,sy); outtext("Scale w.r.t Origin"); draw(q); reset(q); draw(q); /* 'x' & 'y' are arbitrary points */ scale(q,50 + x,20 + y,sx,sy); outtext("Scale w.r.t point entered "); draw(q); reset(q); /* Note : cleardevice() is done in reset() */ draw(q); reflectxy(q); outtext("Reflection about the line y = x "); draw(q); closegraph(); } /* PROGRAM #9 - Rotate Wheel */ #include #include #include void putwheel(int cx,int cy,int r,float angle) { int x,y; circle(cx,cy,r); x = r * cos(angle); y = r * sin(angle); line(cx - x,cy - y,cx + x,cy + y); /* These 2 lines draws the spokes */ line(cx - y,cy + x,cx + y,cy - x); line(1,201 + r,640,201 + r); } /* Draws the Surface for the wheel to rotate */ void main(void) { int gd = DETECT,gm,n,steps,r,x; float t; initgraph(&gd,&gm,""); printf("\nEnter the radius:"); scanf("%d",&r); printf("\nEnter the number of steps:"); scanf("%d",&steps); cleardevice(); if (steps 640) steps = 640; n = 640 / steps; for(x = 1,t = 0.1; x < 650; t += (float) n / r) { putwheel(x,200,r,t); /* Y coordinate remains the same (200) */ delay(300); /* & only the X coordinate of the wheel */ cleardevice(); /* is incremented,and a new wheel is */ x += n; /* drawn in each loop while erasing the */ } /* previous image of the wheel. */ putwheel(x,200,r,t); getch(); closegraph(); } /* PROGRAM #10 - Line Clipping */ #include #include struct eachpoint { int x,y; } pts[2],wmin,wmax,vmin,vmax,vp,vq; int vx,vy; void encode(struct eachpoint p,int code[4]) { code[0] = p.x < wmin.x; /* Encodes the lines endpoint using */ code[1] = p.x wmax.x; /* region codes. */ code[2] = p.y < wmin.y; code[3] = p.y wmax.y; } int accept(int c1[4],int c2[4]) { int k, acc = 1; for(k=0;k<4;k++) if(c1[k] || c2[k]) acc = 0; return acc; } int reject(int c1[4],int c2[4]) { int k,rej = 0; for(k=0;k<4;k++) if(c1[k] && c2[k]) rej = 1; return rej; } int ptinside(int c[4]) { if(c[0] || c[1] || c[2] || c[3]) return 0; return 1; } void clipline(struct eachpoint p1,struct eachpoint p2) { int c1[4],c2[4],done,draw,i,t; float m; struct eachpoint tp; done = draw = 0; while(!done) /* Repeat for all portions of line until it is */ { /* accepted or rejected. */ encode(p1,c1); encode(p2,c2); if(accept(c1,c2)) /* If the region codes for both end points [p1,p2]*/ done = draw = 1; /* are zero,the line is accepted for display. */ else if(reject(c1,c2)) /* If the region codes are 1 and the logical AND */ done = 1; /* of the codes is 1 then reject the line. */ else { if(ptinside(c1)) { for(i=0;i<4;i++) { t = c1[i]; c1[i] = c2[i]; c2[i] = t; } tp = p1; p1 = p2; p2 = tp; } if ((float) (p2.x - p1.x) <1 && (float) (p2.x - p1.x) > -1) m = 32000; else /* 'm' is the slope */ m = (float) (p2.y - p1.y) / (p2.x - p1.x); if (c1[0]) /* LEFT */ { /* Intersection of the first point */ p1.y += (wmin.x - p1.x) * m; /* with the window boundary. */ p1.x = wmin.x; } else if (c1[1]) /* RIGHT */ { p1.y += (wmax.x - p1.x) * m; p1.x = wmax.x; } else if (c1[2]) /* BOTTOM */ { p1.x += (wmin.y - p1.y) / m; p1.y = wmin.y; } else if (c1[3]) { p1.x += (wmax.y - p1.y) / m; /* TOP */ p1.y = wmax.y; } } /* End Else */ } /* End While construct */ if(draw) { line(p1.x,p1.y,p2.x,p2.y); /* Clipped line in window */ getch(); cleardevice(); vp.x = vmin.x + (p1.x-wmin.x) * (float)(vmax.x-vmin.x) / (wmax.x-wmin.x); vp.y = vmin.y + (p1.y-wmin.y) * (float)(vmax.y-vmin.y) / (wmax.y-wmin.y); vq.x = vmin.x + (p2.x-wmin.x) * (float)(vmax.x-vmin.x) / (wmax.x-wmin.x); vq.y = vmin.y + (p2.y-wmin.y) * (float)(vmax.y-vmin.y) / (wmax.y-wmin.y); outtext("Clipped line in viewport"); line(vp.x,vp.y,vq.x,vq.y); } } void main() { int i,driver = DETECT,mode,x,y; initgraph(&driver,&mode,""); printf("\nEnter the starting and ending co-ordinates of the line \n"); scanf("%d %d %d %d",&pts[0].x,&pts[0].y,&pts[1].x,&pts[1].y); printf("\nEnter clip window co-ordinates (Top left & Bottom right ends)\n"); scanf("%d %d %d %d",&wmin.x,&wmin.y,&wmax.x,&wmax.y); printf("\nEnter the viewport co-ordinates(Top left & Bottom right ends)\n"); scanf("%d %d %d %d",&vmin.x,&vmin.y,&vmax.x,&vmax.y); cleardevice(); outtext("ORIGINAL STATUS"); setcolor(5); line(pts[0].x,pts[0].y,pts[1].x,pts[1].y); rectangle(wmin.x,wmin.y,wmax.x,wmax.y); getch(); cleardevice(); outtext("CLIPPED LINE IN WINDOW"); rectangle(wmin.x,wmin.y,wmax.x,wmax.y); setcolor(6); clipline(pts[0],pts[1]); rectangle(vmin.x,vmin.y,vmax.x,vmax.y); /* Draws the Viewport */ getch(); closegraph(); } /* PROGRAM #11 - Sutherland-Hodgemann Polygon Clipping */ # include # include # include # include # include # include # include # define TRUE 1 # define FALSE 0 typedef struct pt { int x,y; } point; typedef enum x {LEFT,RIGHT,BOTTOM,TOP} Boundary; int ptscnt = 0,index = 0, newboundary[4], ptsin[40],ptsout[40], n; point WinMax,WinMin,s[4],firstpoint[4]; /* 'ptsin[]' stores the input co-ordiates of the polygon 'ptsout[]' stores the output co-ordinates of the clipped polygon 'ptscnt' stores the no. of points in the clipped polygon 'WinMax','WinMin' are the co-ordinates of the window */ int Inside (point p,Boundary b) { int flag = TRUE; switch(b) { case LEFT : if (p.x WinMax.x) flag = FALSE; break; case BOTTOM: if (p.y WinMax.y) flag = FALSE; break; } return flag; } int Cross (point p1,point p2,Boundary b) { if (Inside (p1,b) == Inside (p2,b)) return FALSE; else return TRUE; } point Intersect(point p1,point p2,Boundary b) { float m; point i; if (p1.x == p2.x) m = 20000; else m = (float) (p1.y-p2.y)/(p1.x-p2.x); switch(b) { case LEFT: i.x = WinMin.x; if (p1.y == p2.y) i.y = p1.y; else i.y = p2.y + (WinMin.x - p2.x) * m; break; case RIGHT: i.x = WinMax.x; if (p1.y == p2.y) i.y = p1.y; else i.y = p2.y + (WinMax.x - p2.x) * m; break; case BOTTOM: i.y = WinMin.y; if (p1.x == p2.x) i.x = p1.x; else i.x = p2.x + (WinMin.y - p2.y) / m; break; case TOP: i.y = WinMax.y; if (p1.x == p2.x) i.x = p1.x; else i.x = p2.x + (WinMax.y - p2.y) / m; break; } /* End Switch construct */ return i; } void ClipPoint(point p,Boundary b) { point i; if (newboundary[b]) { firstpoint[b] = p; newboundary[b] = FALSE; } else if (Cross(p,s[b],b)) { i = Intersect(p,s[b],b); if (b < = TOP; b++) { if (Cross(s[b],firstpoint[b],b)) { i = Intersect(s[b],firstpoint[b],b); if (b <= TOP; b++) newboundary[b] = TRUE; for (i = 0; i < n; i++) { tmp.x = ptsin[ptr++]; tmp.y = ptsin[ptr++]; ClipPoint(tmp,LEFT); } CloseClip(); } void main(void) { int ptr = 0,i; int gd = DETECT,gm; printf("\nEnter the no. of sides of the polygon.."); scanf("%d",&n); printf("\nEnter the vertices of the polygon.."); fflush(stdin); for (i=0;i<n;i++) { printf(" \nVertex %d ",i+1); scanf("%d %d",&ptsin[ptr++],&ptsin[ptr++]); } ptsin[ptr++] = ptsin[0]; ptsin[ptr] = ptsin[1]; printf("\nWindow Boundaries...\nMin x,y.."); scanf("%d %d",&WinMin.x,&WinMin.y); /* Min Window boundary */ printf("\nMax x,y.."); scanf("%d %d",&WinMax.x,&WinMax.y); /* Max Window boundary */ initgraph(&gd,&gm,""); rectangle(WinMin.x,WinMin.y,WinMax.x,WinMax.y); /* Draw Window */ drawpoly(n + 1,ptsin); /* "drawpoly()" draws the outline of a polygon */ getch(); cleardevice(); PolyClip(); ptsout[index++] = ptsout[0]; ptsout[index++] = ptsout[1]; drawpoly(ptscnt + 1,ptsout); getch(); } /***************************************************************************** Working -------- enter the # of points for the polygon then enter the x,y points for the polygon vertex by vertex enter the min & max values for the window finally the clipped polygon is being displayed. *******************************************************************************/ /* PROGRAM #12 - Car */ #include<stdio.h> #include<conio.h> #include<graphics.h> void body(); void wheel(); void main() { int gd = DETECT,gm,q; initgraph(&gd,&gm,""); setbkcolor(WHITE); while(1) { gotoxy(1,1); printf("\n1.Display body of car\n2.Display Wheels\n3.Display Car\n4.Exit\n"); q = getch(); cleardevice(); switch(q) { case '1' : body(); break; case '2' : wheel(); break; case '3' : body(); wheel(); break; case '4' : exit(0); } /* End of switch(q) */ } /* End of While(1) */ } /* End of Main() */ void body() { setcolor(RED); line(200,200,350,200); line(350,200,425,250); line(425,250,525,275); line(525,275,525,325); line(525,325,425,325); line(350,325,225,325); line(100,325,100,250); line(150,250,100,250); line(150,325,100,325); line(150,250,200,200); arc(188,325,0,180,36); arc(388,325,0,180,36); } void wheel() { setcolor(DARKGRAY); circle(188,325,33); circle(388,325,33); fillellipse(188,325,30,30); fillellipse(388,325,30,30); }
|