Publicité
computer graphics practicals
computer graphics practicals
computer graphics practicals
computer graphics practicals
Publicité
computer graphics practicals
computer graphics practicals
computer graphics practicals
computer graphics practicals
computer graphics practicals
Publicité
computer graphics practicals
computer graphics practicals
computer graphics practicals
computer graphics practicals
computer graphics practicals
Publicité
computer graphics practicals
computer graphics practicals
computer graphics practicals
computer graphics practicals
computer graphics practicals
Publicité
computer graphics practicals
computer graphics practicals
computer graphics practicals
computer graphics practicals
computer graphics practicals
Publicité
computer graphics practicals
computer graphics practicals
Prochain SlideShare
Computer graphics programs in c++Computer graphics programs in c++
Chargement dans ... 3
1 sur 26
Publicité

Contenu connexe

Publicité

computer graphics practicals

  1. Computer graphiCs Practical file AMITY UNIVERSITY HARYANA Submitted by : MANOJ KUMAR B.tech(cse) Sem-4
  2. LIST OF CONTENTS S.N O Programs pag e no date T.sign 1 Write a program to Draw Graphics Objects 3 2 Write a program to draw 2D bar 5 3 Write a program to draw Pattern fill 7 4 Write a program to draw PIE Chart 8 5 Write a program for Boundary Fill 9 6 Write a program for Flood fill 11 7 Write a program for Translatation of object 13 8 Write a program for Scaling of object 15 9 Write a program for Rotation of object 17 10 Write a program for Window to viewport transformation 19 11 Write a program for Shearing of object 23 2
  3. 1)Write a program to Draw Graphics Objects. #include<stdio.h> #include<conio.h> #include<stdlib.h> #include<dos.h> #include<graphics.h> void bfill4(int x,int y) {int c,f=2,b=0; c=getpixel(x,y); if((c!=b)&&(c!=f)) { putpixel(x,y,f); delay(1); bfill4(x+1,y); bfill4(x-1,y); bfill4(x,y+1); bfill4(x,y-1); } } void bfill4(int,int); void main() { int gd=DETECT,gm; int i=0,m=0; initgraph(&gd,&gm,"..bgi"); while(i<500) { 3
  4. cleardevice(); i++; line(80+i,300,90+i,270); line(90+i,270,120+i,270); line(120+i,270,160+i,240); line(160+i,240,230+i,240); line(230+i,240,275+i,270); line(275+i,270,310+i,270); line(310+i,270,335+i,290); line(335+i,290,335+i,300); line(255+i,300,335+i,300); line(180+i,300,210+i,300); line(80+i,300,135+i,300); circle(232+i,300,18); circle(157+i,300,18); bfill4(232+i,300); delay(5); } getch(); closegraph(); } 4
  5. 2)Write a program to draw 2D bar. #include<graphics.h> #include<conio.h> Void main() { int gd = DETECT, gm; initgraph(&gd, &gm, "C:TCBGI"); setcolor(YELLOW); rectangle(0,30,639,450); settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2); setcolor(WHITE); outtextxy(275,0,"Bar Chart"); setlinestyle(SOLID_LINE,0,2); line(100,420,100,60); line(100,420,600,420); line(90,70,100,60); line(110,70,100,60); line(590,410,600,420); line(590,430,600,420); outtextxy(95,35,"Y"); outtextxy(610,405,"X"); outtextxy(85,415,"O"); setfillstyle(LINE_FILL,BLUE); bar(150,100,200,419); setfillstyle(XHATCH_FILL,RED); bar(225,150,275,419); setfillstyle(WIDE_DOT_FILL,GREEN); 5
  6. bar(300,200,350,419); setfillstyle(INTERLEAVE_FILL,MAGENTA); bar(375,125,425,419); setfillstyle(HATCH_FILL,BROWN); bar(450,175,500,419); getch(); } 6
  7. 3)Write a program for Pattern fill. #include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> int main(void) { int gdriver = DETECT, gmode, errorcode; int midx, midy, i; initgraph(&gdriver, &gmode, "c:tcbgi"); errorcode = graphresult(); if (errorcode != grOk { printf("Graphics error: %sn", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); } midx = getmaxx() / 2; midy = getmaxy() / 2; for (i=0; i<12; i++) { setfillstyle(i, RED); bar(midx-50, midy-50, midx+50,midy+50); rectangle(midx-50,midy-50,midx+50,midy+50); } 7
  8. getch(); } 4)Write a program to draw PIE Chart. #include<graphics.h> #include<conio.h> main() { int gd = DETECT, gm, midx, midy; initgraph(&gd, &gm, "C:TCBGI"); setcolor(MAGENTA); rectangle(0,40,639,450); settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2); setcolor(WHITE); outtextxy(275,10,"Pie Chart"); midx = getmaxx()/2; midy = getmaxy()/2; setfillstyle(LINE_FILL,BLUE); pieslice(midx, midy, 0, 75, 100); outtextxy(midx+100, midy - 75, "20.83%"); setfillstyle(XHATCH_FILL,RED); pieslice(midx, midy, 75, 225, 100); outtextxy(midx-175, midy - 75, "41.67%"); 8
  9. setfillstyle(WIDE_DOT_FILL,GREEN); pieslice(midx, midy, 225, 360, 100); outtextxy(midx+75, midy + 75, "37.50%"); getch(); return 0; } 9
  10. 5)Write a program for Boundary Fill of polygons. #include<stdio.h> #include<conio.h> #include<dos.h> #include<graphics.h> void bfill4(int x,int y,int f,int b) {int c; c=getpixel(x,y); if((c!=b)&&(c!=f)) { putpixel(x,y,f); delay(1); bfill4(x+1,y,f,b); bfill4(x-1,y,f,b); bfill4(x,y+1,f,b); bfill4(x,y-1,f,b); } } void bfill4(int,int,int,int); void bfill8(int x,int y,int f,int b) { int d; d=getpixel(x,y); if((d!=b)&&(d!=f)) { putpixel(x,y,f); 10
  11. delay(1); bfill8(x-1,y+1,f,b); bfill8(x+1,y,f,b); bfill8(x-1,y,f,b); bfill8(x+1,y+1,f,b); bfill8(x-1,y-1,f,b); bfill8(x+1,y-1,f,b); bfill8(x,y+1,f,b); bfill8(x,y-1,f,b); } } void bfill8(int,int,int,int); void main() { clrscr(); int gd=DETECT,gm; initgraph(&gd,&gm,"c:turboc3bgi"); settextstyle(2,0,6); outtextxy(5,20,"4-connected b fill"); rectangle(10,50,70,100); bfill4(55,55,4,15); outtextxy(205,20,"8-connected b fill"); rectangle(220,50,280,100); bfill8(255,55,2,15); getch(); } 11
  12. 6) Write a program for flood Fill of polygons. #include<stdio.h> #include<conio.h> #include<graphics.h> #include<dos.h> void flood_fill(int x, int y, int ncolor, int ocolor) { if (getpixel(x, y) == ocolor) { putpixel(x, y, ncolor); delay(0); flood_fill(x + 1, y, ncolor, ocolor); flood_fill(x, y - 1, ncolor, ocolor); flood_fill(x, y + 1, ncolor, ocolor); flood_fill(x - 1, y, ncolor, ocolor); } } void main() { int gd=DETECT,gm; initgraph(&gd,&gm,"c:turboc3bgi"); int ncolor, ocolor; printf("Enter old color : "); scanf("%d", &ocolor); printf("Enter new color : "); scanf("%d", &ncolor); 12
  13. setcolor(RED); line(550,100,600,100); setcolor(GREEN); line(550,100,550,150); setcolor(BLUE); line(600,100,600,150); setcolor(WHITE); line(550,150,600,150); flood_fill(560,130, ncolor, ocolor); getch(); } 13
  14. 7)Write a program for Translatation of objects. #include<graphics.h> #include<conio.h> #include<stdio.h> #include<iostream.h> void draw(int,int,int,int,int,int,int,int); void translation(int [][3],int[][3]); void main() {int gd=DETECT,gm; initgraph(&gd,&gm,"c:turboc3bgi"); int x1,y1,x2,y2,x3,y3,x4,y4; printf("entre four coordinate"); scanf("%d%d%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4); draw(x1,y1,x2,y2,x3,y3,x4,y4); int obj[5][3]={ {x1,y1,1}, {x2,y2,1}, {x3,y3,1}, {x4,y4,1}, {x1,y1,1} }; int tx,ty; printf(" enter translation factor"); scanf("%d%d",&ty,&tx); int trans[3][3]={ {1,0,0}, {0,1,0}, {-tx,-ty,1}, 14
  15. }; translation(obj,trans); getch(); } void draw(int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4) { line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x3,y3,x4,y4); line(x4,y4,x1,y1); } void translation(int obj[][3],int trans[][3]) { int c[5][3]; for(int i=0;i<=4;i++) { for(int j=0;j<=2;j++) { c[i][j]=0; for(int k=0;k<=2;k++) { c[i][j]=c[i][j]+obj[i][k]*trans[k][j]; } } } draw(c[0][0],c[0][1],c[1][0],c[1][1],c[2][0],c[2][1],c[3][0],c[3][1]); } 15
  16. 16
  17. 8) Write a program for scaling of objects. #include<graphics.h> #include<conio.h> #include<iostream.h> void draw(int,int,int,int,int,int); void scaling(int[][2],int[][2]); void main() { int gd=DETECT,gm,x1,y1,x2,y2,x3,y3,sx,sy; initgraph(&gd,&gm,"..bgi"); cout<<"enter coordinate for first vertex of triangle:"; cin>>x1>>y1; cout<<"enter for second vertex:"; cin>>x2>>y2; cout<<"enter for third :"; cin>>x3>>y3; draw(x1,y1,x2,y2,x3,y3); int object[4][2]={ {x1,y1}, {x2,y2}, {x3,y3}, {x1,y1} }; cout<<"n enter x scaling:"; cin>>sx; cout<<"n y scaling"; cin>>sy; 17
  18. int scale[2][2]={ {sx,0}, {0,sy} }; scaling(object,scale); getch(); closegraph(); } void draw(int x1,int y1,int x2,int y2,int x3,int y3) { cout<<x1<<y1<<x2<<y2<<x3<<y3; line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x3,y3,x1,y1); } void scaling(int object[][2],int scale[][2]) { int c[4][2]; for(int i=0;i<=3;i++) { for(int j=0;j<=1;j++) { c[i][j]=0; for(int k=0;k<=1;k++) { c[i][j]=c[i][j]+object[i][k]*scale[k][j]; } } } draw(c[0][0],c[0][1],c[1][0],c[1][1],c[2][0],c[2][1]); } 18
  19. 19
  20. 9) write a program for rotation of objects #include<graphics.h> #include<conio.h> #include<iostream.h> #include<math.h> #include<stdio.h> void draw(float [][2]); void rotation(float[][2],float[][2]); void main() { int gd=DETECT,gm,x1,y1,x2,y2,x3,y3; float theta,a1,a2,a3; initgraph(&gd,&gm,"c:turboc3bgi"); cout<<"enter coordinate for first vertex of triangle:"; cin>>x1>>y1; cout<<"enter for second vertex:"; cin>>x2>>y2; cout<<"enter for third :"; cin>>x3>>y3; float object[3][2]={ {x1,y1}, {x2,y2}, {x3,y3} }; draw(object); cout<<"n enter rotation angle:"; cin>>theta; 20
  21. theta=theta*(3.14/180); a1=cos(theta); a2=-sin(theta); a3=sin(theta); float rotate[2][2]={ {a1,a2}, {a3,a1}, }; rotation(object,rotate); getch(); closegraph(); } void draw(float m[][2]) { line(m[0][0],m[0][1],m[1][0],m[1][1]); line(m[1][0],m[1][1],m[2][0],m[2][1]); line(m[2][0],m[2][1],m[0][0],m[0][1]); } void rotation(float object[][2],float rotate[][2]) { float c[3][2]; for(int i=0;i<=2;i++) { for(int j=0;j<=1;j++) { c[i][j]=0; for(int k=0;k<=1;k++) { c[i][j]=c[i][j]+object[i][k]*rotate[k][j]; 21
  22. } } } draw(c); } 10) Write a program for window to viewport transformation #include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> #include<dos.h> void draw(int ,int ,int ,int,int,int,int,int); void translation2(int ,int ,int ,int,int,int,int,int,int,int); void transaction(int,int,int,int,int,int,int,int); void scaling(int,int,int ,int ,int,int,int,int,int,int,int,int); void main() { int gd = DETECT, gm; initgraph(&gd,&gm,"c:turboc3BGI"); int x1,y1,x2,y2,x3,y3,x4,y4; printf("enter the coordinates of window"); scanf("%d%d%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4); draw(x1,y1,x2,y2,x3,y3,x4,y4); transaction(x1,y1,x2,y2,x3,y3,x4,y4); getch(); 22
  23. } void draw(int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4) { line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x3,y3,x4,y4); line(x4,y4,x1,y1); } void transaction(int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4) { int object[][3]={ {x1,y1,1}, {x2,y2,1}, {x3,y3,1}, {x4,y4,1} } ; int tx,ty,sx,sy; printf("enter the transaction factor"); scanf("%d%d",&tx,&ty); int trans[][3]={ {1,0,0}, {0,1,0}, {-tx,-ty,1} }; int c[4][3],i,j,k; for(i=0;i<=3;i++) { for(j=0;j<=2;j++) 23
  24. { c[i][j]=0; for(k=0;k<=2;k++) { c[i][j]=c[i][j]+object[i][k]*trans[k][j]; } } } draw(c[0][0],c[0][1],c[1][0],c[1][1],c[2][0],c[2][1],c[3][0],c[3][1]); scaling(c[0][0],c[0][1],c[1][0],c[1][1],c[2][0],c[2][1],c[3][0],c[3][1],x1,y1,x4,y2); } void scaling(int x5,int y5,int x6,int y6,int x7,int y7,int x8,int y8,int x1,int y1,int x4,int y2) { int newobj[][3]={ {x5,y5,1}, {x6,y6,1}, {x7,y7,1}, {x8,y8,1} }; int sx,sy,x9,y9,x10,y10,x11,y11,x12,y12; printf("enter the coordinates of view port"); scanf("%d%d%d%d%d%d%d%d",&x9,&y9,&x10,&y10,&x11,&y11,&x12,&y12); draw(x9,y9,x10,y10,x11,y11,x12,y12); sx=((y10-y9)/(y2-y1)); sy=((x12-x9)/(x4-x1)); int scale[][3]={ {sx,0,0}, 24
  25. {0,sy,0}, {0,0,1} }; int r[4][3],i,j,k; for(i=0;i<=3;i++) { for(j=0;j<=2;j++) { r[i][j]=0; for(k=0;k<=2;k++) { r[i][j]=r[i][j]+newobj[i][k]*scale[k][j]; } } } draw(r[0][0],r[0][1],r[1][0],r[1][1],r[2][0],r[2][1],r[3][0],r[3][1]); translation2(r[0][0],r[0][1],r[1][0],r[1][1],r[2][0],r[2][1],r[3][0],r[3][1],x9,y9); } void translation2(int x5,int y5,int x6,int y6,int x7,int y7,int x8,int y8,int x9,int y9) { int object[][3]={ {x5,y5,1}, {x6,y6,1}, {x7,y7,1}, {x8,y8,1} } ; int tx,ty,sx,sy; tx=(x9); ty=(y9); 25
  26. int trans[][3]={ {1,0,0}, {0,1,0}, {tx,ty,1} }; int c[4][3],i,j,k; for(i=0;i<=3;i++) { for(j=0;j<=2;j++) { c[i][j]=0; for(k=0;k<=2;k++) { c[i][j]=c[i][j]+object[i][k]*trans[k][j]; } } } draw(c[0][0],c[0][1],c[1][0],c[1][1],c[2][0],c[2][1],c[3][0],c[3][1]); } 26
Publicité