CG Programs
- Get link
- X
- Other Apps
1. To draw a line using simple DDA algorithm
#include<stdio.h> #include<math.h> #include<conio.h> #include<graphics.h> void dda() { int x1,x2,y1,y2,i,step,xinc,yinc,dx,dy; printf("ENTER THE STARTING COORDINATE: "); scanf("%d %d",&x1,&y1); printf("ENTER THE END POINT COORDINATE: "); scanf("%d %d",&x2,&y2); dx=x2-x1; dy=y2-y1; if(abs(dx)>=abs(dy)){ step=abs(dx); } else { step=abs(dy); } for(i=0;i<=step;i++){ x1=x1+(dx/step); y1=y1+(dy/step); putpixel(x1,y1,6); } } void main(){ int gd=DETECT,gm; initgraph(&gd,&gm,"c://tc//bgi"); dda(); getch(); closegraph(); } |
2. Use Bresenham’s line drawing algorithm for drawing thick line
#include<stdio.h> #include<graphics.h> #include<math.h>
void bline(float x1, float x2, float y1, float y2); void main(){ int gd=DETECT,gm; Float wy,wx,x1,y1,x2,y2; Int i,thickness; initgraph(&gd,&gm,””); printf(“Enter x1,y1,x2,y2:\n”); scanf("%f%f%f%f",&x1,&y1,&x2,&y2); printf("\nEnter thickness of line: "); scanf("%d",&thickness); bline(x1,y1,x2,y2); if((y2-y1)/(x2-x1)<1) { wy=(thickness-1)*sqrt(pow((x2-x1),2)+pow((y2-y1),2))/(2*fabs(x2-x1)); for(i=0;i<wy;i++) { bline(x1,y1-i,x2,y2-i); bline(x1,y1+i,x2,y2+i); } } else { wx=(thickness-1)*sqrt(pow((x2-x1),2)+pow((y2-y1),2))/(2*fabs(y2-y1)); for(i=0;i<wx;i++) { bline(x1-i,y1,x2-i,y2); bline(x1+i,y1,x2+i,y2); } } getch(); closegraph(); restorecrtmode(); } void bline(float x1,float y1,float x2,float y2) { float xinc,yinc,x,y; float dx,dy,e; dx=abs(x2-x1); dy=abs(y2-y1); if(x1<x2) xinc=1; else xinc=-1; if(y1<y2) yinc=1; else yinc=-1; x=x1; y=y1; putpixel(x,y,WHITE); if(dx>=dy) { e=(2*dy)-dx; while(x!=x2) { if(e<0) { e+=(2*dy); } else { e+=(2*(dy-dx)); y+=yinc; } x+=xinc; putpixel(x,y,WHITE); } } else { e=(2*dx)-dy; while(y!=y2) { if(e<0) { e+=(2*dx); } else { e+=(2*(dx-dy)); x+=xinc; } y+=yinc; putpixel(x,y,WHITE); } } } |
3. To draw a line using Bresenham’s Line algorithm.
#include<stdio.h> #include<conio.h> #include<graphics.h> void drawLineBresenham(int x1, int y1, int x2, int y2){ int dx, dy, p, x, y; dx = x2 - x1; dy = y2 - y1; x = x1; y = y1; p = 2 * dy - dx; while(x<=x2){ putpixel(x,y,1); delay(10); if(p>=0){ y++; p = p + 2 * dy - 2 * dx; } else { p = p + 2 * dy; } x++; } } int main() { int gd=DETECT,gm; int x1,y1,x2,y2; initgraph(&gd,&gm,"C://TURBOC3//BGI"); printf("Enter the co-ordinates of the starting point(x1 y1): "); scanf("%d %d",&x1,&y1); printf("Enter the co-ordinates of the ending point(x2 y2): "); scanf("%d %d",&x2,&y2); drawLineBresenham(x1,y1,x2,y2); getch(); closegraph(); return 0; } |
4. To draw a circle using Bresenham’s Circle algorithm
#include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> #include <math.h> void EightWaySymmetricPlot(int xc,int yc,int x,int y) { putpixel(x+xc,y+yc,RED); putpixel(x+xc,-y+yc,YELLOW); putpixel(-x+xc,-y+yc,GREEN); putpixel(-x+xc,y+yc,YELLOW); putpixel(y+xc,x+yc,12); putpixel(y+xc,-x+yc,14); putpixel(-y+xc,-x+yc,15); putpixel(-y+xc,x+yc,6); } void BresenhamCircle(int xc,int yc,int r) { int x=0,y=r,d=3-(2*r); EightWaySymmetricPlot(xc,yc,x,y); while(x<=y) { if(d<=0) { d=d+(4*x)+6; } else { d=d+(4*x)-(4*y)+10; y=y-1; } x=x+1; EightWaySymmetricPlot(xc,yc,x,y); } } int main(void) { int xc,yc,r,gdriver = DETECT, gmode, errorcode; initgraph(&gdriver, &gmode, "C:\\TURBOC3\\BGI"); 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 */ } printf("Enter the values of xc and yc :"); scanf("%d%d",&xc,&yc); printf("Enter the value of radius :"); scanf("%d",&r); BresenhamCircle(xc,yc,r);
getch(); closegraph(); return 0; } |
5. To draw a circle using mid point circle algorithm
#include<stdio.h> #include<graphics.h> #include<math.h> main(){ int x1,y1,x,y,p; int gd=DETECT,gm; initgraph(&gd,&gm,""); printf("Enter the center(x1,y1) and the radius\n"); scanf("%d %d %d", &x1,&y1,&r); x=0; y=r; p=1-r; do{ putpixel(x1+x,y1+y,1); putpixel(x1+x,y1-y,1); putpixel(x1-x,y1+y,1); putpixel(x1-x,y1-y,1); putpixel(x1+y,y1+x,1); putpixel(x1+y,y1-x,1); putpixel(x1-y,y1+x,1); putpixel(x1-y,y1-x,1);
if (p<0){ p=p+2*x+3; x=x+1; } else{ p=p+2*x-2*y+5; x=x+1; y=y-1; } while(x<=y); getch(); closegraph(); } } |
6. To implement Point Clipping
#include<stdio.h> #include<conio.h> #include<graphics.h> void main() { int gm, gr, xcmin, ycmin, xcmax, ycmax, x, y, c; clrscr(); detectgraph(&gm, &gr); initgraph(&gm, &gr,"d:\\tc\\BGI"); printf("Enter the clipmin coordinate :\n"); scanf("%d%d", &xcmin, &ycmin); printf("Enter the clipmax coordinate :\n"); scanf("%d%d",&xcmax, &ycmax); rectangle(xcmin, ycmax, xcmax, ycmin); printf("Enter the coordinate of the point:\n"); scanf("%d%d",&x,&y); detectgraph(&gm, &gr); initgraph(&gm, &gr,"d:\\tc\\BGI"); putpixel(x,y,15); printf("\n1.Point clipping\n2.Exit\nEnter your choice:\n"); scanf("%d",&c); switch(c) { case 1: detectgraph(&gm, &gr); initgraph(&gm, &gr,"d:\\tc\\BGI"); rectangle(xcmin, ycmax, xcmax, ycmin); printf("*******POINT CLIPPING******\n"); if((xcmin<x) && (x<xcmax)) { if((ycmin<y) && (y<ycmax)) { printf("The point is inside the clip window\n"); putpixel(x,y,15); } } else printf("The point is outside the clipwindow \nThe point is clipped\n"); break; case 2: exit(0); } getch(); } |
7. Window is defined by A(20,20), B(90,20), C(90,70) and D(20,70). Clip the line
p1p2 with p1(5,60) and p2(30,90) using Cohen-Sutherland line clipping algo.
#include <stdio.h> #include <conio.h> #include <graphics.h> #define INSIDE 0 // 0000 #define LEFT 1 // 0001 #define RIGHT 2 // 0010 #define BOTTOM 4 // 0100 #define TOP 8 // 1000 int xmin, ymin, xmax, ymax; int computeCode(int x, int y) { int code = INSIDE; if (x < xmin) code |= LEFT; else if (x > xmax) code |= RIGHT; if (y < ymin) code |= BOTTOM; else if (y > ymax) code |= TOP; return code; } void cohenSutherlandClip(int x1, int y1, int x2, int y2) { int code1 = computeCode(x1, y1); int code2 = computeCode(x2, y2); int accept = 0; while (1) { if (!(code1 | code2)) { accept = 1; break; } else if (code1 & code2) { break; } else { int code_out; int x, y; if (code1 != 0) code_out = code1; else code_out = code2; if (code_out & TOP) { x = x1 + (x2 - x1) * (ymax - y1) / (y2 - y1); y = ymax; } else if (code_out & BOTTOM) { x = x1 + (x2 - x1) * (ymin - y1) / (y2 - y1); y = ymin; } else if (code_out & RIGHT) { y = y1 + (y2 - y1) * (xmax - x1) / (x2 - x1); x = xmax; } else if (code_out & LEFT) { y = y1 + (y2 - y1) * (xmin - x1) / (x2 - x1); x = xmin; } if (code_out == code1) { x1 = x; y1 = y; code1 = computeCode(x1, y1); } else { x2 = x; y2 = y; code2 = computeCode(x2, y2); } } } if (accept) { setcolor(RED); line(x1, y1, x2, y2); } } int main() { int gd = DETECT, gm; initgraph(&gd, &gm, "C:\\Turboc3\\BGI"); xmin = 20; ymin = 20; xmax = 90; ymax = 70; rectangle(xmin, ymin, xmax, ymax); int x1 = 5, y1 = 60, x2 = 30, y2 = 90; setcolor(WHITE); line(x1, y1, x2, y2); cohenSutherlandClip(x1, y1, x2, y2); getch(); closegraph(); return 0; } |
8. Clip the line between (100,100) to (450,300) against the window
(Xwmin,Ywmin)=(200,200) and (Xwmax,Ywmax) = (400,400) using Liang-
Barsky line clipping algo.
#include <stdio.h> #include <conio.h> #include <graphics.h> int x_min, y_min, x_max, y_max; void LBClip(int x1, int y1, int x2, int y2) { float u1 = 0.0, u2 = 1.0, dx = x2 - x1, dy = y2 - y1, p[4], q[4], r; p[0] = -dx; p[1] = dx; p[2] = -dy; p[3] = dy; q[0] = x1 - x_min; q[1] = x_max - x1; q[2] = y1 - y_min; q[3] = y_max - y1; for (int i = 0; i < 4; i++) { if (p[i] == 0) { if (q[i] < 0) { return; } } else { r = q[i] / p[i]; if (p[i] < 0) { if (r > u2) { return; } else if (r > u1) { u1 = r; } } else { if (r < u1) { return; } else if (r < u2) { u2 = r; } } } } int new_x1 = x1 + u1 * dx; int new_y1 = y1 + u1 * dy; int new_x2 = x1 + u2 * dx; int new_y2 = y1 + u2 * dy; setcolor(RED); line(new_x1, new_y1, new_x2, new_y2); } int main() { int gd = DETECT, gm; initgraph(&gd, &gm, "C:\\Turboc3\\BGI"); x_min = 200; y_min = 200; x_max = 400; y_max = 400; rectangle(x_min, y_min, x_max, y_max); int x1 = 100, y1 = 100, x2 = 450, y2 = 300; setcolor(WHITE); line(x1, y1, x2, y2); LBClip(x1, y1, x2, y2); getch(); closegraph(); return 0; } |
9. To Translate an object
#include<conio.h> #include<graphics.h> #include<stdio.h> void main() { int gd=DETECT,gm;
int l[2][2],v[2]={10,15},i=0,j; clrscr(); initgraph(&gd,&gm,"C:\\TURBOC3\\BGI"); printf("Enter the initial and final coordinates of a line ");
while(i<2) { printf("x%d and y%d = ",i,i); j=0; scanf("%d",&l[i][j]); scanf("%d",&l[i][j+1]); i++; } // Line before translation line(l[0][0],l[0][1],l[1][0],l[1][1]); setcolor(BLUE); // Line after translation line(l[0][0]+v[0],l[0][1]+v[1],l[1][0]+v[0],l[1][1]+v[1]); getch(); closegraph(); } |
10.To Scale any object
#include<stdio.h> #include<conio.h> #include<graphics.h> void main(){ int x,y,x1,y1,x2,y2; int scl_fctr_x,scl_fctr_y; int gd=DETECT,gm; initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("\t\t\t********** Scaling ***********\n"); printf("\n\t\t\t Please enter first coordinate of Triangle = "); scanf("%d %d",&x,&y); printf("\n\t\t\t Please enter second coordinate of Triangle = "); scanf("%d %d",&x1,&y1); printf("\n\t\t\t Please enter third coordinate of Triangle = "); scanf("%d %d",&x2,&y2);
line(x,y,x1,y1); line(x1,y1,x2,y2); line(x2,y2,x,y);
printf("\n\t\t\t Now Enter Scaling factor x and y = "); scanf("%d %d",&scl_fctr_x,&scl_fctr_y);
x = x* scl_fctr_x; x1 = x1* scl_fctr_x; x2 = x2* scl_fctr_x; y = y* scl_fctr_y; y1 = y1* scl_fctr_y; y2= y2 * scl_fctr_y ;
line(x,y,x1,y1); line(x1,y1,x2,y2); line(x2,y2,x,y); getch(); closegraph();} |
11.To rotate any object
#include<graphics.h> #include<stdio.h> #include<conio.h> #include<math.h>
void main(){ int gd=DETECT,gm; int pivot_x,pivot_y,x,y; double degree,radian; int rotated_point_x,rotated_point_y; initgraph(&gd,&gm,"C://TURBOC3//BGI"); cleardevice();
printf("\t\t*********** ROTATION *********** \n"); printf("\n Enter an initial coordinates of the line = "); scanf("%d %d",&pivot_x,&pivot_y); printf("\n Enter a final coordinates of the line = "); scanf("%d %d",&x,&y); line(pivot_x,pivot_y,x,y); printf("\n\n Now, Enter a degree = "); scanf("%lf",°ree);
radian=degree*0.01745; rotated_point_x=(int)(pivot_x+((x-pivot_x)*cos(radian)-(y-pivot_y)*sin(radian))); rotated_point_y=(int)(pivot_y+((x-pivot_x)*sin(radian)+(y-pivot_y)*cos(radian)));
setcolor(RED); line(pivot_x,pivot_y,rotated_point_x,rotated_point_y); getch(); closegraph();} |
12.To implement Boundary fill algorithm using Four connected method
#include<stdio.h> #include<graphics.h> #include<dos.h>
void boundaryfill(int x,int y,int f_color,int b_color) { if(getpixel(x,y)!=b_color && getpixel(x,y)!=f_color) { putpixel(x,y,f_color); boundaryfill(x+1,y,f_color,b_color); boundaryfill(x,y+1,f_color,b_color); boundaryfill(x-1,y,f_color,b_color); boundaryfill(x,y-1,f_color,b_color); } } //getpixel(x,y) gives the color of specified pixel
int main() { int gm,gd=DETECT,radius; int x,y; printf("Enter x and y positions for circle\n"); scanf("%d%d",&x,&y); printf("Enter radius of circle\n"); scanf("%d",&radius); initgraph(&gd,&gm,"c:\\turboc3\\bgi"); circle(x,y,radius); boundaryfill(x,y,4,15); delay(5000); closegraph(); return 0; } |
13.To implement Boundary fill algorithm using Eight connected method
#include<stdio.h> #include<graphics.h> #include<dos.h>
void boundaryfill(int x,int y,int f_color,int b_color) { if(getpixel(x,y)!=b_color && getpixel(x,y)!=f_color) { putpixel(x, y, f_colour); boundary_fill(x + 1, y, f_colour, b_colour); boundary_fill(x - 1, y, f_colour, b_colour); boundary_fill(x, y + 1, f_colour, b_colour); boundary_fill(x, y - 1, f_colour, b_colour); boundary_fill(x + 1, y + 1, f_colour, b_colour); boundary_fill (x - 1, y - 1, f_colour, b_colour); boundary_fill(x + 1, y - 1, f_colour, b_colour); boundary_fill(x - 1, y + 1, f_colour, b_colour); } } //getpixel(x,y) gives the color of specified pixel
int main() { int gm,gd=DETECT,radius; int x,y; printf("Enter x and y positions for circle\n"); scanf("%d%d",&x,&y); printf("Enter radius of circle\n"); scanf("%d",&radius); initgraph(&gd,&gm,"c:\\turboc3\\bgi"); circle(x,y,radius); boundaryfill(x,y,4,15); delay(5000); closegraph(); return 0; } |
14.To implement flood fill algorithm
#include<stdio.h> #include<graphics.h> void floodfill(int x, int y, int old, int newcol) { int current = getpixel(x, y); if(current == old) { putpixel(x, y, newcol); floodfill(x + 1, y, old, newcol); floodfill(x - 1, y, old, newcol); floodfill(x, y + 1, old, newcol); floodfill(x, y - 1, old, newcol); floodfill(x + 1, y + 1, old, newcol); floodfill(x - 1, y + 1, old, newcol); floodfill(x + 1, y - 1, old, newcol); floodfill(x - 1, y - 1, old, newcol); } } void main() { int gd = DETECT, gm; initgraph(&gd, &gm, "C:\\TURBOC3\\BGI"); rectangle(50, 50, 150, 150); floodfill(70, 70, 0, 15); getch(); closegraph(); } |
15.To implement Bezier Curves
#include <stdio.h> #include <graphics.h> #include <math.h>
int x[4]={200,100,200,250}; int y[4]={200,150,75,100};
void bezier () { int i; double t,xt,yt;
for (t = 0.0; t < 1.0; t += 0.0005) { xt = pow(1-t,3)*x[0]+3*t*pow(1-t,2)*x[1]+3*pow(t,2)*(1-t)*x[2]+pow(t,3)*x[3]; yt = pow(1-t,3)*y[0]+3*t*pow(1-t,2)*y[1]+3*pow(t,2)*(1-t)*y[2]+pow(t,3)*y[3]; putpixel (xt, yt,WHITE); } for (i=0; i<4; i++) putpixel (x[i], y[i], YELLOW); getch(); closegraph(); } void main() { int gd = DETECT, gm; initgraph (&gd, &gm, "..\\bgi"); bezier (); } |
16. Implement Reflection about origin
#include<graphics.h> #include<conio.h> void reflectAboutOrigin(int x1, int y1, int x2, int y2) { int gd = DETECT, gm; initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
// Original line setcolor(WHITE); line(x1, y1, x2, y2);
// Reflected line setcolor(RED); line(-x1, -y1, -x2, -y2); getch(); closegraph(); } int main() { int x1 = 100, y1 = 100, x2 = 200, y2 = 200; reflectAboutOrigin(x1, y1, x2, y2); return 0; } |
17.Perform Reflection along X-axis
#include<graphics.h> #include<conio.h> void reflectAboutXAxis(int x1, int y1, int x2, int y2) { int gd = DETECT, gm; initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
// Original line setcolor(WHITE); line(x1, y1, x2, y2);
// Reflected line setcolor(RED); line(x1, -y1, x2, -y2); getch(); closegraph(); } int main() { int x1 = 100, y1 = 100, x2 = 200, y2 = 200; reflectAboutXAxis(x1, y1, x2, y2); return 0; } |
18.Perform Reflection along Y-axis
#include<graphics.h> #include<conio.h> void reflectAboutYAxis(int x1, int y1, int x2, int y2) { int gd = DETECT, gm; initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
// Original line setcolor(WHITE); line(x1, y1, x2, y2);
// Reflected line setcolor(RED); line(-x1, y1, -x2, y2); getch(); closegraph(); } int main() { int x1 = 100, y1 = 100, x2 = 200, y2 = 200; reflectAboutYAxis(x1, y1, x2, y2); return 0; } |
19.Perform Reflection along the line(x=y)
#include<graphics.h> #include<conio.h> void reflectAlongLine(int x1, int y1, int x2, int y2) { int gd = DETECT, gm; initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
// Original line setcolor(WHITE); line(x1, y1, x2, y2);
// Reflected line setcolor(RED); line(y1, x1, y2, x2); getch(); closegraph(); } int main() { int x1 = 100, y1 = 100, x2 = 200, y2 = 200; reflectAlongLine(x1, y1, x2, y2); return 0; } |
20. Implement X-shear and y-shear
#include <graphics.h> #include <stdlib.h> #include <stdio.h> void drawRectangle(int x1, int y1, int x2, int y2) { int gd = DETECT, gm; initgraph(&gd, &gm, "C:\\TURBOC3\\BGI"); // Original rectangle rectangle(x1, y1, x2, y2); // X-shear transformation int x_shear_factor = 2; line(x1 + x_shear_factor * y1, y1, x2 + x_shear_factor * y2, y2); // Y-shear transformation int y_shear_factor = 2; line(x1, y1 + y_shear_factor * x1, x2, y2 + y_shear_factor * x2); getch(); closegraph(); } int main() { int x1 = 100, y1 = 100, x2 = 200, y2 = 200; drawRectangle(x1, y1, x2, y2); return 0; } |
21. fill the polygon using scanline polygon filling algorithm with the test cases given
below,
1) Number of vertices : 3 A(100,200) , B(50 100), C(200,20)
2) Number of vertices: 5 A(50,100) , B (10,40 ) , C(60,60) , D (70,80), E(100,20)
#include <graphics.h> #include <stdio.h> void scanlineFill(int numVertices, int arrX[], int arrY[]) { int gd = DETECT, gm; initgraph(&gd, &gm, "C:\\TURBOC3\\BGI"); int i, j, temp, x1, y1, x2, y2, ymax; for (i = 0; i < numVertices; i++) { j = (i + 1) % numVertices; x1 = arrX[i]; y1 = arrY[i]; x2 = arrX[j]; y2 = arrY[j]; if (y1 > y2) { temp = x1; x1 = x2; x2 = temp; temp = y1; y1 = y2; y2 = temp; } if (y1 == y2) { if (x1 > x2) { temp = x1; x1 = x2; x2 = temp; } for (j = x1; j <= x2; j++) putpixel(j, y1, WHITE); } else { for (j = y1; j <= y2; j++) { ymax = (x2 - x1) * (j - y1) / (y2 - y1) + x1; for (int k = x1; k <= ymax; k++) putpixel(k, j, WHITE); } } } getch(); closegraph(); } int main() { int numVertices1 = 3; int arrX1[] = {100, 50, 200}; int arrY1[] = {200, 100, 20}; scanlineFill(numVertices1, arrX1, arrY1); int numVertices2 = 5; int arrX2[] = {50, 10, 60, 70, 100}; int arrY2[] = {100, 40, 60, 80, 20}; scanlineFill(numVertices2, arrX2, arrY2); return 0; } |
22. Create a given fractal object.
23. Program for creating simple car shape
#include <graphics.h> #include <conio.h> int main() { // Initializing graphic driver and graphic mode variable int graphicdriver=DETECT, graphicmode; // Calling initgraph function with certain parameters initgraph(&graphicdriver, &graphicmode, "C:\\TURBOC3\\BGI"); // Printing message for the user outtextxy(100, 100, "Program to draw a car with pre-defined functions in C graphics"); // Using pre-defined functions like rectangle and circle. rectangle(200, 300, 500, 400); // Car body rectangle(300, 250, 450, 300); // Car roof circle(250, 410, 25); // Left wheel circle(450, 410, 25); // Right wheel line(500, 300, 500, 400); // Rear window getch(); closegraph(); return 0; } |
24. Write a C-program for performing the translation of 3D object?
#include<stdio.h> #include<conio.h> #include<math.h> #include<process.h> #include<graphics.h> int x1,x2,y1,y2,mx,my,depth; void draw(); void trans(); void main() { int gd=DETECT,gm,c; initgraph(&gd,&gm,"C:\\TurboC3\\BGI"); printf("\n\t\t3D Translation\n\n"); printf("\nEnter 1st top value(x1,y1):"); scanf("%d%d",&x1,&y1); printf("Enter right bottom value(x2,y2):"); scanf("%d%d",&x2,&y2); depth=(x2-x1)/4; mx=(x1+x2)/2; my=(y1+y2)/2; draw(); getch(); cleardevice(); trans(); getch(); } void draw() { bar3d(x1,y1,x2,y2,depth,1); } void trans() { int a1,a2,b1,b2,dep,x,y; printf("\n Enter the Translation Distances:"); scanf("%d%d",&x,&y); a1=x1+x; a2=x2+x; b1=y1+y; b2=y2+y; dep=(a2-a1)/4; bar3d(a1,b1,a2,b2,dep,1); setcolor(5); draw(); } |
25. Write a C program for movement of ball from left to right and vice-versa
#include <graphics.h> #include <conio.h> int main() { int gd = DETECT, gm, midx, midy; initgraph(&gd, &gm, "C:\\TURBOC3\\BGI"); midx = getmaxx() / 2; midy = getmaxy() / 2; int radius = 50; int x = midx - radius, y = midy; int xmax = getmaxx() - radius; int xmin = radius; int direction = 1; // 1 for right, -1 for left while (!kbhit()) { setcolor(BLACK); circle(x, y, radius); x += direction; setcolor(WHITE); circle(x, y, radius); delay(10); if (x >= xmax || x <= xmin) { direction *= -1; } } getch(); closegraph(); return 0; } |
26. Write a C program for man walking
#include <graphics.h> #include <conio.h> void drawMan(int x, int y) { circle(x, y, 10); // head line(x, y + 10, x, y + 30); // body line(x, y + 30, x - 10, y + 50); // left leg line(x, y + 30, x + 10, y + 50); // right leg line(x, y + 15, x - 10, y + 25); // left hand line(x, y + 15, x + 10, y + 5); // right hand } int main() { int gd = DETECT, gm; initgraph(&gd, &gm, "C:\\TURBOC3\\BGI"); int maxX = getmaxx(); for (int i = 0; i < maxX - 100; i++) { cleardevice(); drawMan(i, 100); delay(100); } getch(); closegraph(); return 0; } |
27. Draw our national flag by using graphics functions and fill appropriate colors by using boundary fill algorithm.
#include<stdio.h> #include<conio.h> #include<graphics.h> #include<dos.h> #include<math.h> int main() { int gdriver = DETECT,gmode, a,b,i,r,x,y;
initgraph(&gdriver,&gmode,"C:\\TC\\BGI");
//draw the bottom rectangle setfillstyle(SOLID_FILL,1); rectangle(10,320,200,340); floodfill(11,321,15); rectangle(30,300,175,320); floodfill(31,301,15); rectangle(50,280,155,300); floodfill(51,281,15); //pole setfillstyle(SOLID_FILL,3); rectangle(100,38,110,280); floodfill(101,39,15); //draw the top rectangle setfillstyle(SOLID_FILL,RED); rectangle(110,40,220,58); floodfill(111,43,15); setfillstyle(SOLID_FILL,15); rectangle(110,58,220,78); floodfill(111,59,15); setfillstyle(SOLID_FILL,GREEN); rectangle(110,78,220,98); floodfill(111,79,15); //Ashok chakra // a=160; b=68; r=13; setcolor(BLUE); circle(a,b,r); for(i=0;i<=360;i=i+25) { x=r*cos(i*3.14/180); y=r*sin(i*3.14/180); line(a,b,a+x,b-y); } getch(); return 0; closegraph(); } |
- Get link
- X
- Other Apps
Comments
Post a Comment