Programas de computação gráfica
Escreva um programa para desenhar construções gráficas básicas como linha, círculo, arco, elipse e retângulo.
#include #include void main() { intgd=DETECT,gm; initgraph (&gd,&gm,'c:\tc\bgi'); setbkcolor(GREEN); printf('
LINE'); line(50,40,190,40); printf('
RECTANGLE'); rectangle(125,115,215,165); printf('
ARC'); arc(120,200,180,0,30); printf('
CIRCLE'); circle(120,270,30); printf('
ECLIPSE'); ellipse(120,350,0,360,30,20); getch(); } Saída
Escreva um programa para desenhar animação usando círculos crescentes preenchidos com diferentes cores e padrões.
#include #include void main() { intgd=DETECT, gm, i, x, y; initgraph(&gd, &gm, 'C:\TC\BGI'); x=getmaxx()/3; y=getmaxx()/3; setbkcolor(WHITE); setcolor(BLUE); for(i=1;i<=8;i++) { setfillstyle(i,i); delay(20); circle(x, y, i*20); floodfill(x-2+i*20,y,blue); } getch(); closegraph(); < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-2.webp" alt="Computer Graphics Programs"> <hr> <h3>Program to make screen saver in that display different size circles filled with different colors and at random places.</h3> <pre> #include #include #include'graphics.h' #include'stdlib.h' void main() { intgd=DETECT,gm,i=0,x,xx,y,yy,r; //Initializes the graphics system initgraph(&gd,&gm,'c:\tc\bgi'); x=getmaxx(); y=getmaxy(); while(!kbhit()) { i++; // setfillstyle(random(i),random(30)); circle(xx=random(x),yy=random(y),random(30)); setfillstyle(random(i),random(30)); floodfill(xx,yy,getmaxcolor()); delay(200); } getch(); } </pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-3.webp" alt="Computer Graphics Programs"> <hr> <h3>Write a Program to make a moving colored car using inbuilt functions.</h3> <pre> #include #include int main() { intgd=DETECT,gm, i, maxx, cy; initgraph(&gd, &gm, 'C:\TC\BGI'); setbkcolor(WHITE); setcolor(RED); maxx = getmaxx(); cy = getmaxy()/2; for(i=0;i<maxx-140;i++) { cleardevice(); line(0+i,cy-20, 0+i, cy+15); line(0+i, cy-20, 25+i, cy-20); line(25+i, 40+i, cy-70); line(40+i, cy-70, 100+i, line(100+i, 115+i, line(115+i, 140+i, cy+15, 18+i, circle(28+i, 10); line(38+i, 102+i, circle(112+i, cy+15,10); line(122+i, cy+15 ,140+i,cy+15); line(140+i, rectangle(50+i, cy-62, 90+i, cy-30); setfillstyle(1,blue); floodfill(5+i, cy-15, red); setfillstyle(1, lightblue); floodfill(52+i, cy-60, delay(10); } getch(); closegraph(); return 0; < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-4.webp" alt="Computer Graphics Programs"> <br> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-5.webp" alt="Computer Graphics Programs"> <hr> <h3>Write a Program to print your name in Hindi script on console output in C.</h3> <pre> #include #include #include #include void main() { int gd=DETECT,gm; initgraph(&gd,&gm,'c:\tc\bgi'); setbkcolor(9); line(100,100,370,100); line(120,100,120,170); arc(143,100,0,180,23); line(165,100,165,155); arc(150,155,100,0,15); line(180,100,180,170); circle(210,140,10); line(210,130,250,130); circle(280,140,10); line(280,130,330,130); line(330,100,330,170); line(345,100,345,170); ellipse(337,100,0,180,9,18); getch(); } </pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-6.webp" alt="Computer Graphics Programs"> <hr> <h3>Write a Program control a ball using arrow keys.</h3> <pre> #include #include void main() { intgd=DETECT,gm,x,y,r=40; charch; initgraph(&gd,&gm,'C:/TURBOC3/BGI'); setbkcolor(3); x=getmaxx()/2; y=getmaxy()/2; setfillstyle(1,RED); circle(x,y,r); floodfill(x,y,getmaxcolor()); while((ch=getch())!=13) { switch(ch) { case 75: if(x>=r+1) { cleardevice(); circle(x-=10,y,r); floodfill(x,y,getmaxcolor()); } break; case 72: if(y>=r+1) { cleardevice(); circle(x,y-=10,r); floodfill(x,y,getmaxcolor()); } break; case 77: if(x<=(getmaxx()-r-10)) { cleardevice(); circle(x+="10,y,r);" floodfill(x,y,getmaxcolor()); } break; case 80: if(y<="(getmaxy()-r-10))" circle(x,y+="10,r);" getch(); < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-7.webp" alt="Computer Graphics Programs"> <hr> <h3>Write a Program to implement Digital Clock.</h3> <pre> #include #include #include #include struct time t; void display(int,int,int); void main() { int i=0,gd=DETECT,gm,hr,min,sec; clrscr(); initgraph(&gd,&gm,'c:\turboc3\bgi'); setcolor(GREEN); settextstyle(4,0,7); while(!kbhit()) { gettime(&t); hr=t.ti_hour; min=t.ti_min; sec=t.ti_sec; i++; display(100,100,hr); display(200,100,min); display(300,100,sec); sound(400); delay(30); nosound(); delay(930); cleardevice(); } getch(); } void display(int x,int y,int num) { char str[3]; itoa(num,str,10); settextstyle(4,0,7); outtextxy(180,100,':'); outtextxy(280,100,':'); outtextxy(x,y,str); rectangle(90,90,380,200); rectangle(70,70,400,220); outtextxy(90,250,'Digital Clock'); } </pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs.webp" alt="Computer Graphics Programs"> <hr> <h3>Write a Program to make puzzle game.</h3> <pre> #include #include #include #include #include int a[5][5]; int t[16]={0,4,11,12,7,1,15,5,13,6,10,3,2,14,8,9}; int test[16]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; struct pos { int h,v; } p[4][4]; int row=4,col=4; void game(int); //MOVEMENT void rec(); //DRAWING RECTANGLE void pri(); //PRINTING NUMBERS INITIALLY int getkey(); // TO TRACE KEY PRESSED inline void space() { cout<<''; } inline void print(int r,int c) { cout< <a[r][c]; init(); to store co-ordinates int stop(); stoping criterion gopr(int,int); print number in game main() gm="DETECT,gd=DETECT;" initgraph(&gm,&gd,'c:\turboc3\bgi'); d,cr="1;" rec(); print(); while(cr!="16)" d="getkey();" game(d); cr="stop();" settextstyle(10,0,1); outtextxy(400,300,'you are winner!'); getch(); rec() setcolor(5); for(int i="0;i<200;i+=50)" j="0;j<240;j+=60)" rectangle(j+100,i+100,j+50,i+60); pri() k="1;" x="0,i=6;x<4;x++,i+=3)" y="0,j=10;y<4&&k<16;y++,j+=7,k++)" gotoxy(p[x][y].h,p[x][y].v); <a[x][y]; getkey() union regs i,o; while(!kbhit()); i.h.ah="0;" int86(22,&i,&o); return(o.h.ah); init() p[x][y].h="j;" p[x][y].v="i;" a[x][y]="t[k++];" game(int s) r="row-1;" c="col-1;" if(s="=77" &&c!="0)" right col--; a[r][c]="a[r][c-1];" gopr(r,c-1); space(); gopr(r,c); print(r,c-1); && r!="0)" down row--; gopr(r-1,c); print(r-1,c); c!="3)" left col++; gopr(r,c+1); print(r,c+1); &&r!="3)" up row++; gopr(r+1,c); print(r+1,c); gopr(int x, y) stop() if(a[x][y]="=test[k])" d++; k++; return d; < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-8.webp" alt="Computer Graphics Programs"> <br> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-9.webp" alt="Computer Graphics Programs"> <hr> <h3>Write a Program to implement bouncing ball using sine wave form.</h3> <pre> #include #include #define HEIGHT getmaxy() #define WIDTH getmaxx() #define GROUND 450 #define MAXHEIGHT 420 void main() { int x,y=0,t=MAXHEIGHT,c=1; int gd=DETECT,gm; initgraph(&gd,&gm,'C:\T urboC3\BGI'); for(x=40;xMAXHEIGHT-20) { c=0; t=t-40; } if(y=40) y=y+(c? 15:-15); cleardevice(); //Exit upon keypress if(kbhit()) break; } getch(); } </pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-10.webp" alt="Computer Graphics Programs"> <hr> <h3>Write a Program to implement Bouncing Ball in vertical direction.</h3> <pre> #include #include #include #include int main() { int gd = DETECT, gm; int i, x, y, flag=0; initgraph(&gd, &gm, 'C:\TC\BGI'); x = getmaxx()/2; y = 30; while (!kbhit()) { if(y >= getmaxy()-30 || y <= 30) flag="!flag;" * draws the gray board setcolor(red); setfillstyle(solid_fill, red); circle(x, y, 30); floodfill(x, delay(50); cleardevice(); if(flag) { y="y" + 5; } else - getch(); closegraph(); return 0; < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-2.webp" alt="Computer Graphics Programs"> <hr> <h3>Write a program of Translation, Rotation, and Scaling using Composite Transformation.</h3> <pre> #include #include #include #include int x1,y1,x2,y2,x3,y3,a,b; void draw(); void rotate(); int main(void) { int gd=DETECT,gm; initgraph(&gd,&gm,'C:\TC\BGI'); printf('Enter first co-ordinate value for triangle:'); scanf('%d%d',&x1,&y1); printf('Enter second co-ordinatevalues for triangle:'); scanf('%d%d',&x2,&y2); printf('Enter third co-ordinate valuesfor triangle:'); scanf('%d%d',&x3,&y3); draw(); getch(); rotate(); getch(); return 0; } void draw() { line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x3,y3,x1,y1); } void rotate() { int a1,a2,a3,b1,b2,b3; float angle; printf('Enter the rotation angle co-ordinates:'); scanf('%f',&angle); cleardevice(); angle=(angle*3.14)/180; a1=a+(x1-a)*cos(angle)-(y1-b)*sin(angle); b1=b+(x1-a)*sin(angle)+(y2-b)*cos(angle); a2=a+(x2-a)*cos(angle)-(y1-b)*sin(angle); b2=b+(x2-a)*sin(angle)+(y2-b)*cos(angle); a3=a+(x3-a)*cos(angle)-(y1-b)*sin(angle); b3=b+(x3-a)*sin(angle)+(y2-b)*cos(angle); printf('ROTATION'); printf('
Changed coordinates
'); printf('%d %d
%d %d
%d %d',a1,b1,a2,b2,a3,b3); line(a1,b1,a2,b2); line(a2,b2,a3,b3); line(a3,b3,a1,b1); } </pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-11.webp" alt="Computer Graphics Programs"> <br> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-12.webp" alt="Computer Graphics Programs"> <hr> <h3>Write a program to draw a Circle using midpoint implementation Method.</h3> <pre> #include #include void drawcircle(int x0, int y0, int radius) { int x = radius; int y = 0; int err = 0; while (x >= y) { putpixel(x0 + x, y0 + y, 7); putpixel(x0 + y, y0 + x, 7); putpixel(x0 - y, y0 + x, 7); putpixel(x0 - x, y0 + y, 7); putpixel(x0 - x, y0 - y, 7); putpixel(x0 - y, y0 - x, 7); putpixel(x0 + y, y0 - x, 7); putpixel(x0 + x, y0 - y, 7); if (err 0) { x -= 1; err -= 2*x + 1; } } } void main() { int gdriver=DETECT, gmode, error, x, y, r; initgraph(&gdriver, &gmode, 'c:\turboc3\bgi'); printf('Enter radius of circle: '); scanf('%d', &r); printf('Enter co-ordinates of center(x and y): '); scanf('%d%d', &x, &y); drawcircle(x, y, r); getch(); } </pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-13.webp" alt="Computer Graphics Programs"> <hr> <h3>Write a program to draw Bezier curve.</h3> <pre> #include #include #include #include void bezier (int x[4], int y[4]) { int gd = DETECT, gm; int i; double t; initgraph (&gd, &gm, 'C:\tc\bgi'); for (t = 0.0; t <1.0; 3 t +="0.0005)" { double xt="pow" (1-t, 3) * x[0] pow 2) x[1] (t, (1-t) x[2] x[3]; yt="pow" y[0] y[1] y[2] y[3]; putpixel (xt, yt, white); } for (i="0;" i<4; i++) (x[i], y[i], yellow); getch(); closegraph(); return; void main() int x[4], y[4]; i; printf ('enter the x- and y-coordinates of four control points.
'); scanf ('%d%d', &x[i], &y[i]); bezier (x, y); < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-14.webp" alt="Computer Graphics Programs"> <br> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-15.webp" alt="Computer Graphics Programs"> <hr> <h3>Program to rotate a rectangle about its midpoint</h3> <pre> #include #include #include #include #include #define pi 3.14 class arc { float x[10], y[10], theta, h1, k1,r[0][10],ang; float p[10][10],p1[10][10],x1[10], y1[10],xm,yx; int i, k, j, n; public: void get(); void cal(); void map(); void graph(); void plot(); void plot1(); }; void arc::get() { cout '<>ang; n = 4; cout<<'
enter'; for (i="0;" i<n; i++) { cout<<'
x[' <i<<'] and y['< <i<>x[i]>>y[i]; } h1=x [0] + (([1]-x[0])/2); k1=y[0]+(([3]-y[0])/2); cout<<'
2 midpoint of rectangle is--'<<h1<<' '<<k1; theta="(ang*pi)/180?;" r[0][0]="cos" (theta); r[0][1]="-sin?(theta);" r[0][2]="(-h1*cos(theta))+(k1*sin(theta)+h1;" r[1][0]="sin" r [1][1]="cos" [1][2]="(-h1*sin(theta))+(-k1*cos(theta)+k1;" r[2][0]="0;" r[2][1]="0;" r[2][2]="1;" } void arc ::cal() { for(i="0;i<n;i++)" p[0][i]="x[i];" p[1][i]="y[i];" p[2][i]="1;" for(j="0;j<n;j++)" p1[i][j]="0;" for (k="0;k<3;k++)" p1[i][j]+="r[i][k]*p[k][j];" x1[i]="p1[0][i];" y1[i]="p[1][i];" arc::map() int gd="DETECT," gm; initgraph (&gd, &gm, ''); errorcode="graphresult();" *an error occurred * if (errorcode!="grOK)" printf('graphics error: %s
',grapherrormsg (errorcode)); printf('press and key to halt: '); getch(); exit(1); terminate with an code arc::graph() xm="getmaxx()/2;" ym="getmaxy()/2;" line (xm, 0, xm, ym); (0, ym, :: plot 1() (i="0;i<n-1;i++)" circle (x1[i]+xm, (-y1[i]+ym), 2); x[i+1] (-y1[0]+ym)); (x1[n-1] +xm, (-y1[n-1]+ym), x1[0]+xm, () (x[i]+xm, (-y[i]+ym), (-y[i+1]+ym)); (x[n-1]+xm, (-y[n-1]+ym),2); (-y[n-1]+ym), x[0]+xm, (-y[0]+ym)); main class a; clrscr(); a.map(); a.graph(); a.get(); a.cal(); a.plot(); a.plot1(); < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-3.webp" alt="Computer Graphics Programs"> <hr> <h3>Program to clip a line using Liang Barsky Method </h3> <pre> #include #include #include #include #include class liang { float x1,x2,y1,y2,u1,u2,dx,dy,xm,ym; float xmin, ymin, xmax, ymax,p[4],q[4],r[5],r[5]; int gd, gm, test, s, k; public: void clipliang(); void get(); void map(); void graph(); }; void liang :: map() { gd=DETECT; initgraph (&gd, &gm, ''); int errorcode = graphresult(); /*an error occurred */ if (errorcode!=grOK) { printf('Graphics error: %s
',grapherrormsg (errorcode)); printf('Press and key to halt: '); getch(); exit(1); /* terminate with an error code */ } } void liang::graph() { xm=getmaxx()/2; ym=getmaxy()/2; line (xm, 0, xm, 2 * ym); line (0, ym, 2 * xm, ym); } void liang :: get() { cout<>xmin>>ymin>>xmax>>ymax>>; rectangle (xmin+xm,-ymin+ym,xmax+xm, -ymax+ym); // rectangle (320+xwmin, 240-ywmax, 320+xwmax, 240-ywmin); cout<>x1>>y1>>x2>>y2; line (x1+xm,-y1+ym, x2+xm,-y2+ym); getch(); } // line (x1, y1, x2, y2); void liang :: clipliang() { float x=0, y=1; dx=x2-x1; dy=y2-y1; p[0]=-dx; p[1]=dx; p[2]=-dy; p[3]=dy; q[0]=x1-xmin; q[1]=xmax-x1; q[2]=y1-ymin; q[3]=ymax-y1; for (k=0;k<4;k++) { if (p[k]="=0&&q[k]<0)" cout<<'
outside'; getch(); exit(0); } (p[k]<0) r[k]="q[k]/p[k];" (r1[k]u2) complete outside window'; void main () liang1; clrscr(); l.map(); l.graph(); l.get(); l.clipliang(); < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-4.webp" alt="Computer Graphics Programs"> <hr> <h3>Program to implement Standard Perspective Projection in 3-Dimensions.</h3> <pre> #include #include #include #include #include #include #define pi 3.14 class per { float x[10], y[10], z [10], d; float x1 [10], y1[10], xm, ym, z1 [10]; int i, k, j, n; public: void get(); void perpro(); void map(); void graph(); void plot1(); } void per::get() { cout<>n; cout<<'
enter'; for (i="0;i<n;i++)" { cout<<'
x['?i<<'], y['< <i<<'] and z ['< <i<>x[i]>>y[i]>>z[i]; } cout<>d; } void per:: perpro() { for(i=0;i<n;i++) 2 { x1[i]="(d*x[i])/(z[i]+d);" y1[i]="(d*y[i])/(z[i]+d);" z1[i]="0;" } cout<< '
enter projection, vertices of polygon are--'; for(i="0;i<n;i++)" cout<<'x?['< <i<<']="<<x1[i]<< " '; cout<<'y?['< cout<<'z?['< cout<<'
'; void per :: map() int gd="DETECT;" initgraph (&gd, &gm, ''); errorcode="graphresult();" *an error occurred * if (errorcode!="grOK)" printf('graphics error: %s
',grapherrormsg (errorcode)); printf('press and key to halt: '); getch(); exit(1); terminate with an code per::graph() xm="get" maxx() 2; ym="get" maxy() line (xm,0, xm, ym); (0, ym, per::plot1() line(x1[i]*100+xm,(-y1[i]*100+ym),x1[i+1]*100+xm,(-y1[i+1]*100+ym)); line(x1[n-1]*100+xm,(-y1[n-1]*100+ym),x1[0]*100+xm,(-y1[0]*100+ym)); main() class a; clrscr(); a.map(); a.graph(); a.get(); a.perpro(); a.plot1(); < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-5.webp" alt="Computer Graphics Programs"> <hr> <h3>Program to implement Parallel Projection in 3-Dimensions.</h3> <pre> #include #include #include #include #include #include #define pi 3.14 class per { float x[10], y[10], z [10], d, m[10], q1[10], r1[10], m1[10]; float x1 [10], y1[10], xm, ym, z1 [10], cav[4] [4], p[10] [10]; float p1[10] [10]; float a, b, c, ang, theta, f; int i, k, j, n; public: void get(); void par_v(); void par_vnr0(); void map(); void graph(); void project(float m[],float q[], float r[]); }; void per:: perproject(float m[], float q[], float r[]) { cout<>d; cout<< '
AFTER PROJECTION, VERTICES OF POLYGON ARE--'; for(i=0;i<n;i++) { cout<<'x?['< <i<<']="<<x1[i]<< " '; cout<<'y?['< cout<<'z?['< cout<<'
'; } getch(); clearviewport(); graph(); for(i="0;i<n-1;i++)" line(m[i]*100+xm,(-q[i]*100+ym),m[i+1]*100+xm,(-n1[i+1]*100+ym)); line(m[n-1]*100+xm,(-q[n-1]*100+ym),m[0]*100+xm,(-q[0]*100+ym)); void per::get() cout<>n; cout<<'
enter'; for (i="0;i<n;i++)" { cout<<'
x['?i<<'], y['< <i<<'] and z ['< <i<>x[i]>>y[i]>>z[i]; } cout<>d; int ch=100; do { cout<orthographic projection'; cout<oblique cout< exit';>ch; switch (ch) { case1: int ch1=100; do { cout<for xy viewplane'; cout<for yz cout'
3->for zx viewplane'; cout<exit'; cout<>ch1; switch(ch1) { case1: x1[i]=x[i]; y1[i]=y[i]; z1[i]=0; project(x1,y1,z1); break; case2: x1[i]=0; y1[i]=0; z1[i]=z[i]; project(x1,y1,z1); break; case3: x1[i]=x[i]; y1[i]=0; z1[i]=z[i]; project(x1,y1,z1); break; } } while (ch!=0) break; case2: ch1=100; do { cout<on xy plane'; cout<cavalier'; cout<exit'; cout<>ch1; switch(ch1) { case1: par_v(); break; case2: par_vnr0(); break(); } while(ch!=0); break; } while(ch!=0) } void per::par_v() { cout<>a>>b>>c; for(i=0; i<n;i++) { x1[i]="x[i]-((a*z[i]/c);" y1[i]="y[i]-((b*z[i]/c);" project(x1,y1, z1); } void per::par_vnr0() cout<>f; cout<>angle; theta= ((pi/180)* ang); for(i=0;i<4;i++) 2 { for(j="0;j<4;j++)" if(i="=j)" cav[i] [j]="1;" else } cav[0] [2]="f*cos(theta);" cav[1][2]="f*sin" (theta); cav[2][2]="0;" for(i="0;i<n;i++)" p[0][i]="x[i];" p[1][i]="y[i];" p[2][i]="z[i];" p[3][i]="1;" p1[i][j]="0;" for(k="0;k<4;k++)" p1[i][j]+="cav[i][k]*p[k][j];" x1[i]="p1[0][i];" y1[i]="p1[1][i];" z1[i]="p1[i];" project(x1,y1,z1); void per :: map() int gd="DETECT;" initgraph (&gd, &gm, ''); errorcode="graphresult();" *an error occurred * if (errorcode!="grOK)" printf('graphics error: %s
',grapherrormsg (errorcode)); printf('press and key to halt: '); getch(); exit(1); terminate with an code per::graph() xm="get" maxx() 2; ym="get" maxy() line (xm,0, xm, ym); (0, ym, main() class a; clrscr(); a.map(); a.get(); < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-6.webp" alt="Computer Graphics Programs"> <hr> <h3>Program to draw Sine, Cosine and Tangent Curves</h3> <pre> #include #include #include #include #include #include #define pi 3.14 class arc { float x, y, a, xm, ym; int i, ch; public: void get(); void map(); void sine(); void cosine(); void tangent(); }; void arc::get() { cout<>y; cout<<'
menu is---------'; cout< sine curve'; cout<cosine tangent exit';>ch; switch(ch) { case1: sine(); break(); case2: cosine(); case3: tangent(); case4: exit(0); } } void arc::sine() { cleardevice(); xm=getmaxx()/2; ym=getmaxy()/2; line(xm, 0, xm, 2*ym); line(0, ym, 2 *xm, ym); outtextxy(0, ym, 'X-AXIS'); outtextxy(xm, 0, 'Y-AXIS'); for(x=-300;x<=300;x=x+0.5) 2 { y="a*sin((pi*x)/180);" putpixel(x+(320),-y+240,red); } void arc::cosine() cleardevice(); xm="getmaxx()/2;" ym="getmaxy()/2;" line(xm, 0, xm, 2*ym); line(0, ym, *xm, ym); outtextxy(0, 'x-axis'); outtextxy(xm, 'y-axis'); for(x="-300;x<=300;x=x+0.5)" arc :: map() int gd="DETECT,gm;" initgraph (&gd, &gm, ''); errorcode="graphresult();" *an error occurred * if (errorcode!="grOK)" printf('graphics error: %s
',grapherrormsg (errorcode)); printf('press and key to halt: '); getch(); exit(1); terminate with an code main() class a; clrscr(); a.map(); a.get(); < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-7.webp" alt="Computer Graphics Programs"> <hr></=300;x=x+0.5)></'
></pre></4;i++)></n;i++)></on></exit';></for></orthographic></'
></n;i++)></pre></n;i++)></'
></pre></4;k++)></pre></'
></'
></pre></1.0;></pre></=></pre></'';></pre></=(getmaxx()-r-10))></pre></maxx-140;i++)></pre></=8;i++)> Saída
Escreva um programa para fazer um carro colorido em movimento usando funções integradas.
#include #include int main() { intgd=DETECT,gm, i, maxx, cy; initgraph(&gd, &gm, 'C:\TC\BGI'); setbkcolor(WHITE); setcolor(RED); maxx = getmaxx(); cy = getmaxy()/2; for(i=0;i<maxx-140;i++) { cleardevice(); line(0+i,cy-20, 0+i, cy+15); line(0+i, cy-20, 25+i, cy-20); line(25+i, 40+i, cy-70); line(40+i, cy-70, 100+i, line(100+i, 115+i, line(115+i, 140+i, cy+15, 18+i, circle(28+i, 10); line(38+i, 102+i, circle(112+i, cy+15,10); line(122+i, cy+15 ,140+i,cy+15); line(140+i, rectangle(50+i, cy-62, 90+i, cy-30); setfillstyle(1,blue); floodfill(5+i, cy-15, red); setfillstyle(1, lightblue); floodfill(52+i, cy-60, delay(10); } getch(); closegraph(); return 0; < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-4.webp" alt="Computer Graphics Programs"> <br> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-5.webp" alt="Computer Graphics Programs"> <hr> <h3>Write a Program to print your name in Hindi script on console output in C.</h3> <pre> #include #include #include #include void main() { int gd=DETECT,gm; initgraph(&gd,&gm,'c:\tc\bgi'); setbkcolor(9); line(100,100,370,100); line(120,100,120,170); arc(143,100,0,180,23); line(165,100,165,155); arc(150,155,100,0,15); line(180,100,180,170); circle(210,140,10); line(210,130,250,130); circle(280,140,10); line(280,130,330,130); line(330,100,330,170); line(345,100,345,170); ellipse(337,100,0,180,9,18); getch(); } </pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-6.webp" alt="Computer Graphics Programs"> <hr> <h3>Write a Program control a ball using arrow keys.</h3> <pre> #include #include void main() { intgd=DETECT,gm,x,y,r=40; charch; initgraph(&gd,&gm,'C:/TURBOC3/BGI'); setbkcolor(3); x=getmaxx()/2; y=getmaxy()/2; setfillstyle(1,RED); circle(x,y,r); floodfill(x,y,getmaxcolor()); while((ch=getch())!=13) { switch(ch) { case 75: if(x>=r+1) { cleardevice(); circle(x-=10,y,r); floodfill(x,y,getmaxcolor()); } break; case 72: if(y>=r+1) { cleardevice(); circle(x,y-=10,r); floodfill(x,y,getmaxcolor()); } break; case 77: if(x<=(getmaxx()-r-10)) { cleardevice(); circle(x+="10,y,r);" floodfill(x,y,getmaxcolor()); } break; case 80: if(y<="(getmaxy()-r-10))" circle(x,y+="10,r);" getch(); < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-7.webp" alt="Computer Graphics Programs"> <hr> <h3>Write a Program to implement Digital Clock.</h3> <pre> #include #include #include #include struct time t; void display(int,int,int); void main() { int i=0,gd=DETECT,gm,hr,min,sec; clrscr(); initgraph(&gd,&gm,'c:\turboc3\bgi'); setcolor(GREEN); settextstyle(4,0,7); while(!kbhit()) { gettime(&t); hr=t.ti_hour; min=t.ti_min; sec=t.ti_sec; i++; display(100,100,hr); display(200,100,min); display(300,100,sec); sound(400); delay(30); nosound(); delay(930); cleardevice(); } getch(); } void display(int x,int y,int num) { char str[3]; itoa(num,str,10); settextstyle(4,0,7); outtextxy(180,100,':'); outtextxy(280,100,':'); outtextxy(x,y,str); rectangle(90,90,380,200); rectangle(70,70,400,220); outtextxy(90,250,'Digital Clock'); } </pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs.webp" alt="Computer Graphics Programs"> <hr> <h3>Write a Program to make puzzle game.</h3> <pre> #include #include #include #include #include int a[5][5]; int t[16]={0,4,11,12,7,1,15,5,13,6,10,3,2,14,8,9}; int test[16]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; struct pos { int h,v; } p[4][4]; int row=4,col=4; void game(int); //MOVEMENT void rec(); //DRAWING RECTANGLE void pri(); //PRINTING NUMBERS INITIALLY int getkey(); // TO TRACE KEY PRESSED inline void space() { cout<<\'\'; } inline void print(int r,int c) { cout< <a[r][c]; init(); to store co-ordinates int stop(); stoping criterion gopr(int,int); print number in game main() gm="DETECT,gd=DETECT;" initgraph(&gm,&gd,\'c:\turboc3\bgi\'); d,cr="1;" rec(); print(); while(cr!="16)" d="getkey();" game(d); cr="stop();" settextstyle(10,0,1); outtextxy(400,300,\'you are winner!\'); getch(); rec() setcolor(5); for(int i="0;i<200;i+=50)" j="0;j<240;j+=60)" rectangle(j+100,i+100,j+50,i+60); pri() k="1;" x="0,i=6;x<4;x++,i+=3)" y="0,j=10;y<4&&k<16;y++,j+=7,k++)" gotoxy(p[x][y].h,p[x][y].v); <a[x][y]; getkey() union regs i,o; while(!kbhit()); i.h.ah="0;" int86(22,&i,&o); return(o.h.ah); init() p[x][y].h="j;" p[x][y].v="i;" a[x][y]="t[k++];" game(int s) r="row-1;" c="col-1;" if(s="=77" &&c!="0)" right col--; a[r][c]="a[r][c-1];" gopr(r,c-1); space(); gopr(r,c); print(r,c-1); && r!="0)" down row--; gopr(r-1,c); print(r-1,c); c!="3)" left col++; gopr(r,c+1); print(r,c+1); &&r!="3)" up row++; gopr(r+1,c); print(r+1,c); gopr(int x, y) stop() if(a[x][y]="=test[k])" d++; k++; return d; < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-8.webp" alt="Computer Graphics Programs"> <br> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-9.webp" alt="Computer Graphics Programs"> <hr> <h3>Write a Program to implement bouncing ball using sine wave form.</h3> <pre> #include #include #define HEIGHT getmaxy() #define WIDTH getmaxx() #define GROUND 450 #define MAXHEIGHT 420 void main() { int x,y=0,t=MAXHEIGHT,c=1; int gd=DETECT,gm; initgraph(&gd,&gm,'C:\T urboC3\BGI'); for(x=40;xMAXHEIGHT-20) { c=0; t=t-40; } if(y=40) y=y+(c? 15:-15); cleardevice(); //Exit upon keypress if(kbhit()) break; } getch(); } </pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-10.webp" alt="Computer Graphics Programs"> <hr> <h3>Write a Program to implement Bouncing Ball in vertical direction.</h3> <pre> #include #include #include #include int main() { int gd = DETECT, gm; int i, x, y, flag=0; initgraph(&gd, &gm, 'C:\TC\BGI'); x = getmaxx()/2; y = 30; while (!kbhit()) { if(y >= getmaxy()-30 || y <= 30) flag="!flag;" * draws the gray board setcolor(red); setfillstyle(solid_fill, red); circle(x, y, 30); floodfill(x, delay(50); cleardevice(); if(flag) { y="y" + 5; } else - getch(); closegraph(); return 0; < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-2.webp" alt="Computer Graphics Programs"> <hr> <h3>Write a program of Translation, Rotation, and Scaling using Composite Transformation.</h3> <pre> #include #include #include #include int x1,y1,x2,y2,x3,y3,a,b; void draw(); void rotate(); int main(void) { int gd=DETECT,gm; initgraph(&gd,&gm,'C:\TC\BGI'); printf('Enter first co-ordinate value for triangle:'); scanf('%d%d',&x1,&y1); printf('Enter second co-ordinatevalues for triangle:'); scanf('%d%d',&x2,&y2); printf('Enter third co-ordinate valuesfor triangle:'); scanf('%d%d',&x3,&y3); draw(); getch(); rotate(); getch(); return 0; } void draw() { line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x3,y3,x1,y1); } void rotate() { int a1,a2,a3,b1,b2,b3; float angle; printf('Enter the rotation angle co-ordinates:'); scanf('%f',&angle); cleardevice(); angle=(angle*3.14)/180; a1=a+(x1-a)*cos(angle)-(y1-b)*sin(angle); b1=b+(x1-a)*sin(angle)+(y2-b)*cos(angle); a2=a+(x2-a)*cos(angle)-(y1-b)*sin(angle); b2=b+(x2-a)*sin(angle)+(y2-b)*cos(angle); a3=a+(x3-a)*cos(angle)-(y1-b)*sin(angle); b3=b+(x3-a)*sin(angle)+(y2-b)*cos(angle); printf('ROTATION'); printf('
Changed coordinates
'); printf('%d %d
%d %d
%d %d',a1,b1,a2,b2,a3,b3); line(a1,b1,a2,b2); line(a2,b2,a3,b3); line(a3,b3,a1,b1); } </pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-11.webp" alt="Computer Graphics Programs"> <br> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-12.webp" alt="Computer Graphics Programs"> <hr> <h3>Write a program to draw a Circle using midpoint implementation Method.</h3> <pre> #include #include void drawcircle(int x0, int y0, int radius) { int x = radius; int y = 0; int err = 0; while (x >= y) { putpixel(x0 + x, y0 + y, 7); putpixel(x0 + y, y0 + x, 7); putpixel(x0 - y, y0 + x, 7); putpixel(x0 - x, y0 + y, 7); putpixel(x0 - x, y0 - y, 7); putpixel(x0 - y, y0 - x, 7); putpixel(x0 + y, y0 - x, 7); putpixel(x0 + x, y0 - y, 7); if (err 0) { x -= 1; err -= 2*x + 1; } } } void main() { int gdriver=DETECT, gmode, error, x, y, r; initgraph(&gdriver, &gmode, 'c:\turboc3\bgi'); printf('Enter radius of circle: '); scanf('%d', &r); printf('Enter co-ordinates of center(x and y): '); scanf('%d%d', &x, &y); drawcircle(x, y, r); getch(); } </pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-13.webp" alt="Computer Graphics Programs"> <hr> <h3>Write a program to draw Bezier curve.</h3> <pre> #include #include #include #include void bezier (int x[4], int y[4]) { int gd = DETECT, gm; int i; double t; initgraph (&gd, &gm, 'C:\tc\bgi'); for (t = 0.0; t <1.0; 3 t +="0.0005)" { double xt="pow" (1-t, 3) * x[0] pow 2) x[1] (t, (1-t) x[2] x[3]; yt="pow" y[0] y[1] y[2] y[3]; putpixel (xt, yt, white); } for (i="0;" i<4; i++) (x[i], y[i], yellow); getch(); closegraph(); return; void main() int x[4], y[4]; i; printf (\'enter the x- and y-coordinates of four control points.
\'); scanf (\'%d%d\', &x[i], &y[i]); bezier (x, y); < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-14.webp" alt="Computer Graphics Programs"> <br> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-15.webp" alt="Computer Graphics Programs"> <hr> <h3>Program to rotate a rectangle about its midpoint</h3> <pre> #include #include #include #include #include #define pi 3.14 class arc { float x[10], y[10], theta, h1, k1,r[0][10],ang; float p[10][10],p1[10][10],x1[10], y1[10],xm,yx; int i, k, j, n; public: void get(); void cal(); void map(); void graph(); void plot(); void plot1(); }; void arc::get() { cout '<>ang; n = 4; cout<<\'
enter\'; for (i="0;" i<n; i++) { cout<<\'
x[\' <i<<\'] and y[\'< <i<>x[i]>>y[i]; } h1=x [0] + (([1]-x[0])/2); k1=y[0]+(([3]-y[0])/2); cout<<\'
2 midpoint of rectangle is--\'<<h1<<\' \'<<k1; theta="(ang*pi)/180?;" r[0][0]="cos" (theta); r[0][1]="-sin?(theta);" r[0][2]="(-h1*cos(theta))+(k1*sin(theta)+h1;" r[1][0]="sin" r [1][1]="cos" [1][2]="(-h1*sin(theta))+(-k1*cos(theta)+k1;" r[2][0]="0;" r[2][1]="0;" r[2][2]="1;" } void arc ::cal() { for(i="0;i<n;i++)" p[0][i]="x[i];" p[1][i]="y[i];" p[2][i]="1;" for(j="0;j<n;j++)" p1[i][j]="0;" for (k="0;k<3;k++)" p1[i][j]+="r[i][k]*p[k][j];" x1[i]="p1[0][i];" y1[i]="p[1][i];" arc::map() int gd="DETECT," gm; initgraph (&gd, &gm, \'\'); errorcode="graphresult();" *an error occurred * if (errorcode!="grOK)" printf(\'graphics error: %s
\',grapherrormsg (errorcode)); printf(\'press and key to halt: \'); getch(); exit(1); terminate with an code arc::graph() xm="getmaxx()/2;" ym="getmaxy()/2;" line (xm, 0, xm, ym); (0, ym, :: plot 1() (i="0;i<n-1;i++)" circle (x1[i]+xm, (-y1[i]+ym), 2); x[i+1] (-y1[0]+ym)); (x1[n-1] +xm, (-y1[n-1]+ym), x1[0]+xm, () (x[i]+xm, (-y[i]+ym), (-y[i+1]+ym)); (x[n-1]+xm, (-y[n-1]+ym),2); (-y[n-1]+ym), x[0]+xm, (-y[0]+ym)); main class a; clrscr(); a.map(); a.graph(); a.get(); a.cal(); a.plot(); a.plot1(); < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-3.webp" alt="Computer Graphics Programs"> <hr> <h3>Program to clip a line using Liang Barsky Method </h3> <pre> #include #include #include #include #include class liang { float x1,x2,y1,y2,u1,u2,dx,dy,xm,ym; float xmin, ymin, xmax, ymax,p[4],q[4],r[5],r[5]; int gd, gm, test, s, k; public: void clipliang(); void get(); void map(); void graph(); }; void liang :: map() { gd=DETECT; initgraph (&gd, &gm, ''); int errorcode = graphresult(); /*an error occurred */ if (errorcode!=grOK) { printf('Graphics error: %s
',grapherrormsg (errorcode)); printf('Press and key to halt: '); getch(); exit(1); /* terminate with an error code */ } } void liang::graph() { xm=getmaxx()/2; ym=getmaxy()/2; line (xm, 0, xm, 2 * ym); line (0, ym, 2 * xm, ym); } void liang :: get() { cout<>xmin>>ymin>>xmax>>ymax>>; rectangle (xmin+xm,-ymin+ym,xmax+xm, -ymax+ym); // rectangle (320+xwmin, 240-ywmax, 320+xwmax, 240-ywmin); cout<>x1>>y1>>x2>>y2; line (x1+xm,-y1+ym, x2+xm,-y2+ym); getch(); } // line (x1, y1, x2, y2); void liang :: clipliang() { float x=0, y=1; dx=x2-x1; dy=y2-y1; p[0]=-dx; p[1]=dx; p[2]=-dy; p[3]=dy; q[0]=x1-xmin; q[1]=xmax-x1; q[2]=y1-ymin; q[3]=ymax-y1; for (k=0;k<4;k++) { if (p[k]="=0&&q[k]<0)" cout<<\'
outside\'; getch(); exit(0); } (p[k]<0) r[k]="q[k]/p[k];" (r1[k]u2) complete outside window\'; void main () liang1; clrscr(); l.map(); l.graph(); l.get(); l.clipliang(); < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-4.webp" alt="Computer Graphics Programs"> <hr> <h3>Program to implement Standard Perspective Projection in 3-Dimensions.</h3> <pre> #include #include #include #include #include #include #define pi 3.14 class per { float x[10], y[10], z [10], d; float x1 [10], y1[10], xm, ym, z1 [10]; int i, k, j, n; public: void get(); void perpro(); void map(); void graph(); void plot1(); } void per::get() { cout<>n; cout<<\'
enter\'; for (i="0;i<n;i++)" { cout<<\'
x[\'?i<<\'], y[\'< <i<<\'] and z [\'< <i<>x[i]>>y[i]>>z[i]; } cout<>d; } void per:: perpro() { for(i=0;i<n;i++) 2 { x1[i]="(d*x[i])/(z[i]+d);" y1[i]="(d*y[i])/(z[i]+d);" z1[i]="0;" } cout<< \'
enter projection, vertices of polygon are--\'; for(i="0;i<n;i++)" cout<<\'x?[\'< <i<<\']="<<x1[i]<< " \'; cout<<\'y?[\'< cout<<\'z?[\'< cout<<\'
\'; void per :: map() int gd="DETECT;" initgraph (&gd, &gm, \'\'); errorcode="graphresult();" *an error occurred * if (errorcode!="grOK)" printf(\'graphics error: %s
\',grapherrormsg (errorcode)); printf(\'press and key to halt: \'); getch(); exit(1); terminate with an code per::graph() xm="get" maxx() 2; ym="get" maxy() line (xm,0, xm, ym); (0, ym, per::plot1() line(x1[i]*100+xm,(-y1[i]*100+ym),x1[i+1]*100+xm,(-y1[i+1]*100+ym)); line(x1[n-1]*100+xm,(-y1[n-1]*100+ym),x1[0]*100+xm,(-y1[0]*100+ym)); main() class a; clrscr(); a.map(); a.graph(); a.get(); a.perpro(); a.plot1(); < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-5.webp" alt="Computer Graphics Programs"> <hr> <h3>Program to implement Parallel Projection in 3-Dimensions.</h3> <pre> #include #include #include #include #include #include #define pi 3.14 class per { float x[10], y[10], z [10], d, m[10], q1[10], r1[10], m1[10]; float x1 [10], y1[10], xm, ym, z1 [10], cav[4] [4], p[10] [10]; float p1[10] [10]; float a, b, c, ang, theta, f; int i, k, j, n; public: void get(); void par_v(); void par_vnr0(); void map(); void graph(); void project(float m[],float q[], float r[]); }; void per:: perproject(float m[], float q[], float r[]) { cout<>d; cout<< '
AFTER PROJECTION, VERTICES OF POLYGON ARE--'; for(i=0;i<n;i++) { cout<<\'x?[\'< <i<<\']="<<x1[i]<< " \'; cout<<\'y?[\'< cout<<\'z?[\'< cout<<\'
\'; } getch(); clearviewport(); graph(); for(i="0;i<n-1;i++)" line(m[i]*100+xm,(-q[i]*100+ym),m[i+1]*100+xm,(-n1[i+1]*100+ym)); line(m[n-1]*100+xm,(-q[n-1]*100+ym),m[0]*100+xm,(-q[0]*100+ym)); void per::get() cout<>n; cout<<\'
enter\'; for (i="0;i<n;i++)" { cout<<\'
x[\'?i<<\'], y[\'< <i<<\'] and z [\'< <i<>x[i]>>y[i]>>z[i]; } cout<>d; int ch=100; do { cout<orthographic projection\'; cout<oblique cout< exit\';>ch; switch (ch) { case1: int ch1=100; do { cout<for xy viewplane\'; cout<for yz cout\'
3->for zx viewplane'; cout<exit\'; cout<>ch1; switch(ch1) { case1: x1[i]=x[i]; y1[i]=y[i]; z1[i]=0; project(x1,y1,z1); break; case2: x1[i]=0; y1[i]=0; z1[i]=z[i]; project(x1,y1,z1); break; case3: x1[i]=x[i]; y1[i]=0; z1[i]=z[i]; project(x1,y1,z1); break; } } while (ch!=0) break; case2: ch1=100; do { cout<on xy plane\'; cout<cavalier\'; cout<exit\'; cout<>ch1; switch(ch1) { case1: par_v(); break; case2: par_vnr0(); break(); } while(ch!=0); break; } while(ch!=0) } void per::par_v() { cout<>a>>b>>c; for(i=0; i<n;i++) { x1[i]="x[i]-((a*z[i]/c);" y1[i]="y[i]-((b*z[i]/c);" project(x1,y1, z1); } void per::par_vnr0() cout<>f; cout<>angle; theta= ((pi/180)* ang); for(i=0;i<4;i++) 2 { for(j="0;j<4;j++)" if(i="=j)" cav[i] [j]="1;" else } cav[0] [2]="f*cos(theta);" cav[1][2]="f*sin" (theta); cav[2][2]="0;" for(i="0;i<n;i++)" p[0][i]="x[i];" p[1][i]="y[i];" p[2][i]="z[i];" p[3][i]="1;" p1[i][j]="0;" for(k="0;k<4;k++)" p1[i][j]+="cav[i][k]*p[k][j];" x1[i]="p1[0][i];" y1[i]="p1[1][i];" z1[i]="p1[i];" project(x1,y1,z1); void per :: map() int gd="DETECT;" initgraph (&gd, &gm, \'\'); errorcode="graphresult();" *an error occurred * if (errorcode!="grOK)" printf(\'graphics error: %s
\',grapherrormsg (errorcode)); printf(\'press and key to halt: \'); getch(); exit(1); terminate with an code per::graph() xm="get" maxx() 2; ym="get" maxy() line (xm,0, xm, ym); (0, ym, main() class a; clrscr(); a.map(); a.get(); < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-6.webp" alt="Computer Graphics Programs"> <hr> <h3>Program to draw Sine, Cosine and Tangent Curves</h3> <pre> #include #include #include #include #include #include #define pi 3.14 class arc { float x, y, a, xm, ym; int i, ch; public: void get(); void map(); void sine(); void cosine(); void tangent(); }; void arc::get() { cout<>y; cout<<\'
menu is---------\'; cout< sine curve\'; cout<cosine tangent exit\';>ch; switch(ch) { case1: sine(); break(); case2: cosine(); case3: tangent(); case4: exit(0); } } void arc::sine() { cleardevice(); xm=getmaxx()/2; ym=getmaxy()/2; line(xm, 0, xm, 2*ym); line(0, ym, 2 *xm, ym); outtextxy(0, ym, 'X-AXIS'); outtextxy(xm, 0, 'Y-AXIS'); for(x=-300;x<=300;x=x+0.5) 2 { y="a*sin((pi*x)/180);" putpixel(x+(320),-y+240,red); } void arc::cosine() cleardevice(); xm="getmaxx()/2;" ym="getmaxy()/2;" line(xm, 0, xm, 2*ym); line(0, ym, *xm, ym); outtextxy(0, \'x-axis\'); outtextxy(xm, \'y-axis\'); for(x="-300;x<=300;x=x+0.5)" arc :: map() int gd="DETECT,gm;" initgraph (&gd, &gm, \'\'); errorcode="graphresult();" *an error occurred * if (errorcode!="grOK)" printf(\'graphics error: %s
\',grapherrormsg (errorcode)); printf(\'press and key to halt: \'); getch(); exit(1); terminate with an code main() class a; clrscr(); a.map(); a.get(); < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-7.webp" alt="Computer Graphics Programs"> <hr></=300;x=x+0.5)></\'
></pre></4;i++)></n;i++)></on></exit\';></for></orthographic></\'
></n;i++)></pre></n;i++)></\'
></pre></4;k++)></pre></\'
></\'
></pre></1.0;></pre></=></pre></\'\';></pre></=(getmaxx()-r-10))></pre></maxx-140;i++)> Saída
Escreva um programa para controlar uma bola usando as teclas de seta.
#include #include void main() { intgd=DETECT,gm,x,y,r=40; charch; initgraph(&gd,&gm,'C:/TURBOC3/BGI'); setbkcolor(3); x=getmaxx()/2; y=getmaxy()/2; setfillstyle(1,RED); circle(x,y,r); floodfill(x,y,getmaxcolor()); while((ch=getch())!=13) { switch(ch) { case 75: if(x>=r+1) { cleardevice(); circle(x-=10,y,r); floodfill(x,y,getmaxcolor()); } break; case 72: if(y>=r+1) { cleardevice(); circle(x,y-=10,r); floodfill(x,y,getmaxcolor()); } break; case 77: if(x<=(getmaxx()-r-10)) { cleardevice(); circle(x+="10,y,r);" floodfill(x,y,getmaxcolor()); } break; case 80: if(y<="(getmaxy()-r-10))" circle(x,y+="10,r);" getch(); < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-7.webp" alt="Computer Graphics Programs"> <hr> <h3>Write a Program to implement Digital Clock.</h3> <pre> #include #include #include #include struct time t; void display(int,int,int); void main() { int i=0,gd=DETECT,gm,hr,min,sec; clrscr(); initgraph(&gd,&gm,'c:\turboc3\bgi'); setcolor(GREEN); settextstyle(4,0,7); while(!kbhit()) { gettime(&t); hr=t.ti_hour; min=t.ti_min; sec=t.ti_sec; i++; display(100,100,hr); display(200,100,min); display(300,100,sec); sound(400); delay(30); nosound(); delay(930); cleardevice(); } getch(); } void display(int x,int y,int num) { char str[3]; itoa(num,str,10); settextstyle(4,0,7); outtextxy(180,100,':'); outtextxy(280,100,':'); outtextxy(x,y,str); rectangle(90,90,380,200); rectangle(70,70,400,220); outtextxy(90,250,'Digital Clock'); } </pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs.webp" alt="Computer Graphics Programs"> <hr> <h3>Write a Program to make puzzle game.</h3> <pre> #include #include #include #include #include int a[5][5]; int t[16]={0,4,11,12,7,1,15,5,13,6,10,3,2,14,8,9}; int test[16]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; struct pos { int h,v; } p[4][4]; int row=4,col=4; void game(int); //MOVEMENT void rec(); //DRAWING RECTANGLE void pri(); //PRINTING NUMBERS INITIALLY int getkey(); // TO TRACE KEY PRESSED inline void space() { cout<<\'\'; } inline void print(int r,int c) { cout< <a[r][c]; init(); to store co-ordinates int stop(); stoping criterion gopr(int,int); print number in game main() gm="DETECT,gd=DETECT;" initgraph(&gm,&gd,\'c:\turboc3\bgi\'); d,cr="1;" rec(); print(); while(cr!="16)" d="getkey();" game(d); cr="stop();" settextstyle(10,0,1); outtextxy(400,300,\'you are winner!\'); getch(); rec() setcolor(5); for(int i="0;i<200;i+=50)" j="0;j<240;j+=60)" rectangle(j+100,i+100,j+50,i+60); pri() k="1;" x="0,i=6;x<4;x++,i+=3)" y="0,j=10;y<4&&k<16;y++,j+=7,k++)" gotoxy(p[x][y].h,p[x][y].v); <a[x][y]; getkey() union regs i,o; while(!kbhit()); i.h.ah="0;" int86(22,&i,&o); return(o.h.ah); init() p[x][y].h="j;" p[x][y].v="i;" a[x][y]="t[k++];" game(int s) r="row-1;" c="col-1;" if(s="=77" &&c!="0)" right col--; a[r][c]="a[r][c-1];" gopr(r,c-1); space(); gopr(r,c); print(r,c-1); && r!="0)" down row--; gopr(r-1,c); print(r-1,c); c!="3)" left col++; gopr(r,c+1); print(r,c+1); &&r!="3)" up row++; gopr(r+1,c); print(r+1,c); gopr(int x, y) stop() if(a[x][y]="=test[k])" d++; k++; return d; < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-8.webp" alt="Computer Graphics Programs"> <br> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-9.webp" alt="Computer Graphics Programs"> <hr> <h3>Write a Program to implement bouncing ball using sine wave form.</h3> <pre> #include #include #define HEIGHT getmaxy() #define WIDTH getmaxx() #define GROUND 450 #define MAXHEIGHT 420 void main() { int x,y=0,t=MAXHEIGHT,c=1; int gd=DETECT,gm; initgraph(&gd,&gm,'C:\T urboC3\BGI'); for(x=40;xMAXHEIGHT-20) { c=0; t=t-40; } if(y=40) y=y+(c? 15:-15); cleardevice(); //Exit upon keypress if(kbhit()) break; } getch(); } </pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-10.webp" alt="Computer Graphics Programs"> <hr> <h3>Write a Program to implement Bouncing Ball in vertical direction.</h3> <pre> #include #include #include #include int main() { int gd = DETECT, gm; int i, x, y, flag=0; initgraph(&gd, &gm, 'C:\TC\BGI'); x = getmaxx()/2; y = 30; while (!kbhit()) { if(y >= getmaxy()-30 || y <= 30) flag="!flag;" * draws the gray board setcolor(red); setfillstyle(solid_fill, red); circle(x, y, 30); floodfill(x, delay(50); cleardevice(); if(flag) { y="y" + 5; } else - getch(); closegraph(); return 0; < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-2.webp" alt="Computer Graphics Programs"> <hr> <h3>Write a program of Translation, Rotation, and Scaling using Composite Transformation.</h3> <pre> #include #include #include #include int x1,y1,x2,y2,x3,y3,a,b; void draw(); void rotate(); int main(void) { int gd=DETECT,gm; initgraph(&gd,&gm,'C:\TC\BGI'); printf('Enter first co-ordinate value for triangle:'); scanf('%d%d',&x1,&y1); printf('Enter second co-ordinatevalues for triangle:'); scanf('%d%d',&x2,&y2); printf('Enter third co-ordinate valuesfor triangle:'); scanf('%d%d',&x3,&y3); draw(); getch(); rotate(); getch(); return 0; } void draw() { line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x3,y3,x1,y1); } void rotate() { int a1,a2,a3,b1,b2,b3; float angle; printf('Enter the rotation angle co-ordinates:'); scanf('%f',&angle); cleardevice(); angle=(angle*3.14)/180; a1=a+(x1-a)*cos(angle)-(y1-b)*sin(angle); b1=b+(x1-a)*sin(angle)+(y2-b)*cos(angle); a2=a+(x2-a)*cos(angle)-(y1-b)*sin(angle); b2=b+(x2-a)*sin(angle)+(y2-b)*cos(angle); a3=a+(x3-a)*cos(angle)-(y1-b)*sin(angle); b3=b+(x3-a)*sin(angle)+(y2-b)*cos(angle); printf('ROTATION'); printf('
Changed coordinates
'); printf('%d %d
%d %d
%d %d',a1,b1,a2,b2,a3,b3); line(a1,b1,a2,b2); line(a2,b2,a3,b3); line(a3,b3,a1,b1); } </pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-11.webp" alt="Computer Graphics Programs"> <br> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-12.webp" alt="Computer Graphics Programs"> <hr> <h3>Write a program to draw a Circle using midpoint implementation Method.</h3> <pre> #include #include void drawcircle(int x0, int y0, int radius) { int x = radius; int y = 0; int err = 0; while (x >= y) { putpixel(x0 + x, y0 + y, 7); putpixel(x0 + y, y0 + x, 7); putpixel(x0 - y, y0 + x, 7); putpixel(x0 - x, y0 + y, 7); putpixel(x0 - x, y0 - y, 7); putpixel(x0 - y, y0 - x, 7); putpixel(x0 + y, y0 - x, 7); putpixel(x0 + x, y0 - y, 7); if (err 0) { x -= 1; err -= 2*x + 1; } } } void main() { int gdriver=DETECT, gmode, error, x, y, r; initgraph(&gdriver, &gmode, 'c:\turboc3\bgi'); printf('Enter radius of circle: '); scanf('%d', &r); printf('Enter co-ordinates of center(x and y): '); scanf('%d%d', &x, &y); drawcircle(x, y, r); getch(); } </pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-13.webp" alt="Computer Graphics Programs"> <hr> <h3>Write a program to draw Bezier curve.</h3> <pre> #include #include #include #include void bezier (int x[4], int y[4]) { int gd = DETECT, gm; int i; double t; initgraph (&gd, &gm, 'C:\tc\bgi'); for (t = 0.0; t <1.0; 3 t +="0.0005)" { double xt="pow" (1-t, 3) * x[0] pow 2) x[1] (t, (1-t) x[2] x[3]; yt="pow" y[0] y[1] y[2] y[3]; putpixel (xt, yt, white); } for (i="0;" i<4; i++) (x[i], y[i], yellow); getch(); closegraph(); return; void main() int x[4], y[4]; i; printf (\'enter the x- and y-coordinates of four control points.
\'); scanf (\'%d%d\', &x[i], &y[i]); bezier (x, y); < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-14.webp" alt="Computer Graphics Programs"> <br> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-15.webp" alt="Computer Graphics Programs"> <hr> <h3>Program to rotate a rectangle about its midpoint</h3> <pre> #include #include #include #include #include #define pi 3.14 class arc { float x[10], y[10], theta, h1, k1,r[0][10],ang; float p[10][10],p1[10][10],x1[10], y1[10],xm,yx; int i, k, j, n; public: void get(); void cal(); void map(); void graph(); void plot(); void plot1(); }; void arc::get() { cout '<>ang; n = 4; cout<<\'
enter\'; for (i="0;" i<n; i++) { cout<<\'
x[\' <i<<\'] and y[\'< <i<>x[i]>>y[i]; } h1=x [0] + (([1]-x[0])/2); k1=y[0]+(([3]-y[0])/2); cout<<\'
2 midpoint of rectangle is--\'<<h1<<\' \'<<k1; theta="(ang*pi)/180?;" r[0][0]="cos" (theta); r[0][1]="-sin?(theta);" r[0][2]="(-h1*cos(theta))+(k1*sin(theta)+h1;" r[1][0]="sin" r [1][1]="cos" [1][2]="(-h1*sin(theta))+(-k1*cos(theta)+k1;" r[2][0]="0;" r[2][1]="0;" r[2][2]="1;" } void arc ::cal() { for(i="0;i<n;i++)" p[0][i]="x[i];" p[1][i]="y[i];" p[2][i]="1;" for(j="0;j<n;j++)" p1[i][j]="0;" for (k="0;k<3;k++)" p1[i][j]+="r[i][k]*p[k][j];" x1[i]="p1[0][i];" y1[i]="p[1][i];" arc::map() int gd="DETECT," gm; initgraph (&gd, &gm, \'\'); errorcode="graphresult();" *an error occurred * if (errorcode!="grOK)" printf(\'graphics error: %s
\',grapherrormsg (errorcode)); printf(\'press and key to halt: \'); getch(); exit(1); terminate with an code arc::graph() xm="getmaxx()/2;" ym="getmaxy()/2;" line (xm, 0, xm, ym); (0, ym, :: plot 1() (i="0;i<n-1;i++)" circle (x1[i]+xm, (-y1[i]+ym), 2); x[i+1] (-y1[0]+ym)); (x1[n-1] +xm, (-y1[n-1]+ym), x1[0]+xm, () (x[i]+xm, (-y[i]+ym), (-y[i+1]+ym)); (x[n-1]+xm, (-y[n-1]+ym),2); (-y[n-1]+ym), x[0]+xm, (-y[0]+ym)); main class a; clrscr(); a.map(); a.graph(); a.get(); a.cal(); a.plot(); a.plot1(); < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-3.webp" alt="Computer Graphics Programs"> <hr> <h3>Program to clip a line using Liang Barsky Method </h3> <pre> #include #include #include #include #include class liang { float x1,x2,y1,y2,u1,u2,dx,dy,xm,ym; float xmin, ymin, xmax, ymax,p[4],q[4],r[5],r[5]; int gd, gm, test, s, k; public: void clipliang(); void get(); void map(); void graph(); }; void liang :: map() { gd=DETECT; initgraph (&gd, &gm, ''); int errorcode = graphresult(); /*an error occurred */ if (errorcode!=grOK) { printf('Graphics error: %s
',grapherrormsg (errorcode)); printf('Press and key to halt: '); getch(); exit(1); /* terminate with an error code */ } } void liang::graph() { xm=getmaxx()/2; ym=getmaxy()/2; line (xm, 0, xm, 2 * ym); line (0, ym, 2 * xm, ym); } void liang :: get() { cout<>xmin>>ymin>>xmax>>ymax>>; rectangle (xmin+xm,-ymin+ym,xmax+xm, -ymax+ym); // rectangle (320+xwmin, 240-ywmax, 320+xwmax, 240-ywmin); cout<>x1>>y1>>x2>>y2; line (x1+xm,-y1+ym, x2+xm,-y2+ym); getch(); } // line (x1, y1, x2, y2); void liang :: clipliang() { float x=0, y=1; dx=x2-x1; dy=y2-y1; p[0]=-dx; p[1]=dx; p[2]=-dy; p[3]=dy; q[0]=x1-xmin; q[1]=xmax-x1; q[2]=y1-ymin; q[3]=ymax-y1; for (k=0;k<4;k++) { if (p[k]="=0&&q[k]<0)" cout<<\'
outside\'; getch(); exit(0); } (p[k]<0) r[k]="q[k]/p[k];" (r1[k]u2) complete outside window\'; void main () liang1; clrscr(); l.map(); l.graph(); l.get(); l.clipliang(); < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-4.webp" alt="Computer Graphics Programs"> <hr> <h3>Program to implement Standard Perspective Projection in 3-Dimensions.</h3> <pre> #include #include #include #include #include #include #define pi 3.14 class per { float x[10], y[10], z [10], d; float x1 [10], y1[10], xm, ym, z1 [10]; int i, k, j, n; public: void get(); void perpro(); void map(); void graph(); void plot1(); } void per::get() { cout<>n; cout<<\'
enter\'; for (i="0;i<n;i++)" { cout<<\'
x[\'?i<<\'], y[\'< <i<<\'] and z [\'< <i<>x[i]>>y[i]>>z[i]; } cout<>d; } void per:: perpro() { for(i=0;i<n;i++) 2 { x1[i]="(d*x[i])/(z[i]+d);" y1[i]="(d*y[i])/(z[i]+d);" z1[i]="0;" } cout<< \'
enter projection, vertices of polygon are--\'; for(i="0;i<n;i++)" cout<<\'x?[\'< <i<<\']="<<x1[i]<< " \'; cout<<\'y?[\'< cout<<\'z?[\'< cout<<\'
\'; void per :: map() int gd="DETECT;" initgraph (&gd, &gm, \'\'); errorcode="graphresult();" *an error occurred * if (errorcode!="grOK)" printf(\'graphics error: %s
\',grapherrormsg (errorcode)); printf(\'press and key to halt: \'); getch(); exit(1); terminate with an code per::graph() xm="get" maxx() 2; ym="get" maxy() line (xm,0, xm, ym); (0, ym, per::plot1() line(x1[i]*100+xm,(-y1[i]*100+ym),x1[i+1]*100+xm,(-y1[i+1]*100+ym)); line(x1[n-1]*100+xm,(-y1[n-1]*100+ym),x1[0]*100+xm,(-y1[0]*100+ym)); main() class a; clrscr(); a.map(); a.graph(); a.get(); a.perpro(); a.plot1(); < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-5.webp" alt="Computer Graphics Programs"> <hr> <h3>Program to implement Parallel Projection in 3-Dimensions.</h3> <pre> #include #include #include #include #include #include #define pi 3.14 class per { float x[10], y[10], z [10], d, m[10], q1[10], r1[10], m1[10]; float x1 [10], y1[10], xm, ym, z1 [10], cav[4] [4], p[10] [10]; float p1[10] [10]; float a, b, c, ang, theta, f; int i, k, j, n; public: void get(); void par_v(); void par_vnr0(); void map(); void graph(); void project(float m[],float q[], float r[]); }; void per:: perproject(float m[], float q[], float r[]) { cout<>d; cout<< '
AFTER PROJECTION, VERTICES OF POLYGON ARE--'; for(i=0;i<n;i++) { cout<<\'x?[\'< <i<<\']="<<x1[i]<< " \'; cout<<\'y?[\'< cout<<\'z?[\'< cout<<\'
\'; } getch(); clearviewport(); graph(); for(i="0;i<n-1;i++)" line(m[i]*100+xm,(-q[i]*100+ym),m[i+1]*100+xm,(-n1[i+1]*100+ym)); line(m[n-1]*100+xm,(-q[n-1]*100+ym),m[0]*100+xm,(-q[0]*100+ym)); void per::get() cout<>n; cout<<\'
enter\'; for (i="0;i<n;i++)" { cout<<\'
x[\'?i<<\'], y[\'< <i<<\'] and z [\'< <i<>x[i]>>y[i]>>z[i]; } cout<>d; int ch=100; do { cout<orthographic projection\'; cout<oblique cout< exit\';>ch; switch (ch) { case1: int ch1=100; do { cout<for xy viewplane\'; cout<for yz cout\'
3->for zx viewplane'; cout<exit\'; cout<>ch1; switch(ch1) { case1: x1[i]=x[i]; y1[i]=y[i]; z1[i]=0; project(x1,y1,z1); break; case2: x1[i]=0; y1[i]=0; z1[i]=z[i]; project(x1,y1,z1); break; case3: x1[i]=x[i]; y1[i]=0; z1[i]=z[i]; project(x1,y1,z1); break; } } while (ch!=0) break; case2: ch1=100; do { cout<on xy plane\'; cout<cavalier\'; cout<exit\'; cout<>ch1; switch(ch1) { case1: par_v(); break; case2: par_vnr0(); break(); } while(ch!=0); break; } while(ch!=0) } void per::par_v() { cout<>a>>b>>c; for(i=0; i<n;i++) { x1[i]="x[i]-((a*z[i]/c);" y1[i]="y[i]-((b*z[i]/c);" project(x1,y1, z1); } void per::par_vnr0() cout<>f; cout<>angle; theta= ((pi/180)* ang); for(i=0;i<4;i++) 2 { for(j="0;j<4;j++)" if(i="=j)" cav[i] [j]="1;" else } cav[0] [2]="f*cos(theta);" cav[1][2]="f*sin" (theta); cav[2][2]="0;" for(i="0;i<n;i++)" p[0][i]="x[i];" p[1][i]="y[i];" p[2][i]="z[i];" p[3][i]="1;" p1[i][j]="0;" for(k="0;k<4;k++)" p1[i][j]+="cav[i][k]*p[k][j];" x1[i]="p1[0][i];" y1[i]="p1[1][i];" z1[i]="p1[i];" project(x1,y1,z1); void per :: map() int gd="DETECT;" initgraph (&gd, &gm, \'\'); errorcode="graphresult();" *an error occurred * if (errorcode!="grOK)" printf(\'graphics error: %s
\',grapherrormsg (errorcode)); printf(\'press and key to halt: \'); getch(); exit(1); terminate with an code per::graph() xm="get" maxx() 2; ym="get" maxy() line (xm,0, xm, ym); (0, ym, main() class a; clrscr(); a.map(); a.get(); < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-6.webp" alt="Computer Graphics Programs"> <hr> <h3>Program to draw Sine, Cosine and Tangent Curves</h3> <pre> #include #include #include #include #include #include #define pi 3.14 class arc { float x, y, a, xm, ym; int i, ch; public: void get(); void map(); void sine(); void cosine(); void tangent(); }; void arc::get() { cout<>y; cout<<\'
menu is---------\'; cout< sine curve\'; cout<cosine tangent exit\';>ch; switch(ch) { case1: sine(); break(); case2: cosine(); case3: tangent(); case4: exit(0); } } void arc::sine() { cleardevice(); xm=getmaxx()/2; ym=getmaxy()/2; line(xm, 0, xm, 2*ym); line(0, ym, 2 *xm, ym); outtextxy(0, ym, 'X-AXIS'); outtextxy(xm, 0, 'Y-AXIS'); for(x=-300;x<=300;x=x+0.5) 2 { y="a*sin((pi*x)/180);" putpixel(x+(320),-y+240,red); } void arc::cosine() cleardevice(); xm="getmaxx()/2;" ym="getmaxy()/2;" line(xm, 0, xm, 2*ym); line(0, ym, *xm, ym); outtextxy(0, \'x-axis\'); outtextxy(xm, \'y-axis\'); for(x="-300;x<=300;x=x+0.5)" arc :: map() int gd="DETECT,gm;" initgraph (&gd, &gm, \'\'); errorcode="graphresult();" *an error occurred * if (errorcode!="grOK)" printf(\'graphics error: %s
\',grapherrormsg (errorcode)); printf(\'press and key to halt: \'); getch(); exit(1); terminate with an code main() class a; clrscr(); a.map(); a.get(); < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-7.webp" alt="Computer Graphics Programs"> <hr></=300;x=x+0.5)></\'
></pre></4;i++)></n;i++)></on></exit\';></for></orthographic></\'
></n;i++)></pre></n;i++)></\'
></pre></4;k++)></pre></\'
></\'
></pre></1.0;></pre></=></pre></\'\';></pre></=(getmaxx()-r-10))> Saída
Escreva um programa para fazer um jogo de quebra-cabeça.
#include #include #include #include #include int a[5][5]; int t[16]={0,4,11,12,7,1,15,5,13,6,10,3,2,14,8,9}; int test[16]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; struct pos { int h,v; } p[4][4]; int row=4,col=4; void game(int); //MOVEMENT void rec(); //DRAWING RECTANGLE void pri(); //PRINTING NUMBERS INITIALLY int getkey(); // TO TRACE KEY PRESSED inline void space() { cout<<\\'\\'; } inline void print(int r,int c) { cout< <a[r][c]; init(); to store co-ordinates int stop(); stoping criterion gopr(int,int); print number in game main() gm="DETECT,gd=DETECT;" initgraph(&gm,&gd,\\'c:\turboc3\bgi\\'); d,cr="1;" rec(); print(); while(cr!="16)" d="getkey();" game(d); cr="stop();" settextstyle(10,0,1); outtextxy(400,300,\\'you are winner!\\'); getch(); rec() setcolor(5); for(int i="0;i<200;i+=50)" j="0;j<240;j+=60)" rectangle(j+100,i+100,j+50,i+60); pri() k="1;" x="0,i=6;x<4;x++,i+=3)" y="0,j=10;y<4&&k<16;y++,j+=7,k++)" gotoxy(p[x][y].h,p[x][y].v); <a[x][y]; getkey() union regs i,o; while(!kbhit()); i.h.ah="0;" int86(22,&i,&o); return(o.h.ah); init() p[x][y].h="j;" p[x][y].v="i;" a[x][y]="t[k++];" game(int s) r="row-1;" c="col-1;" if(s="=77" &&c!="0)" right col--; a[r][c]="a[r][c-1];" gopr(r,c-1); space(); gopr(r,c); print(r,c-1); && r!="0)" down row--; gopr(r-1,c); print(r-1,c); c!="3)" left col++; gopr(r,c+1); print(r,c+1); &&r!="3)" up row++; gopr(r+1,c); print(r+1,c); gopr(int x, y) stop() if(a[x][y]="=test[k])" d++; k++; return d; < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-8.webp" alt="Computer Graphics Programs"> <br> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-9.webp" alt="Computer Graphics Programs"> <hr> <h3>Write a Program to implement bouncing ball using sine wave form.</h3> <pre> #include #include #define HEIGHT getmaxy() #define WIDTH getmaxx() #define GROUND 450 #define MAXHEIGHT 420 void main() { int x,y=0,t=MAXHEIGHT,c=1; int gd=DETECT,gm; initgraph(&gd,&gm,'C:\T urboC3\BGI'); for(x=40;xMAXHEIGHT-20) { c=0; t=t-40; } if(y=40) y=y+(c? 15:-15); cleardevice(); //Exit upon keypress if(kbhit()) break; } getch(); } </pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-10.webp" alt="Computer Graphics Programs"> <hr> <h3>Write a Program to implement Bouncing Ball in vertical direction.</h3> <pre> #include #include #include #include int main() { int gd = DETECT, gm; int i, x, y, flag=0; initgraph(&gd, &gm, 'C:\TC\BGI'); x = getmaxx()/2; y = 30; while (!kbhit()) { if(y >= getmaxy()-30 || y <= 30) flag="!flag;" * draws the gray board setcolor(red); setfillstyle(solid_fill, red); circle(x, y, 30); floodfill(x, delay(50); cleardevice(); if(flag) { y="y" + 5; } else - getch(); closegraph(); return 0; < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-2.webp" alt="Computer Graphics Programs"> <hr> <h3>Write a program of Translation, Rotation, and Scaling using Composite Transformation.</h3> <pre> #include #include #include #include int x1,y1,x2,y2,x3,y3,a,b; void draw(); void rotate(); int main(void) { int gd=DETECT,gm; initgraph(&gd,&gm,'C:\TC\BGI'); printf('Enter first co-ordinate value for triangle:'); scanf('%d%d',&x1,&y1); printf('Enter second co-ordinatevalues for triangle:'); scanf('%d%d',&x2,&y2); printf('Enter third co-ordinate valuesfor triangle:'); scanf('%d%d',&x3,&y3); draw(); getch(); rotate(); getch(); return 0; } void draw() { line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x3,y3,x1,y1); } void rotate() { int a1,a2,a3,b1,b2,b3; float angle; printf('Enter the rotation angle co-ordinates:'); scanf('%f',&angle); cleardevice(); angle=(angle*3.14)/180; a1=a+(x1-a)*cos(angle)-(y1-b)*sin(angle); b1=b+(x1-a)*sin(angle)+(y2-b)*cos(angle); a2=a+(x2-a)*cos(angle)-(y1-b)*sin(angle); b2=b+(x2-a)*sin(angle)+(y2-b)*cos(angle); a3=a+(x3-a)*cos(angle)-(y1-b)*sin(angle); b3=b+(x3-a)*sin(angle)+(y2-b)*cos(angle); printf('ROTATION'); printf('
Changed coordinates
'); printf('%d %d
%d %d
%d %d',a1,b1,a2,b2,a3,b3); line(a1,b1,a2,b2); line(a2,b2,a3,b3); line(a3,b3,a1,b1); } </pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-11.webp" alt="Computer Graphics Programs"> <br> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-12.webp" alt="Computer Graphics Programs"> <hr> <h3>Write a program to draw a Circle using midpoint implementation Method.</h3> <pre> #include #include void drawcircle(int x0, int y0, int radius) { int x = radius; int y = 0; int err = 0; while (x >= y) { putpixel(x0 + x, y0 + y, 7); putpixel(x0 + y, y0 + x, 7); putpixel(x0 - y, y0 + x, 7); putpixel(x0 - x, y0 + y, 7); putpixel(x0 - x, y0 - y, 7); putpixel(x0 - y, y0 - x, 7); putpixel(x0 + y, y0 - x, 7); putpixel(x0 + x, y0 - y, 7); if (err 0) { x -= 1; err -= 2*x + 1; } } } void main() { int gdriver=DETECT, gmode, error, x, y, r; initgraph(&gdriver, &gmode, 'c:\turboc3\bgi'); printf('Enter radius of circle: '); scanf('%d', &r); printf('Enter co-ordinates of center(x and y): '); scanf('%d%d', &x, &y); drawcircle(x, y, r); getch(); } </pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-13.webp" alt="Computer Graphics Programs"> <hr> <h3>Write a program to draw Bezier curve.</h3> <pre> #include #include #include #include void bezier (int x[4], int y[4]) { int gd = DETECT, gm; int i; double t; initgraph (&gd, &gm, 'C:\tc\bgi'); for (t = 0.0; t <1.0; 3 t +="0.0005)" { double xt="pow" (1-t, 3) * x[0] pow 2) x[1] (t, (1-t) x[2] x[3]; yt="pow" y[0] y[1] y[2] y[3]; putpixel (xt, yt, white); } for (i="0;" i<4; i++) (x[i], y[i], yellow); getch(); closegraph(); return; void main() int x[4], y[4]; i; printf (\\'enter the x- and y-coordinates of four control points.
\\'); scanf (\\'%d%d\\', &x[i], &y[i]); bezier (x, y); < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-14.webp" alt="Computer Graphics Programs"> <br> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-15.webp" alt="Computer Graphics Programs"> <hr> <h3>Program to rotate a rectangle about its midpoint</h3> <pre> #include #include #include #include #include #define pi 3.14 class arc { float x[10], y[10], theta, h1, k1,r[0][10],ang; float p[10][10],p1[10][10],x1[10], y1[10],xm,yx; int i, k, j, n; public: void get(); void cal(); void map(); void graph(); void plot(); void plot1(); }; void arc::get() { cout '<>ang; n = 4; cout<<\\'
enter\\'; for (i="0;" i<n; i++) { cout<<\\'
x[\\' <i<<\\'] and y[\\'< <i<>x[i]>>y[i]; } h1=x [0] + (([1]-x[0])/2); k1=y[0]+(([3]-y[0])/2); cout<<\\'
2 midpoint of rectangle is--\\'<<h1<<\\' \\'<<k1; theta="(ang*pi)/180?;" r[0][0]="cos" (theta); r[0][1]="-sin?(theta);" r[0][2]="(-h1*cos(theta))+(k1*sin(theta)+h1;" r[1][0]="sin" r [1][1]="cos" [1][2]="(-h1*sin(theta))+(-k1*cos(theta)+k1;" r[2][0]="0;" r[2][1]="0;" r[2][2]="1;" } void arc ::cal() { for(i="0;i<n;i++)" p[0][i]="x[i];" p[1][i]="y[i];" p[2][i]="1;" for(j="0;j<n;j++)" p1[i][j]="0;" for (k="0;k<3;k++)" p1[i][j]+="r[i][k]*p[k][j];" x1[i]="p1[0][i];" y1[i]="p[1][i];" arc::map() int gd="DETECT," gm; initgraph (&gd, &gm, \\'\\'); errorcode="graphresult();" *an error occurred * if (errorcode!="grOK)" printf(\\'graphics error: %s
\\',grapherrormsg (errorcode)); printf(\\'press and key to halt: \\'); getch(); exit(1); terminate with an code arc::graph() xm="getmaxx()/2;" ym="getmaxy()/2;" line (xm, 0, xm, ym); (0, ym, :: plot 1() (i="0;i<n-1;i++)" circle (x1[i]+xm, (-y1[i]+ym), 2); x[i+1] (-y1[0]+ym)); (x1[n-1] +xm, (-y1[n-1]+ym), x1[0]+xm, () (x[i]+xm, (-y[i]+ym), (-y[i+1]+ym)); (x[n-1]+xm, (-y[n-1]+ym),2); (-y[n-1]+ym), x[0]+xm, (-y[0]+ym)); main class a; clrscr(); a.map(); a.graph(); a.get(); a.cal(); a.plot(); a.plot1(); < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-3.webp" alt="Computer Graphics Programs"> <hr> <h3>Program to clip a line using Liang Barsky Method </h3> <pre> #include #include #include #include #include class liang { float x1,x2,y1,y2,u1,u2,dx,dy,xm,ym; float xmin, ymin, xmax, ymax,p[4],q[4],r[5],r[5]; int gd, gm, test, s, k; public: void clipliang(); void get(); void map(); void graph(); }; void liang :: map() { gd=DETECT; initgraph (&gd, &gm, ''); int errorcode = graphresult(); /*an error occurred */ if (errorcode!=grOK) { printf('Graphics error: %s
',grapherrormsg (errorcode)); printf('Press and key to halt: '); getch(); exit(1); /* terminate with an error code */ } } void liang::graph() { xm=getmaxx()/2; ym=getmaxy()/2; line (xm, 0, xm, 2 * ym); line (0, ym, 2 * xm, ym); } void liang :: get() { cout<>xmin>>ymin>>xmax>>ymax>>; rectangle (xmin+xm,-ymin+ym,xmax+xm, -ymax+ym); // rectangle (320+xwmin, 240-ywmax, 320+xwmax, 240-ywmin); cout<>x1>>y1>>x2>>y2; line (x1+xm,-y1+ym, x2+xm,-y2+ym); getch(); } // line (x1, y1, x2, y2); void liang :: clipliang() { float x=0, y=1; dx=x2-x1; dy=y2-y1; p[0]=-dx; p[1]=dx; p[2]=-dy; p[3]=dy; q[0]=x1-xmin; q[1]=xmax-x1; q[2]=y1-ymin; q[3]=ymax-y1; for (k=0;k<4;k++) { if (p[k]="=0&&q[k]<0)" cout<<\\'
outside\\'; getch(); exit(0); } (p[k]<0) r[k]="q[k]/p[k];" (r1[k]u2) complete outside window\\'; void main () liang1; clrscr(); l.map(); l.graph(); l.get(); l.clipliang(); < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-4.webp" alt="Computer Graphics Programs"> <hr> <h3>Program to implement Standard Perspective Projection in 3-Dimensions.</h3> <pre> #include #include #include #include #include #include #define pi 3.14 class per { float x[10], y[10], z [10], d; float x1 [10], y1[10], xm, ym, z1 [10]; int i, k, j, n; public: void get(); void perpro(); void map(); void graph(); void plot1(); } void per::get() { cout<>n; cout<<\\'
enter\\'; for (i="0;i<n;i++)" { cout<<\\'
x[\\'?i<<\\'], y[\\'< <i<<\\'] and z [\\'< <i<>x[i]>>y[i]>>z[i]; } cout<>d; } void per:: perpro() { for(i=0;i<n;i++) 2 { x1[i]="(d*x[i])/(z[i]+d);" y1[i]="(d*y[i])/(z[i]+d);" z1[i]="0;" } cout<< \\'
enter projection, vertices of polygon are--\\'; for(i="0;i<n;i++)" cout<<\\'x?[\\'< <i<<\\']="<<x1[i]<< " \\'; cout<<\\'y?[\\'< cout<<\\'z?[\\'< cout<<\\'
\\'; void per :: map() int gd="DETECT;" initgraph (&gd, &gm, \\'\\'); errorcode="graphresult();" *an error occurred * if (errorcode!="grOK)" printf(\\'graphics error: %s
\\',grapherrormsg (errorcode)); printf(\\'press and key to halt: \\'); getch(); exit(1); terminate with an code per::graph() xm="get" maxx() 2; ym="get" maxy() line (xm,0, xm, ym); (0, ym, per::plot1() line(x1[i]*100+xm,(-y1[i]*100+ym),x1[i+1]*100+xm,(-y1[i+1]*100+ym)); line(x1[n-1]*100+xm,(-y1[n-1]*100+ym),x1[0]*100+xm,(-y1[0]*100+ym)); main() class a; clrscr(); a.map(); a.graph(); a.get(); a.perpro(); a.plot1(); < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-5.webp" alt="Computer Graphics Programs"> <hr> <h3>Program to implement Parallel Projection in 3-Dimensions.</h3> <pre> #include #include #include #include #include #include #define pi 3.14 class per { float x[10], y[10], z [10], d, m[10], q1[10], r1[10], m1[10]; float x1 [10], y1[10], xm, ym, z1 [10], cav[4] [4], p[10] [10]; float p1[10] [10]; float a, b, c, ang, theta, f; int i, k, j, n; public: void get(); void par_v(); void par_vnr0(); void map(); void graph(); void project(float m[],float q[], float r[]); }; void per:: perproject(float m[], float q[], float r[]) { cout<>d; cout<< '
AFTER PROJECTION, VERTICES OF POLYGON ARE--'; for(i=0;i<n;i++) { cout<<\\'x?[\\'< <i<<\\']="<<x1[i]<< " \\'; cout<<\\'y?[\\'< cout<<\\'z?[\\'< cout<<\\'
\\'; } getch(); clearviewport(); graph(); for(i="0;i<n-1;i++)" line(m[i]*100+xm,(-q[i]*100+ym),m[i+1]*100+xm,(-n1[i+1]*100+ym)); line(m[n-1]*100+xm,(-q[n-1]*100+ym),m[0]*100+xm,(-q[0]*100+ym)); void per::get() cout<>n; cout<<\\'
enter\\'; for (i="0;i<n;i++)" { cout<<\\'
x[\\'?i<<\\'], y[\\'< <i<<\\'] and z [\\'< <i<>x[i]>>y[i]>>z[i]; } cout<>d; int ch=100; do { cout<orthographic projection\\'; cout<oblique cout< exit\\';>ch; switch (ch) { case1: int ch1=100; do { cout<for xy viewplane\\'; cout<for yz cout\\'
3->for zx viewplane'; cout<exit\\'; cout<>ch1; switch(ch1) { case1: x1[i]=x[i]; y1[i]=y[i]; z1[i]=0; project(x1,y1,z1); break; case2: x1[i]=0; y1[i]=0; z1[i]=z[i]; project(x1,y1,z1); break; case3: x1[i]=x[i]; y1[i]=0; z1[i]=z[i]; project(x1,y1,z1); break; } } while (ch!=0) break; case2: ch1=100; do { cout<on xy plane\\'; cout<cavalier\\'; cout<exit\\'; cout<>ch1; switch(ch1) { case1: par_v(); break; case2: par_vnr0(); break(); } while(ch!=0); break; } while(ch!=0) } void per::par_v() { cout<>a>>b>>c; for(i=0; i<n;i++) { x1[i]="x[i]-((a*z[i]/c);" y1[i]="y[i]-((b*z[i]/c);" project(x1,y1, z1); } void per::par_vnr0() cout<>f; cout<>angle; theta= ((pi/180)* ang); for(i=0;i<4;i++) 2 { for(j="0;j<4;j++)" if(i="=j)" cav[i] [j]="1;" else } cav[0] [2]="f*cos(theta);" cav[1][2]="f*sin" (theta); cav[2][2]="0;" for(i="0;i<n;i++)" p[0][i]="x[i];" p[1][i]="y[i];" p[2][i]="z[i];" p[3][i]="1;" p1[i][j]="0;" for(k="0;k<4;k++)" p1[i][j]+="cav[i][k]*p[k][j];" x1[i]="p1[0][i];" y1[i]="p1[1][i];" z1[i]="p1[i];" project(x1,y1,z1); void per :: map() int gd="DETECT;" initgraph (&gd, &gm, \\'\\'); errorcode="graphresult();" *an error occurred * if (errorcode!="grOK)" printf(\\'graphics error: %s
\\',grapherrormsg (errorcode)); printf(\\'press and key to halt: \\'); getch(); exit(1); terminate with an code per::graph() xm="get" maxx() 2; ym="get" maxy() line (xm,0, xm, ym); (0, ym, main() class a; clrscr(); a.map(); a.get(); < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-6.webp" alt="Computer Graphics Programs"> <hr> <h3>Program to draw Sine, Cosine and Tangent Curves</h3> <pre> #include #include #include #include #include #include #define pi 3.14 class arc { float x, y, a, xm, ym; int i, ch; public: void get(); void map(); void sine(); void cosine(); void tangent(); }; void arc::get() { cout<>y; cout<<\\'
menu is---------\\'; cout< sine curve\\'; cout<cosine tangent exit\\';>ch; switch(ch) { case1: sine(); break(); case2: cosine(); case3: tangent(); case4: exit(0); } } void arc::sine() { cleardevice(); xm=getmaxx()/2; ym=getmaxy()/2; line(xm, 0, xm, 2*ym); line(0, ym, 2 *xm, ym); outtextxy(0, ym, 'X-AXIS'); outtextxy(xm, 0, 'Y-AXIS'); for(x=-300;x<=300;x=x+0.5) 2 { y="a*sin((pi*x)/180);" putpixel(x+(320),-y+240,red); } void arc::cosine() cleardevice(); xm="getmaxx()/2;" ym="getmaxy()/2;" line(xm, 0, xm, 2*ym); line(0, ym, *xm, ym); outtextxy(0, \\'x-axis\\'); outtextxy(xm, \\'y-axis\\'); for(x="-300;x<=300;x=x+0.5)" arc :: map() int gd="DETECT,gm;" initgraph (&gd, &gm, \\'\\'); errorcode="graphresult();" *an error occurred * if (errorcode!="grOK)" printf(\\'graphics error: %s
\\',grapherrormsg (errorcode)); printf(\\'press and key to halt: \\'); getch(); exit(1); terminate with an code main() class a; clrscr(); a.map(); a.get(); < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-7.webp" alt="Computer Graphics Programs"> <hr></=300;x=x+0.5)></\\'
></pre></4;i++)></n;i++)></on></exit\\';></for></orthographic></\\'
></n;i++)></pre></n;i++)></\\'
></pre></4;k++)></pre></\\'
></\\'
></pre></1.0;></pre></=></pre></\\'\\';> Saída
Escreva um programa para implementar Bouncing Ball na direção vertical.
#include #include #include #include int main() { int gd = DETECT, gm; int i, x, y, flag=0; initgraph(&gd, &gm, 'C:\TC\BGI'); x = getmaxx()/2; y = 30; while (!kbhit()) { if(y >= getmaxy()-30 || y <= 30) flag="!flag;" * draws the gray board setcolor(red); setfillstyle(solid_fill, red); circle(x, y, 30); floodfill(x, delay(50); cleardevice(); if(flag) { y="y" + 5; } else - getch(); closegraph(); return 0; < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-2.webp" alt="Computer Graphics Programs"> <hr> <h3>Write a program of Translation, Rotation, and Scaling using Composite Transformation.</h3> <pre> #include #include #include #include int x1,y1,x2,y2,x3,y3,a,b; void draw(); void rotate(); int main(void) { int gd=DETECT,gm; initgraph(&gd,&gm,'C:\TC\BGI'); printf('Enter first co-ordinate value for triangle:'); scanf('%d%d',&x1,&y1); printf('Enter second co-ordinatevalues for triangle:'); scanf('%d%d',&x2,&y2); printf('Enter third co-ordinate valuesfor triangle:'); scanf('%d%d',&x3,&y3); draw(); getch(); rotate(); getch(); return 0; } void draw() { line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x3,y3,x1,y1); } void rotate() { int a1,a2,a3,b1,b2,b3; float angle; printf('Enter the rotation angle co-ordinates:'); scanf('%f',&angle); cleardevice(); angle=(angle*3.14)/180; a1=a+(x1-a)*cos(angle)-(y1-b)*sin(angle); b1=b+(x1-a)*sin(angle)+(y2-b)*cos(angle); a2=a+(x2-a)*cos(angle)-(y1-b)*sin(angle); b2=b+(x2-a)*sin(angle)+(y2-b)*cos(angle); a3=a+(x3-a)*cos(angle)-(y1-b)*sin(angle); b3=b+(x3-a)*sin(angle)+(y2-b)*cos(angle); printf('ROTATION'); printf('
Changed coordinates
'); printf('%d %d
%d %d
%d %d',a1,b1,a2,b2,a3,b3); line(a1,b1,a2,b2); line(a2,b2,a3,b3); line(a3,b3,a1,b1); } </pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-11.webp" alt="Computer Graphics Programs"> <br> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-12.webp" alt="Computer Graphics Programs"> <hr> <h3>Write a program to draw a Circle using midpoint implementation Method.</h3> <pre> #include #include void drawcircle(int x0, int y0, int radius) { int x = radius; int y = 0; int err = 0; while (x >= y) { putpixel(x0 + x, y0 + y, 7); putpixel(x0 + y, y0 + x, 7); putpixel(x0 - y, y0 + x, 7); putpixel(x0 - x, y0 + y, 7); putpixel(x0 - x, y0 - y, 7); putpixel(x0 - y, y0 - x, 7); putpixel(x0 + y, y0 - x, 7); putpixel(x0 + x, y0 - y, 7); if (err 0) { x -= 1; err -= 2*x + 1; } } } void main() { int gdriver=DETECT, gmode, error, x, y, r; initgraph(&gdriver, &gmode, 'c:\turboc3\bgi'); printf('Enter radius of circle: '); scanf('%d', &r); printf('Enter co-ordinates of center(x and y): '); scanf('%d%d', &x, &y); drawcircle(x, y, r); getch(); } </pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-13.webp" alt="Computer Graphics Programs"> <hr> <h3>Write a program to draw Bezier curve.</h3> <pre> #include #include #include #include void bezier (int x[4], int y[4]) { int gd = DETECT, gm; int i; double t; initgraph (&gd, &gm, 'C:\tc\bgi'); for (t = 0.0; t <1.0; 3 t +="0.0005)" { double xt="pow" (1-t, 3) * x[0] pow 2) x[1] (t, (1-t) x[2] x[3]; yt="pow" y[0] y[1] y[2] y[3]; putpixel (xt, yt, white); } for (i="0;" i<4; i++) (x[i], y[i], yellow); getch(); closegraph(); return; void main() int x[4], y[4]; i; printf (\\'enter the x- and y-coordinates of four control points.
\\'); scanf (\\'%d%d\\', &x[i], &y[i]); bezier (x, y); < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-14.webp" alt="Computer Graphics Programs"> <br> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-15.webp" alt="Computer Graphics Programs"> <hr> <h3>Program to rotate a rectangle about its midpoint</h3> <pre> #include #include #include #include #include #define pi 3.14 class arc { float x[10], y[10], theta, h1, k1,r[0][10],ang; float p[10][10],p1[10][10],x1[10], y1[10],xm,yx; int i, k, j, n; public: void get(); void cal(); void map(); void graph(); void plot(); void plot1(); }; void arc::get() { cout '<>ang; n = 4; cout<<\\'
enter\\'; for (i="0;" i<n; i++) { cout<<\\'
x[\\' <i<<\\'] and y[\\'< <i<>x[i]>>y[i]; } h1=x [0] + (([1]-x[0])/2); k1=y[0]+(([3]-y[0])/2); cout<<\\'
2 midpoint of rectangle is--\\'<<h1<<\\' \\'<<k1; theta="(ang*pi)/180?;" r[0][0]="cos" (theta); r[0][1]="-sin?(theta);" r[0][2]="(-h1*cos(theta))+(k1*sin(theta)+h1;" r[1][0]="sin" r [1][1]="cos" [1][2]="(-h1*sin(theta))+(-k1*cos(theta)+k1;" r[2][0]="0;" r[2][1]="0;" r[2][2]="1;" } void arc ::cal() { for(i="0;i<n;i++)" p[0][i]="x[i];" p[1][i]="y[i];" p[2][i]="1;" for(j="0;j<n;j++)" p1[i][j]="0;" for (k="0;k<3;k++)" p1[i][j]+="r[i][k]*p[k][j];" x1[i]="p1[0][i];" y1[i]="p[1][i];" arc::map() int gd="DETECT," gm; initgraph (&gd, &gm, \\'\\'); errorcode="graphresult();" *an error occurred * if (errorcode!="grOK)" printf(\\'graphics error: %s
\\',grapherrormsg (errorcode)); printf(\\'press and key to halt: \\'); getch(); exit(1); terminate with an code arc::graph() xm="getmaxx()/2;" ym="getmaxy()/2;" line (xm, 0, xm, ym); (0, ym, :: plot 1() (i="0;i<n-1;i++)" circle (x1[i]+xm, (-y1[i]+ym), 2); x[i+1] (-y1[0]+ym)); (x1[n-1] +xm, (-y1[n-1]+ym), x1[0]+xm, () (x[i]+xm, (-y[i]+ym), (-y[i+1]+ym)); (x[n-1]+xm, (-y[n-1]+ym),2); (-y[n-1]+ym), x[0]+xm, (-y[0]+ym)); main class a; clrscr(); a.map(); a.graph(); a.get(); a.cal(); a.plot(); a.plot1(); < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-3.webp" alt="Computer Graphics Programs"> <hr> <h3>Program to clip a line using Liang Barsky Method </h3> <pre> #include #include #include #include #include class liang { float x1,x2,y1,y2,u1,u2,dx,dy,xm,ym; float xmin, ymin, xmax, ymax,p[4],q[4],r[5],r[5]; int gd, gm, test, s, k; public: void clipliang(); void get(); void map(); void graph(); }; void liang :: map() { gd=DETECT; initgraph (&gd, &gm, ''); int errorcode = graphresult(); /*an error occurred */ if (errorcode!=grOK) { printf('Graphics error: %s
',grapherrormsg (errorcode)); printf('Press and key to halt: '); getch(); exit(1); /* terminate with an error code */ } } void liang::graph() { xm=getmaxx()/2; ym=getmaxy()/2; line (xm, 0, xm, 2 * ym); line (0, ym, 2 * xm, ym); } void liang :: get() { cout<>xmin>>ymin>>xmax>>ymax>>; rectangle (xmin+xm,-ymin+ym,xmax+xm, -ymax+ym); // rectangle (320+xwmin, 240-ywmax, 320+xwmax, 240-ywmin); cout<>x1>>y1>>x2>>y2; line (x1+xm,-y1+ym, x2+xm,-y2+ym); getch(); } // line (x1, y1, x2, y2); void liang :: clipliang() { float x=0, y=1; dx=x2-x1; dy=y2-y1; p[0]=-dx; p[1]=dx; p[2]=-dy; p[3]=dy; q[0]=x1-xmin; q[1]=xmax-x1; q[2]=y1-ymin; q[3]=ymax-y1; for (k=0;k<4;k++) { if (p[k]="=0&&q[k]<0)" cout<<\\'
outside\\'; getch(); exit(0); } (p[k]<0) r[k]="q[k]/p[k];" (r1[k]u2) complete outside window\\'; void main () liang1; clrscr(); l.map(); l.graph(); l.get(); l.clipliang(); < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-4.webp" alt="Computer Graphics Programs"> <hr> <h3>Program to implement Standard Perspective Projection in 3-Dimensions.</h3> <pre> #include #include #include #include #include #include #define pi 3.14 class per { float x[10], y[10], z [10], d; float x1 [10], y1[10], xm, ym, z1 [10]; int i, k, j, n; public: void get(); void perpro(); void map(); void graph(); void plot1(); } void per::get() { cout<>n; cout<<\\'
enter\\'; for (i="0;i<n;i++)" { cout<<\\'
x[\\'?i<<\\'], y[\\'< <i<<\\'] and z [\\'< <i<>x[i]>>y[i]>>z[i]; } cout<>d; } void per:: perpro() { for(i=0;i<n;i++) 2 { x1[i]="(d*x[i])/(z[i]+d);" y1[i]="(d*y[i])/(z[i]+d);" z1[i]="0;" } cout<< \\'
enter projection, vertices of polygon are--\\'; for(i="0;i<n;i++)" cout<<\\'x?[\\'< <i<<\\']="<<x1[i]<< " \\'; cout<<\\'y?[\\'< cout<<\\'z?[\\'< cout<<\\'
\\'; void per :: map() int gd="DETECT;" initgraph (&gd, &gm, \\'\\'); errorcode="graphresult();" *an error occurred * if (errorcode!="grOK)" printf(\\'graphics error: %s
\\',grapherrormsg (errorcode)); printf(\\'press and key to halt: \\'); getch(); exit(1); terminate with an code per::graph() xm="get" maxx() 2; ym="get" maxy() line (xm,0, xm, ym); (0, ym, per::plot1() line(x1[i]*100+xm,(-y1[i]*100+ym),x1[i+1]*100+xm,(-y1[i+1]*100+ym)); line(x1[n-1]*100+xm,(-y1[n-1]*100+ym),x1[0]*100+xm,(-y1[0]*100+ym)); main() class a; clrscr(); a.map(); a.graph(); a.get(); a.perpro(); a.plot1(); < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-5.webp" alt="Computer Graphics Programs"> <hr> <h3>Program to implement Parallel Projection in 3-Dimensions.</h3> <pre> #include #include #include #include #include #include #define pi 3.14 class per { float x[10], y[10], z [10], d, m[10], q1[10], r1[10], m1[10]; float x1 [10], y1[10], xm, ym, z1 [10], cav[4] [4], p[10] [10]; float p1[10] [10]; float a, b, c, ang, theta, f; int i, k, j, n; public: void get(); void par_v(); void par_vnr0(); void map(); void graph(); void project(float m[],float q[], float r[]); }; void per:: perproject(float m[], float q[], float r[]) { cout<>d; cout<< '
AFTER PROJECTION, VERTICES OF POLYGON ARE--'; for(i=0;i<n;i++) { cout<<\\'x?[\\'< <i<<\\']="<<x1[i]<< " \\'; cout<<\\'y?[\\'< cout<<\\'z?[\\'< cout<<\\'
\\'; } getch(); clearviewport(); graph(); for(i="0;i<n-1;i++)" line(m[i]*100+xm,(-q[i]*100+ym),m[i+1]*100+xm,(-n1[i+1]*100+ym)); line(m[n-1]*100+xm,(-q[n-1]*100+ym),m[0]*100+xm,(-q[0]*100+ym)); void per::get() cout<>n; cout<<\\'
enter\\'; for (i="0;i<n;i++)" { cout<<\\'
x[\\'?i<<\\'], y[\\'< <i<<\\'] and z [\\'< <i<>x[i]>>y[i]>>z[i]; } cout<>d; int ch=100; do { cout<orthographic projection\\'; cout<oblique cout< exit\\';>ch; switch (ch) { case1: int ch1=100; do { cout<for xy viewplane\\'; cout<for yz cout\\'
3->for zx viewplane'; cout<exit\\'; cout<>ch1; switch(ch1) { case1: x1[i]=x[i]; y1[i]=y[i]; z1[i]=0; project(x1,y1,z1); break; case2: x1[i]=0; y1[i]=0; z1[i]=z[i]; project(x1,y1,z1); break; case3: x1[i]=x[i]; y1[i]=0; z1[i]=z[i]; project(x1,y1,z1); break; } } while (ch!=0) break; case2: ch1=100; do { cout<on xy plane\\'; cout<cavalier\\'; cout<exit\\'; cout<>ch1; switch(ch1) { case1: par_v(); break; case2: par_vnr0(); break(); } while(ch!=0); break; } while(ch!=0) } void per::par_v() { cout<>a>>b>>c; for(i=0; i<n;i++) { x1[i]="x[i]-((a*z[i]/c);" y1[i]="y[i]-((b*z[i]/c);" project(x1,y1, z1); } void per::par_vnr0() cout<>f; cout<>angle; theta= ((pi/180)* ang); for(i=0;i<4;i++) 2 { for(j="0;j<4;j++)" if(i="=j)" cav[i] [j]="1;" else } cav[0] [2]="f*cos(theta);" cav[1][2]="f*sin" (theta); cav[2][2]="0;" for(i="0;i<n;i++)" p[0][i]="x[i];" p[1][i]="y[i];" p[2][i]="z[i];" p[3][i]="1;" p1[i][j]="0;" for(k="0;k<4;k++)" p1[i][j]+="cav[i][k]*p[k][j];" x1[i]="p1[0][i];" y1[i]="p1[1][i];" z1[i]="p1[i];" project(x1,y1,z1); void per :: map() int gd="DETECT;" initgraph (&gd, &gm, \\'\\'); errorcode="graphresult();" *an error occurred * if (errorcode!="grOK)" printf(\\'graphics error: %s
\\',grapherrormsg (errorcode)); printf(\\'press and key to halt: \\'); getch(); exit(1); terminate with an code per::graph() xm="get" maxx() 2; ym="get" maxy() line (xm,0, xm, ym); (0, ym, main() class a; clrscr(); a.map(); a.get(); < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-6.webp" alt="Computer Graphics Programs"> <hr> <h3>Program to draw Sine, Cosine and Tangent Curves</h3> <pre> #include #include #include #include #include #include #define pi 3.14 class arc { float x, y, a, xm, ym; int i, ch; public: void get(); void map(); void sine(); void cosine(); void tangent(); }; void arc::get() { cout<>y; cout<<\\'
menu is---------\\'; cout< sine curve\\'; cout<cosine tangent exit\\';>ch; switch(ch) { case1: sine(); break(); case2: cosine(); case3: tangent(); case4: exit(0); } } void arc::sine() { cleardevice(); xm=getmaxx()/2; ym=getmaxy()/2; line(xm, 0, xm, 2*ym); line(0, ym, 2 *xm, ym); outtextxy(0, ym, 'X-AXIS'); outtextxy(xm, 0, 'Y-AXIS'); for(x=-300;x<=300;x=x+0.5) 2 { y="a*sin((pi*x)/180);" putpixel(x+(320),-y+240,red); } void arc::cosine() cleardevice(); xm="getmaxx()/2;" ym="getmaxy()/2;" line(xm, 0, xm, 2*ym); line(0, ym, *xm, ym); outtextxy(0, \\'x-axis\\'); outtextxy(xm, \\'y-axis\\'); for(x="-300;x<=300;x=x+0.5)" arc :: map() int gd="DETECT,gm;" initgraph (&gd, &gm, \\'\\'); errorcode="graphresult();" *an error occurred * if (errorcode!="grOK)" printf(\\'graphics error: %s
\\',grapherrormsg (errorcode)); printf(\\'press and key to halt: \\'); getch(); exit(1); terminate with an code main() class a; clrscr(); a.map(); a.get(); < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-7.webp" alt="Computer Graphics Programs"> <hr></=300;x=x+0.5)></\\'
></pre></4;i++)></n;i++)></on></exit\\';></for></orthographic></\\'
></n;i++)></pre></n;i++)></\\'
></pre></4;k++)></pre></\\'
></\\'
></pre></1.0;></pre></=> Saída
Escreva um programa para desenhar um círculo usando o método de implementação do ponto médio.
#include #include void drawcircle(int x0, int y0, int radius) { int x = radius; int y = 0; int err = 0; while (x >= y) { putpixel(x0 + x, y0 + y, 7); putpixel(x0 + y, y0 + x, 7); putpixel(x0 - y, y0 + x, 7); putpixel(x0 - x, y0 + y, 7); putpixel(x0 - x, y0 - y, 7); putpixel(x0 - y, y0 - x, 7); putpixel(x0 + y, y0 - x, 7); putpixel(x0 + x, y0 - y, 7); if (err 0) { x -= 1; err -= 2*x + 1; } } } void main() { int gdriver=DETECT, gmode, error, x, y, r; initgraph(&gdriver, &gmode, 'c:\turboc3\bgi'); printf('Enter radius of circle: '); scanf('%d', &r); printf('Enter co-ordinates of center(x and y): '); scanf('%d%d', &x, &y); drawcircle(x, y, r); getch(); } Saída
Escreva um programa para desenhar a curva de Bézier.
#include #include #include #include void bezier (int x[4], int y[4]) { int gd = DETECT, gm; int i; double t; initgraph (&gd, &gm, 'C:\tc\bgi'); for (t = 0.0; t <1.0; 3 t +="0.0005)" { double xt="pow" (1-t, 3) * x[0] pow 2) x[1] (t, (1-t) x[2] x[3]; yt="pow" y[0] y[1] y[2] y[3]; putpixel (xt, yt, white); } for (i="0;" i<4; i++) (x[i], y[i], yellow); getch(); closegraph(); return; void main() int x[4], y[4]; i; printf (\\\'enter the x- and y-coordinates of four control points.
\\\'); scanf (\\\'%d%d\\\', &x[i], &y[i]); bezier (x, y); < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-14.webp" alt="Computer Graphics Programs"> <br> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-15.webp" alt="Computer Graphics Programs"> <hr> <h3>Program to rotate a rectangle about its midpoint</h3> <pre> #include #include #include #include #include #define pi 3.14 class arc { float x[10], y[10], theta, h1, k1,r[0][10],ang; float p[10][10],p1[10][10],x1[10], y1[10],xm,yx; int i, k, j, n; public: void get(); void cal(); void map(); void graph(); void plot(); void plot1(); }; void arc::get() { cout '<>ang; n = 4; cout<<\\\'
enter\\\'; for (i="0;" i<n; i++) { cout<<\\\'
x[\\\' <i<<\\\'] and y[\\\'< <i<>x[i]>>y[i]; } h1=x [0] + (([1]-x[0])/2); k1=y[0]+(([3]-y[0])/2); cout<<\\\'
2 midpoint of rectangle is--\\\'<<h1<<\\\' \\\'<<k1; theta="(ang*pi)/180?;" r[0][0]="cos" (theta); r[0][1]="-sin?(theta);" r[0][2]="(-h1*cos(theta))+(k1*sin(theta)+h1;" r[1][0]="sin" r [1][1]="cos" [1][2]="(-h1*sin(theta))+(-k1*cos(theta)+k1;" r[2][0]="0;" r[2][1]="0;" r[2][2]="1;" } void arc ::cal() { for(i="0;i<n;i++)" p[0][i]="x[i];" p[1][i]="y[i];" p[2][i]="1;" for(j="0;j<n;j++)" p1[i][j]="0;" for (k="0;k<3;k++)" p1[i][j]+="r[i][k]*p[k][j];" x1[i]="p1[0][i];" y1[i]="p[1][i];" arc::map() int gd="DETECT," gm; initgraph (&gd, &gm, \\\'\\\'); errorcode="graphresult();" *an error occurred * if (errorcode!="grOK)" printf(\\\'graphics error: %s
\\\',grapherrormsg (errorcode)); printf(\\\'press and key to halt: \\\'); getch(); exit(1); terminate with an code arc::graph() xm="getmaxx()/2;" ym="getmaxy()/2;" line (xm, 0, xm, ym); (0, ym, :: plot 1() (i="0;i<n-1;i++)" circle (x1[i]+xm, (-y1[i]+ym), 2); x[i+1] (-y1[0]+ym)); (x1[n-1] +xm, (-y1[n-1]+ym), x1[0]+xm, () (x[i]+xm, (-y[i]+ym), (-y[i+1]+ym)); (x[n-1]+xm, (-y[n-1]+ym),2); (-y[n-1]+ym), x[0]+xm, (-y[0]+ym)); main class a; clrscr(); a.map(); a.graph(); a.get(); a.cal(); a.plot(); a.plot1(); < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-3.webp" alt="Computer Graphics Programs"> <hr> <h3>Program to clip a line using Liang Barsky Method </h3> <pre> #include #include #include #include #include class liang { float x1,x2,y1,y2,u1,u2,dx,dy,xm,ym; float xmin, ymin, xmax, ymax,p[4],q[4],r[5],r[5]; int gd, gm, test, s, k; public: void clipliang(); void get(); void map(); void graph(); }; void liang :: map() { gd=DETECT; initgraph (&gd, &gm, ''); int errorcode = graphresult(); /*an error occurred */ if (errorcode!=grOK) { printf('Graphics error: %s
',grapherrormsg (errorcode)); printf('Press and key to halt: '); getch(); exit(1); /* terminate with an error code */ } } void liang::graph() { xm=getmaxx()/2; ym=getmaxy()/2; line (xm, 0, xm, 2 * ym); line (0, ym, 2 * xm, ym); } void liang :: get() { cout<>xmin>>ymin>>xmax>>ymax>>; rectangle (xmin+xm,-ymin+ym,xmax+xm, -ymax+ym); // rectangle (320+xwmin, 240-ywmax, 320+xwmax, 240-ywmin); cout<>x1>>y1>>x2>>y2; line (x1+xm,-y1+ym, x2+xm,-y2+ym); getch(); } // line (x1, y1, x2, y2); void liang :: clipliang() { float x=0, y=1; dx=x2-x1; dy=y2-y1; p[0]=-dx; p[1]=dx; p[2]=-dy; p[3]=dy; q[0]=x1-xmin; q[1]=xmax-x1; q[2]=y1-ymin; q[3]=ymax-y1; for (k=0;k<4;k++) { if (p[k]="=0&&q[k]<0)" cout<<\\\'
outside\\\'; getch(); exit(0); } (p[k]<0) r[k]="q[k]/p[k];" (r1[k]u2) complete outside window\\\'; void main () liang1; clrscr(); l.map(); l.graph(); l.get(); l.clipliang(); < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-4.webp" alt="Computer Graphics Programs"> <hr> <h3>Program to implement Standard Perspective Projection in 3-Dimensions.</h3> <pre> #include #include #include #include #include #include #define pi 3.14 class per { float x[10], y[10], z [10], d; float x1 [10], y1[10], xm, ym, z1 [10]; int i, k, j, n; public: void get(); void perpro(); void map(); void graph(); void plot1(); } void per::get() { cout<>n; cout<<\\\'
enter\\\'; for (i="0;i<n;i++)" { cout<<\\\'
x[\\\'?i<<\\\'], y[\\\'< <i<<\\\'] and z [\\\'< <i<>x[i]>>y[i]>>z[i]; } cout<>d; } void per:: perpro() { for(i=0;i<n;i++) 2 { x1[i]="(d*x[i])/(z[i]+d);" y1[i]="(d*y[i])/(z[i]+d);" z1[i]="0;" } cout<< \\\'
enter projection, vertices of polygon are--\\\'; for(i="0;i<n;i++)" cout<<\\\'x?[\\\'< <i<<\\\']="<<x1[i]<< " \\\'; cout<<\\\'y?[\\\'< cout<<\\\'z?[\\\'< cout<<\\\'
\\\'; void per :: map() int gd="DETECT;" initgraph (&gd, &gm, \\\'\\\'); errorcode="graphresult();" *an error occurred * if (errorcode!="grOK)" printf(\\\'graphics error: %s
\\\',grapherrormsg (errorcode)); printf(\\\'press and key to halt: \\\'); getch(); exit(1); terminate with an code per::graph() xm="get" maxx() 2; ym="get" maxy() line (xm,0, xm, ym); (0, ym, per::plot1() line(x1[i]*100+xm,(-y1[i]*100+ym),x1[i+1]*100+xm,(-y1[i+1]*100+ym)); line(x1[n-1]*100+xm,(-y1[n-1]*100+ym),x1[0]*100+xm,(-y1[0]*100+ym)); main() class a; clrscr(); a.map(); a.graph(); a.get(); a.perpro(); a.plot1(); < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-5.webp" alt="Computer Graphics Programs"> <hr> <h3>Program to implement Parallel Projection in 3-Dimensions.</h3> <pre> #include #include #include #include #include #include #define pi 3.14 class per { float x[10], y[10], z [10], d, m[10], q1[10], r1[10], m1[10]; float x1 [10], y1[10], xm, ym, z1 [10], cav[4] [4], p[10] [10]; float p1[10] [10]; float a, b, c, ang, theta, f; int i, k, j, n; public: void get(); void par_v(); void par_vnr0(); void map(); void graph(); void project(float m[],float q[], float r[]); }; void per:: perproject(float m[], float q[], float r[]) { cout<>d; cout<< '
AFTER PROJECTION, VERTICES OF POLYGON ARE--'; for(i=0;i<n;i++) { cout<<\\\'x?[\\\'< <i<<\\\']="<<x1[i]<< " \\\'; cout<<\\\'y?[\\\'< cout<<\\\'z?[\\\'< cout<<\\\'
\\\'; } getch(); clearviewport(); graph(); for(i="0;i<n-1;i++)" line(m[i]*100+xm,(-q[i]*100+ym),m[i+1]*100+xm,(-n1[i+1]*100+ym)); line(m[n-1]*100+xm,(-q[n-1]*100+ym),m[0]*100+xm,(-q[0]*100+ym)); void per::get() cout<>n; cout<<\\\'
enter\\\'; for (i="0;i<n;i++)" { cout<<\\\'
x[\\\'?i<<\\\'], y[\\\'< <i<<\\\'] and z [\\\'< <i<>x[i]>>y[i]>>z[i]; } cout<>d; int ch=100; do { cout<orthographic projection\\\'; cout<oblique cout< exit\\\';>ch; switch (ch) { case1: int ch1=100; do { cout<for xy viewplane\\\'; cout<for yz cout\\\'
3->for zx viewplane'; cout<exit\\\'; cout<>ch1; switch(ch1) { case1: x1[i]=x[i]; y1[i]=y[i]; z1[i]=0; project(x1,y1,z1); break; case2: x1[i]=0; y1[i]=0; z1[i]=z[i]; project(x1,y1,z1); break; case3: x1[i]=x[i]; y1[i]=0; z1[i]=z[i]; project(x1,y1,z1); break; } } while (ch!=0) break; case2: ch1=100; do { cout<on xy plane\\\'; cout<cavalier\\\'; cout<exit\\\'; cout<>ch1; switch(ch1) { case1: par_v(); break; case2: par_vnr0(); break(); } while(ch!=0); break; } while(ch!=0) } void per::par_v() { cout<>a>>b>>c; for(i=0; i<n;i++) { x1[i]="x[i]-((a*z[i]/c);" y1[i]="y[i]-((b*z[i]/c);" project(x1,y1, z1); } void per::par_vnr0() cout<>f; cout<>angle; theta= ((pi/180)* ang); for(i=0;i<4;i++) 2 { for(j="0;j<4;j++)" if(i="=j)" cav[i] [j]="1;" else } cav[0] [2]="f*cos(theta);" cav[1][2]="f*sin" (theta); cav[2][2]="0;" for(i="0;i<n;i++)" p[0][i]="x[i];" p[1][i]="y[i];" p[2][i]="z[i];" p[3][i]="1;" p1[i][j]="0;" for(k="0;k<4;k++)" p1[i][j]+="cav[i][k]*p[k][j];" x1[i]="p1[0][i];" y1[i]="p1[1][i];" z1[i]="p1[i];" project(x1,y1,z1); void per :: map() int gd="DETECT;" initgraph (&gd, &gm, \\\'\\\'); errorcode="graphresult();" *an error occurred * if (errorcode!="grOK)" printf(\\\'graphics error: %s
\\\',grapherrormsg (errorcode)); printf(\\\'press and key to halt: \\\'); getch(); exit(1); terminate with an code per::graph() xm="get" maxx() 2; ym="get" maxy() line (xm,0, xm, ym); (0, ym, main() class a; clrscr(); a.map(); a.get(); < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-6.webp" alt="Computer Graphics Programs"> <hr> <h3>Program to draw Sine, Cosine and Tangent Curves</h3> <pre> #include #include #include #include #include #include #define pi 3.14 class arc { float x, y, a, xm, ym; int i, ch; public: void get(); void map(); void sine(); void cosine(); void tangent(); }; void arc::get() { cout<>y; cout<<\\\'
menu is---------\\\'; cout< sine curve\\\'; cout<cosine tangent exit\\\';>ch; switch(ch) { case1: sine(); break(); case2: cosine(); case3: tangent(); case4: exit(0); } } void arc::sine() { cleardevice(); xm=getmaxx()/2; ym=getmaxy()/2; line(xm, 0, xm, 2*ym); line(0, ym, 2 *xm, ym); outtextxy(0, ym, 'X-AXIS'); outtextxy(xm, 0, 'Y-AXIS'); for(x=-300;x<=300;x=x+0.5) 2 { y="a*sin((pi*x)/180);" putpixel(x+(320),-y+240,red); } void arc::cosine() cleardevice(); xm="getmaxx()/2;" ym="getmaxy()/2;" line(xm, 0, xm, 2*ym); line(0, ym, *xm, ym); outtextxy(0, \\\'x-axis\\\'); outtextxy(xm, \\\'y-axis\\\'); for(x="-300;x<=300;x=x+0.5)" arc :: map() int gd="DETECT,gm;" initgraph (&gd, &gm, \\\'\\\'); errorcode="graphresult();" *an error occurred * if (errorcode!="grOK)" printf(\\\'graphics error: %s
\\\',grapherrormsg (errorcode)); printf(\\\'press and key to halt: \\\'); getch(); exit(1); terminate with an code main() class a; clrscr(); a.map(); a.get(); < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/computer-graphics/65/computer-graphics-programs-7.webp" alt="Computer Graphics Programs"> <hr></=300;x=x+0.5)></\\\'
></pre></4;i++)></n;i++)></on></exit\\\';></for></orthographic></\\\'
></n;i++)></pre></n;i++)></\\\'
></pre></4;k++)></pre></\\\'
></\\\'
></pre></1.0;> 4;i++)> \\\\\'
> \\\\'
> 4;k++)> \\\'
> \\\'
> 1.0;> => \\'\\';> =(getmaxx()-r-10))> =8;i++)>