CG Record
CG Record
PROGRAM:
void main()
{ int x1,x2,y1,y2; int gd=DETECT,gm; void linebres(int,int,int,int); printf("Enter the two end points:"); scanf("%d%d%d%d",&x1,&x2,&y1,&y2); initgraph(&gd,&gm,""); cleardevice(); linebres(x1,y1,x2,y2); getch(); line(x1,y1,x2,y2); getch(); closegraph(); }
void linebres(int x1,int y1,int x2,int y2)
y=y2; xend=x1; } else { x=x1; y=y1; xend=x2; } putpixel(x,y,2); for(i=x;i<xend;i++) { x+=1; if(p<0) p+=2*dy; else p+=2*(dy-dx); } putpixel(x,y,2); } else { p=2*dx-dy;
if(y1>y2) { x=x2; y=y2; yend=y2; } putpixel(x,y,2); for(i=y;i<yend;i++) { y+=1; if(p<0) p+=2*dx; else { x+=1; p+=2*(dx-dy); } putpixel(x,y,2); } } }
OUTPUT : Bresenham linedrawing algorithm Enter the co-ordinates 150 100 250 300
RESULT: Thus the above program has been executed and output is verified.
void main() { int gd=DETECT,gm; int x,y,r; void cir(int,int,int); printf("EntertheMidpoints and Radious:"); scanf("%d%d%d",&x,&y,&r); initgraph(&gd,&gm,""); cir(x,y,r); getch(); closegraph(); } void cir(int x1,int y1,int r) { int x=0,y=r,p=1-r; void cliplot(int,int,int,int); cliplot(x1,y1,x,y); while(x<y) { x++; if(p<0) p+=2*x+1;
{ putpixel(xctr +x,yctr +y,1); putpixel(xctr -x,yctr +y,1); putpixel(xctr +x,yctr -y,1); putpixel(xctr -x,yctr -y,1); putpixel(xctr +y,yctr +x,1); putpixel(xctr -y,yctr +x,1); putpixel(xctr +y,yctr -x,1); putpixel(xctr -y,yctr -x,1);
getch(); }
RESULT: Thus the above program has been executed and output is verified.
10
PROGRAM: #include<stdio.h> #include<conio.h> #include<math.h> #include<graphics.h> main() { int gd=DETECT, gm; int xcenter, ycenter,rx,ry; int p,x,y,px,py,rx1,ry1,rx2,ry2; initgraph(&gd,&gm,"c:\\tc\\bgi"); printf("Enter The Radius Value:\n"); scanf("%d%d",&rx,&ry); printf("Enter The xcenter and ycenter Values:\n");
11
scanf("%d%d",&xcenter,&ycenter);
/* REGION 1 */ x =0 ; y=ry; plot points (xcenter,ycenter,x,y); p=(ry1-rx1*ry+(0.25*rx1)); px=0; py=rx2*y; while(px<py) { x=x+1; px=px+ry2; if(p>=0) y=y-1; py=py-rx2; if(p<0) p=p+ry1+px; else p=p+ry1+px-py; plot points (xcenter, ycenter, x, y); /* REGION 2*/ p=(ry1*(x+0.5)*(x+0.5)+rx1*(y-1)*(y-1)-rx1*ry1); while(y>0)
12
y=y-1; py=py-rx2; if(p<=0) { x=x+1; px=px+ry2; } if(p>0) p=p+rx1-py; else p=p+rx1-py+px; plotpoints(xcenter,ycenter,x,y); } } getch(); return(0); } int plotpoints(int xcenter,int ycenter,int x,int y) { putpixel(xcenter+x,ycenter+y,6); putpixel(xcenter-x,ycenter+y,6); putpixel(xcenter+x,ycenter-y,6); putpixel(xcenter-x,ycenter-y,6);
13
OUTPUT: Enter the Radius values: 10 30 Enter the X center and Y center : 300 150
RESULT: Thus the above program has been executed and output is verified.
14
PROGRAM: #include <graphics.h> #include <stdlib.h> #include <string.h> #include <stdio.h> #include <conio.h>
15
printf("******Line Types*******\n"); printf("\[Link] Line"); printf("\[Link] Line"); printf("\[Link] Line"); printf("\[Link] Line"); printf("\[Link] Line"); printf("\[Link]"); printf("\n\nEnter your choice:\n"); scanf("%d",&ch); switch(ch) { case 1: clrscr(); initgraph(&gd,&gm," "); setlinestyle(0,1,3); line(100,30,250,250); getch(); cleardevice(); closegraph(); break; case 2: clrscr(); initgraph(&gd,&gm," "); clrscr(); setlinestyle(1,1,3); line(100,30,250,250); getch();
16
cleardevice(); closegraph(); break; case 3: clrscr(); initgraph(&gd,&gm," "); setlinestyle(2,1,3); line(100,30,250,250); getch(); cleardevice(); closegraph(); break; case 4: clrscr(); initgraph(&gd,&gm," "); setlinestyle(3,1,3); line(100,30,250,250); getch(); cleardevice(); closegraph(); break; case 5: clrscr(); initgraph(&gd,&gm," "); setlinestyle(4,1,3); line(100,30,250,250); getch();
17
} } }
18
Output: ******Line Types******* [Link] Line [Link] Line [Link] Line [Link] Line [Link] Line [Link]
RESULT: Thus using C++ program implementation of Bresenhams algorithm for line drawing is done.
19
PROGRAM: #include <graphics.h> #include <stdlib.h> #include <string.h> #include <stdio.h> #include <conio.h> void main() { int gd=DETECT,gm; int ch; clrscr(); while(1) { printf("******Circle Attributes*******\n"); printf("\[Link] Fill");
20
printf("\[Link] Fill"); printf("\[Link] Fill"); printf("\[Link] Fill"); printf("\[Link] Fill"); printf("\[Link] Fill"); printf("\[Link] Fill"); printf("\[Link] Fill"); printf("\[Link] Fill"); printf("\[Link] Fill"); printf("\[Link] dot Fill"); printf("\[Link] dot Fill"); printf("\[Link] Fill"); printf("\[Link]"); printf("\n\nEnter your choice:\n"); scanf("%d",&ch); switch(ch) { case 1: clrscr(); initgraph(&gd,&gm," "); setfillstyle(EMPTY_FILL, RED); circle(100, 100, 50); floodfill(100, 100, WHITE); getch(); cleardevice(); closegraph(); break;
21
case 2: clrscr(); initgraph(&gd,&gm," "); setfillstyle(SOLID_FILL, RED); circle(100, 100, 50); floodfill(100, 100, WHITE); getch(); cleardevice(); closegraph(); break; case 3: clrscr(); initgraph(&gd,&gm," "); setfillstyle(LINE_FILL, RED); circle(100, 100, 50); floodfill(100, 100, WHITE); getch(); cleardevice(); closegraph(); break; case 4: clrscr(); initgraph(&gd,&gm," "); setfillstyle(LTSLASH_FILL, RED); circle(100, 100, 50); floodfill(100, 100, WHITE); getch(); cleardevice();
22
closegraph(); break; case 5: clrscr(); initgraph(&gd,&gm," "); setfillstyle(SLASH_FILL, RED); circle(100, 100, 50); floodfill(100, 100, WHITE); getch(); cleardevice(); closegraph(); break; case 6: clrscr(); initgraph(&gd,&gm," "); setfillstyle(BKSLASH_FILL, RED); circle(100, 100, 50); floodfill(100, 100, WHITE); getch(); cleardevice(); closegraph(); break; case 7: clrscr(); initgraph(&gd,&gm," "); setfillstyle(LTBKSLASH_FILL, RED); circle(100, 100, 50); floodfill(100, 100, WHITE);
CS 2405 COMPUTER GRAPHICS LABORATORY
23
getch(); cleardevice(); closegraph(); break; case 8: clrscr(); initgraph(&gd,&gm," "); setfillstyle(HATCH_FILL, RED); circle(100, 100, 50); floodfill(100, 100, WHITE); getch(); cleardevice(); closegraph(); break; case 9: clrscr(); initgraph(&gd,&gm," "); setfillstyle(XHATCH_FILL, RED); circle(100, 100, 50); floodfill(100, 100, WHITE); getch(); cleardevice(); closegraph(); break; case 10: clrscr(); initgraph(&gd,&gm," "); setfillstyle(INTERLEAVE_FILL, RED);
CS 2405 COMPUTER GRAPHICS LABORATORY
24
circle(100, 100, 50); floodfill(100, 100, WHITE); getch(); cleardevice();closegraph(); break; case 11: clrscr(); initgraph(&gd,&gm," "); setfillstyle(WIDE_DOT_FILL, RED); circle(100, 100, 50); floodfill(100, 100, WHITE); getch(); cleardevice();closegraph(); break; case 12: clrscr(); initgraph(&gd,&gm," "); setfillstyle(CLOSE_DOT_FILL, RED); circle(100, 100, 50); floodfill(100, 100, WHITE); getch(); cleardevice();closegraph(); break; case 13: clrscr(); initgraph(&gd,&gm," "); setfillstyle(USER_FILL, RED); circle(100, 100, 50); floodfill(100, 100, WHITE);
CS 2405 COMPUTER GRAPHICS LABORATORY
25
26
Output:******Circle Attributes******* [Link] Fill [Link] Fill [Link] Fill [Link] Fill [Link] Fill [Link] Fill [Link] Fill [Link] Fill [Link] Fill [Link] Fill [Link] dot Fill [Link] dot Fill [Link] Fill [Link] Enter your choice:
1.
2.
27
12
Result: Thus using C program implementation of Bresenhams algorithm for circle drawing is done.
28
PROGRAM:#include <graphics.h> #include <stdlib.h> #include <string.h> #include <stdio.h> #include <conio.h> void main() { int gd=DETECT,gm; int ch; clrscr(); while(1) { printf("******Ellipse Attributes*******\n"); printf("\[Link] Fill"); printf("\[Link] Fill");
29
printf("\[Link] Fill"); printf("\[Link] Fill"); printf("\[Link] Fill"); printf("\[Link] Fill"); printf("\[Link] Fill"); printf("\[Link] Fill"); printf("\[Link] Fill"); printf("\[Link] Fill"); printf("\[Link] dot Fill"); printf("\[Link] dot Fill"); printf("\[Link] Fill"); printf("\[Link]"); printf("\n\nEnter your choice:\n"); scanf("%d",&ch); switch(ch) { case 1: clrscr(); initgraph(&gd,&gm," "); setfillstyle(EMPTY_FILL, RED); ellipse(100, 100,0,360,50,25); floodfill(100, 100, WHITE); getch(); cleardevice(); closegraph(); break; case 2: clrscr(); initgraph(&gd,&gm," "); setfillstyle(SOLID_FILL, RED); ellipse(100, 100,0,360,50,25); floodfill(100, 100, WHITE); getch(); cleardevice(); closegraph(); break;
30
case 3: clrscr(); initgraph(&gd,&gm," "); setfillstyle(LINE_FILL, RED); ellipse(100, 100,0,360,50,25); floodfill(100, 100, WHITE); getch(); cleardevice(); closegraph(); break; case 4: clrscr(); initgraph(&gd,&gm," "); setfillstyle(LTSLASH_FILL, RED); ellipse(100, 100,0,360,50,25); floodfill(100, 100, WHITE); getch(); cleardevice(); closegraph(); break; case 5: clrscr(); initgraph(&gd,&gm," "); setfillstyle(SLASH_FILL, RED); ellipse(100, 100,0,360,50,25); floodfill(100, 100, WHITE); getch(); cleardevice(); closegraph(); break; case 6: clrscr(); initgraph(&gd,&gm," "); setfillstyle(BKSLASH_FILL, RED); ellipse(100, 100,0,360,50,25); floodfill(100, 100, WHITE); getch();
31
cleardevice(); closegraph(); break; case 7: clrscr(); initgraph(&gd,&gm," "); setfillstyle(LTBKSLASH_FILL, RED); ellipse(100, 100,0,360,50,25); floodfill(100, 100, WHITE); getch(); cleardevice(); closegraph(); break; case 8: clrscr(); initgraph(&gd,&gm," "); setfillstyle(HATCH_FILL, RED); ellipse(100, 100,0,360,50,25); floodfill(100, 100, WHITE); getch(); cleardevice(); closegraph(); break; case 9: clrscr(); initgraph(&gd,&gm," "); setfillstyle(XHATCH_FILL, RED); ellipse(100, 100,0,360,50,25); floodfill(100, 100, WHITE); getch(); cleardevice(); closegraph(); break; case 10: clrscr(); initgraph(&gd,&gm," "); setfillstyle(INTERLEAVE_FILL, RED);
32
ellipse(100, 100,0,360,50,25); floodfill(100, 100, WHITE); getch(); cleardevice(); closegraph(); break; case 11: clrscr(); initgraph(&gd,&gm," "); setfillstyle(WIDE_DOT_FILL, RED); ellipse(100, 100,0,360,50,25); floodfill(100, 100, WHITE); getch(); cleardevice(); closegraph(); break; case 12: clrscr(); initgraph(&gd,&gm," "); setfillstyle(CLOSE_DOT_FILL, RED); ellipse(100, 100,0,360,50,25); floodfill(100, 100, WHITE); getch(); cleardevice(); closegraph(); break; case 13: clrscr(); initgraph(&gd,&gm," "); setfillstyle(USER_FILL, RED); ellipse(100, 100,0,360,50,25); floodfill(100, 100, WHITE); getch(); cleardevice();closegraph(); break;
33
OUTPUT:******Ellipse Attributes******* [Link] Fill [Link] Fill [Link] Fill [Link] Fill [Link] Fill [Link] Fill [Link] Fill [Link] Fill [Link] Fill [Link] Fill [Link] dot Fill [Link] dot Fill [Link] Fill [Link] Enter your choice: 1
34
12
RESULT: Thus using C program implementation of Bresenhams algorithm for ellipse drawing is done.
35
36
37
PROGRAM: #include<stdio.h> #include<conio.h> #include<graphics.h> #include<dos.h> #include<math.h> #include<stdlib.h> void menu(); void input(); void output(); void translation(); void rotation(); void scaling(); void shearing(); void reflection();
int a[10][2],i,x,option,temp,angle,tx,ty,fx,fy,sh,k,n,axis,y;
38
printf("[Link]\n"); printf("[Link]\n"); printf("enter the choice:"); scanf("%d",&option); switch(option) { case 1: input(); translation(); break; case 2: input(); rotation(); break; case 3: input(); scaling(); break; case 4 : input(); shearing(); break; case 5: input(); reflection(); break;
39
case 6: exit(0); } } void input() { printf("enter the number of vertices:" ); scanf("%d",&n); for(i=0;i<n;i++) { printf("enter the coordinates:"); scanf("%d%d%d%d",&a[i][0],&a[i][1],&a[i+1][0],&a[i+1][1]); } } void output() { clear device(); for(i=0;i<n;i++) { line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]); } } break;
void translation() {
40
output(); printf(Enter the transformation vertex tx,ty:/n); scanf(%d%d,&tx,&ty); for(i=0;i<=n;i++) { a[i][0]=a[i][0]+tx; a[i][1]=a[i][1]+ty; } output(); delay(10); menu(); }
void rotation() { output(); printf("enter the rotating angle:"); scanf("%d",&y); printf("enter the pivot point:"); scanf("%d%d",&fx,&fy); k=(y*3.14)/180; for(i=0;i<=n;i++) { a[i][0]=fx+(a[i][0]-fx)*cos(k)-(a[i][1]-fy)*sin(k); a[i][1]=fy+(a[i][0]-fx)*sin(k)-(a[i][1]-fy)*cos(k); }
41
output(); delay(10); menu(); } void scaling() { output(); printf("enter the scaling factor\n"); scanf("%f%f",&sx,&sy); printf("enter the fixed point:"); scanf("%d%d",&fx,&fy); for(i=0;i<=n;i++) { a[i][0]=a[i][0]*sx+fy*(1-sx); a[i][1]=a[i][1]*sy+fy*(1-sy); } output(); delay(10); menu(); } void shearing() { output(); printf("enter the shear value:"); scanf("%d",&sh); printf("enter the fixed point:");
42
scanf("%d%d",&fx,&fy); printf("enter the axis for shearing if x-axis then 1 if y-axis the scanf("%d",&axis); for(i=0;i<=n;i++) { if(axis==1) { a[i][0]=a[i][0]+sh*(a[i][1]-fy); } else { a[i][1]=a[i][1]+sh*(a[i][0]-fx); } } output(); delay(10); menu(); } void reflection() { output(); for(i=0;i<=n;i++) { temp=a[i][0]; a[i][0]=a[i][1]; a[i][1]=temp; }
43
output(); delay(10); menu(); } void main() { int gd=DETECT,gm; initgraph(&gd,&gm,"c:\\tc\\bgi"); menu(); getch(); }
44
OUTPUT: MENU 1. Translation 2. Rotation 3. Scaling 4. Shearing 5. Reflection 6. Exit Enter the choice : 1 Enter the number of Vertices: 3 Enter the coordinates : Enter the coordinates : Enter the coordinates : 30 10 60 150 200 200 10 60 30 200 200 150
45
ROTATION
Enter the choice : 2 Enter the number of Vertices: 3 Enter the coordinates : Enter the coordinates : Enter the coordinates : 30 10 60 150 200 200 10 60 30 200 200 150
Enter the Rotating Angle : 90 Enter the Pivot Point : 100 200
46
SCALING
Enter the choice : 3 Enter the number of Vertices: 3 Enter the coordinates : Enter the coordinates : Enter the coordinates : 30 10 150 200 10 60 30 200 200 150
60 200
0.3 100
0.4 200
47
SHEARING
Enter the choice : 4 Enter the number of Vertices: 3 Enter the coordinates : Enter the coordinates : Enter the coordinates : 30 10 60 150 200 200 10 60 30 200 200 150
Enter the shear Value : Enter the fixed point : Enter the Axis for shearing if x-axis then 1 if y-axis then 0
5 50 100
48
REFLECTION Enter the choice : 5 Enter the number of Vertices: 3 Enter the coordinates : Enter the coordinates : Enter the coordinates : 30 10 60 150 200 200 10 60 30 200 200 150
RESULT: Thus the above program has been executed and output is verified.
49
50
PROGRAM:
51
void main() { int gd,gm,n,i,xa[10],ya[10],op,tx,ty,xa1[10],ya1[10],theta,xf,yf,rx,ry, sx,sy,shx,shy,xref,yref; char d; gd=DETECT; initgraph(&gd,&gm,""); cout<<"enter the no of points"; cin>>n; for(i=0;i<n;i++) { cout<<"enter the coordinates"<<i+1; cin>>xa[i]>>ya[i]; } do { cout<<"menu"; cout<<"\[Link]\[Link]\[Link]\[Link]\[Link] \[Link]"; cin>>op; switch(op) { case 1: cout<<"enter the translation vector"; cin>>tx>>ty; for(i=0;i<n;i++) { xa1[i]=xa[i]+tx; ya1[i]=ya[i]+ty;
52
case 2: cout<<"enter the rotation angle"; cin>>theta; theta=(theta*3.14)/180; cout<<"enter the reference points"; cin>>xf>>yf; for(i=0;i<n;i++) { xa1[i]=xf+(xa[i]-xf)*cos(theta)-(ya[i]-yf)*sin(theta); ya1[i]=yf+(xa[i]-xf)*sin(theta)-(ya[i]-yf)*cos(theta); }
53
cleardevice(); cout<<"before rotation"; for(i=0;i<n;i++) { line(xa[i],ya[i],xa[(i+1)%n],ya[(i+1)%n]); } cout<<"after rotation"; for(i=0;i<n;i++) { line(xa1[i],ya1[i],xa1[(i+1)%n],ya1[(i+1)%n]); } getch(); cleardevice(); cout<<"before scaling"; for(i=0;i<n;i++) { line(xa[i],ya[i],xa[(i+1)%n],ya[(i+1)%n]); } cout<<"after scaling"; for(i=0;i<n;i++) { line(xa1[i],ya1[i],xa1[(i+1)%n],ya1[(i+1%n]); } getch(); cleardevice(); break;
54
case 4: cout<<"enter the shear value"; cin>>shx>>shy; cout<<"enter the reference point"; cin>>xref>>yref; cout<<"enter the shear direction x or y"; cin>>d; if(d=='x') { for(i=0;i<n;i++) { xa1[i]=xa[i]+shx*(ya[i]-yref); ya1[i]=ya[i]; } } cleardevice(); cout<<"before shearing"; for(i=0;i<n;i++) { line(xa[i],ya[i],xa[(i+1)%n],ya[(i+1)%n]); } cout<<"after shearing"; for(i=0;i<n;i++) { line(xa1[i],ya1[i],xa1[(i+1)%n],ya1[(i+1)%n]); } getch();
55
cleardevice(); break: case 5: cout<<"before reflection"; for(i=0;i<n;i++) { line(xa[i],ya[i],xa[(i+1)%n],ya[(i+1)%n]); } cout<<"after reflection"; for(i=0;i<n;i++) { line(ya[i],xa[i],ya[(i+1)%n],xa[(i+1)%n]); } getch(); cleardevice(); break; case 6: exit(0); break; } } while(op!=6); }
56
enter the no of points: 3 enter the coordinates 1: 50 enter the coordinates 2: 50 enter the coordinates 3: 150 enter the translation vector: 50 50 75 30
BEFORE TRANSLATION
AFTER TRANSLATION
57
Enter your choice 2 enter the rotation angle:60 enter the reference points: 30 40
BEFORE
AFTER
Enter your choice 3 Enter the scaling factor: 3 4 Enter the reference points: 30 40 BEFORE AFTER
58
Enter your choice 4: Enter the shear value: 3 4 Enter the reference point: 20 30 Enter the shear direction x or y: X BEFORE AFTER
Enter your choice 5 Enter the reflection value: 3 4 Enter the reflection point: 20 30 BEFORE AFTER
RESULT: Thus the above program has been executed and output is verified.
59
60
PROGRAM: #include<stdio.h> #include<math.h> #include<graphics.h> #include<conio.h> float cxl, cxr,cyt,cyb; code(float,float); void clip(float,float,float,float); void rect(float,float,float,float); void main() { float x1,y1,x2,y2; int g=0,d; initgraph(&g,&d,"c:\\tc\\bin"); settextstyle(1,0,1); outtextxy(40,15,"BEFORE CLIPPING"); printf("\n\n\n please enter the left,bottom,right,top,of clip window"); scanf("%f%f%f%f",&cxl,&cyt,&cxr,&cyt); rect(cxl,cyb,cxr,cyt); getch(); printf("\n please enter the line(x1,y1,x2,y2):"); scanf("%f%f%f%f",&x1,&y1,&x2,&y2); line(x1,y1,x2,y2); getch(); cleardevice(); settextstyle(1,0,1);
61
outtextxy(40,15,"after clipping"); clip(x1,y1,x2,y2); getch(); closegraph(); } void clip(float x1,float y1,float x2,float y2) { int c,c1,c2; float x,y; c1=code(x1,y1); c2=code(x2,y2); getch(); while((c1!=0)||(c2!=0)) { if((c1&c2)!=0) goto out; c=c1; if(c==0) c=c2; if((c&1)==1) { y=y1+(y2-y1)*(cxl-x1)/(x2-x1); x=cxl; } Else if((c&2)==2 { y=y1+(y2-y1)*(cxl-x1)/(x2-x1);
62
x=cxr; } else if((c&8)==8) { x=x1+(x2-x1)*(cyb-y1)/(y2-y1); y=cyb; } Else if((c&4)==4) { x=x1+(x2-x1)*(cyt-y1)/(y2-y1); y=cyt; } if(c==c1) { x1=x; y1=y; c1=code(x,y); } Else { x2=x; y2=y; c2=code(x,y); } }
63
out : rect(cxl,cyb,cxr,cyt); line(x1,y1,x2,y2); } code(float x,float y) { int c=0; if(x<cxl) c=1; else if(x>cxr) c=2; if(y<cyb) c=c|8; else if(y>cyt) c=c|4; return c; } void rect(float xl,float yb,float xr,float yt) { line(xl,yb,xr,yb); line(xr,yb,xr,yt); line(xr,yt,xl,yt); line(xl,yt,xl,yb); }
64
OUTPUT: Enter the left, bottom, right ,top of clip window: 200 200 400 400 Enter the line coordinates: 250 300 400 450
BEFORE CLIPPING
AFTER CLIPPING
RESULT: Thus the above program has been executed and output is verified.
65
PROGRAM:
main() { float sx,sy; int w1,w2,w3,w4,x1,x2,x3,x4,y1,y2,y3,y4,v1,v2,v3,v4; int gd=DETECT,gm; initgraph(&gd,&gm,"c:\\tc\\bgi"); printf("Enter The Coordinate x1,y1,x2,y2,x3,y3\n"); scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3);
66
cleardevice(); w1=5; w2=5; w3=635; w4=465; rectangle(w1,w2,w3,w4); line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x3,y3,x1,y1); getch(); v1=425; v2=75; v3=550; v4=250; sx=(float)(v3-v1)/(w3-w1); sy=(float)(v4-v2)/(w4-w2); rectangle(v1,v2,v3,v4); x1=v1+floor(((float)(x1-w1)*sx)+.5); x2=v1+floor(((float)(x2-w1)*sx)+.5); x3=v1+floor(((float)(x3-w1)*sx)+.5); y1=v2+floor(((float)(y1-w2)*sy)+.5); y2=v2+floor(((float)(y2-w2)*sy)+.5); y3=v2+floor(((float)(y3-w2)*sy)+.5); line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x3,y3,x1,y1); getch(); return 0; }
CS 2405 COMPUTER GRAPHICS LABORATORY
67
OUTPUT:
Enter The Coordinate: x1,y1,x2,y2,x3,y3 100 200 300 400 500 350
RESULT: Thus the above program has been executed and output is verified.
68
69
PROGRAM:
#include <stdio.h> #include <graphics.h> #include <conio.h> #include <math.h> #include <process.h> #define TRUE 1 #define FALSE 0 typedef unsigned int outcode; outcode CompOutCode(float x,float y); enum { TOP = 0x1, BOTTOM = 0x2, RIGHT = 0x4, LEFT = 0x8 }; float xwmin,xwmax,ywmin,ywmax; void clip(float x0,float y0,float x1,float y1) { outcode outcode0,outcode1,outcodeOut; int accept = FALSE,done = FALSE; outcode0 = CompOutCode(x0,y0); outcode1 = CompOutCode(x1,y1); do {
70
if(!(outcode0|outcode1)) { accept = TRUE done = TRUE; } else if(outcode0 & outcode1) done = TRUE; else { float x,y; outcodeOut = outcode0?outcode0:outcode1; if(outcodeOut & TOP) { x = x0+(x1-x0)*(ywmax-y0)/(y1-y0); y = ywmax; } else if(outcodeOut & BOTTOM) { x = x0+(x1-x0)*(ywmin-y0)/(y1-y0); y = ywmin; } else if(outcodeOut & RIGHT) { y = y0+(y1-y0)*(xwmax-x0)/(x1-x0); x = xwmax;
CS 2405 COMPUTER GRAPHICS LABORATORY
71
} else { y = y0+(y1-y0)*(xwmin-x0)/(x1-x0); x = xwmin; } if(outcodeOut==outcode0) { x 0 = x; y0 = y; outcode0 = CompOutCode(x0,y0); } else { x 1 = x; y1 = y; outcode1 = CompOutCode(x1,y1); } } } while(done==FALSE); if(accept) line(x0,y0,x1,y1); outtextxy(150,20,"POLYGON AFTER CLIPPING"); rectangle(xwmin,ywmin,xwmax,ywmax); } outcode CompOutCode(float x,float y)
72
{ outcode code = 0; if(y>ywmax) code|=TOP; else if(y<ywmin) code|=BOTTOM; if(x>xwmax) code|=RIGHT; else if(x<xwmin) code|=LEFT; return code; } void main( ) { float x1,y1,x2,y2; /* request auto detection */ int gdriver = DETECT, gmode, n,poly[14],i; clrscr( ); printf("Enter the no of sides of polygon:"); scanf("%d",&n); printf("\nEnter the coordinates of polygon\n"); for(i=0;i<2*n;i++) { scanf("%d",&poly[i]); }
73
poly[2*n]=poly[0]; poly[2*n+1]=poly[1]; printf("Enter the rectangular coordinates of clipping window\n"); scanf("%f%f%f%f",&xwmin,&ywmin,&xwmax,&ywmax); /* initialize graphics and local variables */ initgraph(&gdriver, &gmode, "c:\\tc\\bgi"); outtextxy(150,20,"POLYGON BEFORE CLIPPING"); drawpoly(n+1,poly); rectangle(xwmin,ywmin,xwmax,ywmax); getch( ); cleardevice( ); for(i=0;i<n;i++) clip(poly[2*i],poly[(2*i)+1],poly[(2*i)+2],poly[(2*i)+3]); getch( ); restorecrtmode( ); }
74
OUTPUT: Enter the no of sides of polygon: 5 Enter the coordinates of polygon: 50 50 200 100 350 350 80 200 40 80 Enter the rectangular coordinates of clipping window: 150 150 300 300
BEFORE CLIPPING
AFTER CLIPPING
RESULT: Thus the above program has been executed and output is verified.
75
76
PROGRAM: #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> int maxx,maxy,midx,midy; void axis() { getch(); cleardevice(); line(midx,0,midx,maxy); line(0,midy,maxx,midy); } void main() { int gd,gm,x,y,z,o,x1,x2,y1,y2; detectgraph(&gd,&gm); initgraph(&gd,&gm," "); setfillstyle(0,getmaxcolor()); maxx=getmaxx(); maxy=getmaxy(); midx=maxx/2; midy=maxy/2; axis(); bar3d(midx+50,midy-100,midx+60,midy-90,5,1); printf("Enter Translation Factor"); scanf("%d%d%d",&x,&y,&z);
77
axis(); printf("after translation"); bar3d(midx+(x+50),midy-(y+100),midx+x+60,midy-(y+90),5,1); axis(); bar3d(midx+50,midy+100,midx+60,midy-90,5,1); printf("Enter Scaling Factor"); scanf("%d%d%d",&x,&y,&z); axis(); printf("After Scaling"); bar3d(midx+(x*50),midy-(y*100),midx+(x*60),midy-(y*90),5*z,1); axis(); bar3d(midx+50,midy-100,midx+60,midy-90,5,1); printf("Enter Rotating Angle"); scanf("%d",&o); x1=50*cos(o*3.14/180)-100*sin(o*3.14/180); y1=50*cos(o*3.14/180)+100*sin(o*3.14/180); x2=60*sin(o*3.14/180)-90*cos(o*3.14/180); y2=60*sin(o*3.14/180)+90*cos(o*3.14/180); axis(); printf("After Rotation about Z Axis"); bar3d(midx+x1,midy-y1,midx+x2,midy-y2,5,1); axis(); printf("After Rotation about X Axis"); bar3d(midx+50,midy-x1,midx+60,midy-x2,5,1); axis(); printf("After Rotation about Y Axis"); bar3d(midx+x1,midy-100,midx+x2,midy-90,5,1); getch(); closegraph(); }
CS 2405 COMPUTER GRAPHICS LABORATORY
78
BEFORE SCALING
AFTER SCALING
79
RESULT: Thus the above program has been executed and output is verified.
80
81
PROGRAM:
#include<iostream.h> #include<graphics.h> #include<math.h> #include<conio.h> #include<stdlib.h> class cube { public: void drawcube(int x1[],int y1[]) { int i; for(i=0;i<4;i++) { if(i<3) line(x1[i],y1[i],x1[i+1],y1[i+1]); line(x1[0],y1[0],x1[3],y1[3]); } for(i=4;i<8;i++) { if(i<7) line(x1[i],y1[i],x1[i+1],y1[i+1]); line(x1[4],y1[4],x1[7],y1[7]); } for(i=0;i<4;i++) { line(x1[i],y1[i],x1[i+4],y1[i+4]);
CS 2405 COMPUTER GRAPHICS LABORATORY
82
} } }; void main() { int i,x1[8],y1[8],x2[8],y2[8],z1[8],x3[8],y3[8],z3[8],x4[8],y4[8],theta,op,ch; int tx,ty,tz,sx,sy,sz,xf,yf,zf,x,y,z,size; int driver=DETECT; int mode; initgraph(&driver,&mode,"C:\\tc++\bgi"); cout<<"enter the points on the cube:"; cin>>x>>y>>z; cout<<"enter the size of the edge:"; cin>>size; x1[0]=x1[3]=x; x1[1]=x1[2]=x+size; x1[4]=x1[7]=x; x1[5]=x1[6]=x+size; y1[0]=y1[1]=y; y1[2]=y1[3]=y+size; y1[4]=y1[5]=y; y1[6]=y1[7]=y+size; z1[1]=z1[2]=z1[3]=z1[0]=z ; z1[4]=z1[5]=z1[6]=z1[7]=z-size; for(i=0;i<8;i++) { x2[i]=x1[i]+z1[i]/2; y2[i]=y1[i]+z1[i]/2;
CS 2405 COMPUTER GRAPHICS LABORATORY
83
} cube c; getch(); cleardevice(); do { cout<<"menu"<<endl; cout<<"\[Link]\[Link]\[Link]\[Link]\n"; cout<<"enter the choice:"; cin>>ch; switch(ch) { case 1: cout<<"enter the translation vector:"; cin>>tx>>ty>>tz; for(i=0;i<8;i++) { x3[i]=x1[i]+tx; y3[i]=y1[i]+ty; z3[i]=z1[i]+tz; } for(i=0;i<8;i++) { x4[i]=x3[i]+z3[i]/2; y4[i]=y3[i]+z3[i]/2; } cleardevice(); cout<<"before translation";
CS 2405 COMPUTER GRAPHICS LABORATORY
84
[Link](x2,y2); getch() cleardevice(); cout<<"after translation"; [Link](x4,y4); getch(); cleardevice(); break; case 2: cout<<"enter the rotation angle:"; cin>>theta; theta=(theta*3.14)/180; cout<<"enter the direction"<<endl; cout<<"[Link] about x axis"<<endl<<"[Link] about yaxis"<<endl<<"[Link] about z axis"; cin>>op; if(op==1) { for(i=0;i<8;i++) { x3[i]=x1[i]; y3[i]=y1[i]*cos(theta)-z1[i]*sin(theta); z3[i]=y1[i]*sin(theta)+z1[i]*cos(theta); } } Else if(op==2) {
CS 2405 COMPUTER GRAPHICS LABORATORY
85
for(i=0;i<8;i++) { y3[i]=y1[i]; x3[i]=z1[i]*cos(theta)-x1[i]*sin(theta); x3[i]=z1[i]*sin(theta)+x1[i]*cos(theta); } } Else if(op==3) { for(i=0;i<8;i++) { z3[i]=z1[i]; x3[i]=x1[i]*cos(theta)-y1[i]*sin(theta); y3[i]=x1[i]*sin(theta)+y1[i]*cos(theta); } } Else cout<<"enter correct option"; for(i=0;i<8;i++) { x4[i]=x3[i]+z3[i]/2; y4[i]=y3[i]+z3[i]/2; } cleardevice(); cout<<"before rotation"; [Link](x2,y2); getch();
CS 2405 COMPUTER GRAPHICS LABORATORY
86
cleardevice(); cout<<"after rotation"; [Link](x4,y4); getch(); cleardevice(); break; case 3: cout<<"enter the scaling factor:"; cin>>sx>>sy>>sz; cout<<"enter the reference point:"; cin>>xf>>yf>>zf; for(i=0;i<8;i++) { x3[i]=xf+(x1[i]*sx)+xf*(1-sx); y3[i]=yf+(y1[i]*sy)+yf*(1-sy); z3[i]=zf+(z1[i]*sz)+zf*(1-sz); } for(i=0;i<8;i++) { x4[i]=x3[i]+z3[i]/2; y4[i]=y3[i]+z3[i]/2; } cleardevice(); cout<<"before scaling"; [Link](x2,y2); getch(); cleardevice(); cout<<"after scaling";
CS 2405 COMPUTER GRAPHICS LABORATORY
87
88
OUTPUT:
Enter the points in the cube: 100 100 100 Enter the size of Edge : 50 MENU [Link] [Link] [Link] [Link] Enter your choice : 1 Enter the Translation Vector : 5 10 15
Before
After
89
Enter your Choice : 2 Rotation Enter the Rotation Angle : 60 Enter the Direction [Link] about x-axis [Link] about y-axis [Link] about z-axis
Before :
90
Enter your choice : 3 Enter the Scaling Factor : 30 40 Enter the Reference point : 20 35 50 45
BEFORE:
AFTER:
91
RESULT: Thus the above program has been executed and output is verified.
92
PROGRAM:
#include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> struct point { int x,y,z; }; struct edge {
CS 2405 COMPUTER GRAPHICS LABORATORY
93
struct point start; struct point end; }; float pi=3.14/180.0; void convert2d(int *x,int *y,int *z) { int xp,yp; xp=(*x)+(*z)*cos(pi*45)/tan(pi*45); yp=(*y)+(*z)*sin(pi*45)/tan(pi*45); *x=xp; *y=yp; } void screen(int *x,int *y) { int xm,ym; xm=getmaxx(); ym=getmaxy(); *x=xm/2+*x; *y=ym/2-*y; } void draw3d(struct edge po[],int n) { int i,x1,y1,z1,x2,y2,z2; for(i=0;i<n;i++) { x1=po[i].start.x; y1=po[i].start.y; z1=po[i].start.z;
CS 2405 COMPUTER GRAPHICS LABORATORY
94
convert2d(&x1,&y1,&z1); x2=po[i].end.x; y2=po[i].end.y; z2=po[i].end.z; convert2d(&x2,&y2,&z2); screen(&x1,&y1); screen(&x2,&y2); line(x1,y1,x2,y2); } } void main() { int gd=DETECT,gm=0; int i,tx,ty,tz,sx,sy,sz,n; int xx1,xx2,yy1,yy2; float rx,ry,rz; struct edge p[50],q[50],r[50],s[50],t[50],v[50]; initgraph(&gd,&gm,"c:\\tc\\bgi"); cleardevice(); printf("\nEnter the number of edges:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("\nStart pt for edge %d(x,y,z):",i+1); scanf("%d%d%d",&p[i].start.x,&p[i].start.y,&p[i].start.z); printf("\nEnd pt for edge %d(x,y,z):",i+1); scanf("%d%d%d",&p[i].end.x,&p[i].end.y,&p[i].end.z); } cleardevice();
CS 2405 COMPUTER GRAPHICS LABORATORY
95
printf("\n3D VIEW"); draw3d(p,n); getch(); cleardevice(); printf("\nTOP VIEW"); for(i=0;i<n;i++) { xx1=p[i].start.x; yy1=p[i].start.z; xx2=p[i].end.x; yy2=p[i].end.z; screen(&xx1,&yy1); screen(&xx2,&yy2); line(xx1,yy1,xx2,yy2); } getch(); cleardevice(); printf("\nSIDE VIEW"); for(i=0;i<n;i++) { xx1=p[i].start.z; yy1=p[i].start.y; xx2=p[i].end.z; yy2=p[i].end.y; screen(&xx1,&yy1); screen(&xx2,&yy2); line(xx1,yy1,xx2,yy2); }
CS 2405 COMPUTER GRAPHICS LABORATORY
96
getch(); cleardevice(); printf("\nFRONT VIEW"); for(i=0;i<n;i++) { xx1=p[i].start.x; yy1=p[i].start.y; xx2=p[i].end.x; yy2=p[i].end.y; screen(&xx1,&yy1); screen(&xx2,&yy2); line(xx1,yy1,xx2,yy2); } getch(); cleardevice(); }
97
OUTPUT:
Start pt for edge 1(x,y,z): End pt for edge 1(x,y,z): Start pt for edge 2(x,y,z): End pt for edge 2(x,y,z): Start pt for edge 3(x,y,z): End pt for edge 3(x,y,z): Start pt for edge 4(x,y,z): End pt for edge 4(x,y,z): Start pt for edge 5(x,y,z): End pt for edge 5(x,y,z): Start pt for edge 6(x,y,z): End pt for edge 6(x,y,z): Start pt for edge 7(x,y,z): End pt for edge 7(x,y,z): Start pt for edge 8(x,y,z): End pt for edge 8(x,y,z): Start pt for edge 9(x,y,z): End pt for edge 9(x,y,z): Start pt for edge 10(x,y,z): End pt for edge 10(x,y,z): Start pt for edge 11(x,y,z): End pt for edge 11(x,y,z): Start pt for edge 12(x,y,z): End pt for edge 12(x,y,z):
0 100 0 200 100 0 200 100 0 200 100 100 200 100 100 200 100 100 0 100 100 0 100 0 0 100 0 0 0 0
200 100 0 200 0 0 200 100 100 200 0 100 0 100 100 0 0 100
98
Output:
3D VIEW:
TOP VIEW:
SIDE VIEW:
99
FRONT VIEW:
RESULT: Thus the above program has been executed and output is verified.
100
EX NO : 10 (A) FRACTALS
101
PROGRAM:
#include<stdio.h> #include<conio.h> #include<stdlib.h> #include<graphics.h> #include<math.h> #define rads 560.274/*3.14/180*/ int x=380,y=450,numbranch=3; float spreadratio=0.8,lenratio=0.75; int main() { int gd=DETECT,gm; int drawtree(int x1,int y1,float a,float l,float f,int n); initgraph(&gd,&gm,"c:\\tcc\\bgi"); drawtree(x,y,280,350,180,27); getch(); closegraph(); restorecrtmode(); return 0;
CS 2405 COMPUTER GRAPHICS LABORATORY
102
} drawtree(int x1,int y1,float a,float l,float f,int n) { int i,num,x2,y2; float delang,ang; if(n>0) { x2=x1+l*cos(rads*a); y2=y1+l*sin(rads*a); setcolor(WHITE); line(x1,y1,x2,y2); num=numbranch; if(num>l) delang=f/(num-1.0); else delang=0.0; ang=a-f/2.0-delang; for(i=1;i<=num;i++) { ang+=delang; drawtree(x2,y2,ang,l*lenratio,f*spreadratio,n-1); } } else { setcolor(random(7)+1); ellipse(x2,y2,0,276,2,4);
103
fillellipse(x2,y2,2,4); } return 0; }
OUTPUT:
RESULT: Thus the C program for Fractals had been successfully executed and the output was verified.
104
EX NO: 10(B)
FRACTAL TREE
105
PROGRAM: #include<stdio.h> #include<conio.h> #include<stdlib.h> #include<graphics.h> #include<math.h> #define rads 0.0174/*3.14/180*/ int x=280,y=350,numbranch=3; float spreadratio=0.8,lenratio=0.75; main() { int gd=DETECT,gm; int drawtree(int x1,int y1,float a,float l,float f,int n); initgraph(&gd,&gm,"c:\\tcc\\bgi"); drawtree(x,y,270,75,30,7); getch(); closegraph();
106
restorecrtmode(); return 0; } drawtree(int x1,int y1,float a,float l,float f,int n) { int i,num,x2,y2; float delang,ang; if(n>0) { switch(n) { case 1: setcolor(LIGHTGREEN); break;
107
setcolor(DARKGRAY); break; } x2=x1+l*cos(rads*a); y2=y1+l*sin(rads*a); for(i=0;i<n;i++) { line(x1+i,y1,x2+i,y2); line(x1-i,y1,x2-i,y2); } num=random(6)+1; if(num>l) delang=f/(num-1.0); else delang=0.0; ang=a-f/2.0-delang; for(i=1;i<=num;i++) { ang+=delang; drawtree(x2,y2,ang+10,l*lenratio,1.0*random(90)+1,-1); }} else { setfillstyle(SOLID_FILL,random(15)); fillellipse(x1,y1,3,6); } return 0; }
CS 2405 COMPUTER GRAPHICS LABORATORY
108
OUTPUT:
RESULT: Thus the C program for Fractals Tree had been successfully executed and the output was verified.
109
110