Cs2405 Computer Graphics Laboratory L T
Cs2405 Computer Graphics Laboratory L T
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
int gd=DETECT,gm;
int r,x,y,p,xc=320,yc=240;
initgraph(&gd,&gm,"");
scanf("%d",&r);
x=0;
y=r;
putpixel(xc+x,yc-y,1);
p=3-(2*r);
for(x=0;x<=y;x++)
if(p<0)
y=y;
p=p+(4*x)+6;
else
y=y-1;
p=p+((4*(x-y)+10));
putpixel(xc+x,yc-y,1);
putpixel(xc-x,yc-y,2);
putpixel(xc+x,yc+y,3);
putpixel(xc-x,yc+y,4);
putpixel(xc+y,yc-x,5);
putpixel(xc-y,yc-x,6);
putpixel(xc+y,yc+x,7);
putpixel(xc-y,yc+x,8);
getch();
closegraph();
}
1. C. Ellipse Algorithm
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
long d1,d2;
int i,gd=DETECT,gm,x,y;
long rx,ry,rxsq,rysq,tworxsq,tworysq,dx,dy;
scanf("%d %d",&rx,&ry);
initgraph(&gd,&gm,"");
rxsq=rx*rx;
rysq=ry*ry;
tworxsq=2*rxsq;
tworysq=2*rysq;
x=0;
y=ry;
d1=rysq-rxsq*ry+(0.25*rxsq);
dx=tworysq*x;
dy=tworxsq*y;
do
putpixel(200+x,200+y,15);
putpixel(200-x,200-y,15);
putpixel(200+x,200-y,15);
putpixel(200-x,200+y,15);
if(d1<0)
x=x+1;
y=y;
dx=dx+tworysq;
d1=d1+dx+rysq;
else
x=x+1;
y=y-1;
dx=dx+tworysq;
dy=dy-tworxsq;
d1=d1+dx-dy+rysq;
while(dx<dy);
d2=rysq*(x+0.5)*(x+.5)+rxsq*(y-1)*(y-1)-rxsq*rysq;
do
putpixel(200+x,200+y,15);
putpixel(200-x,200-y,15);
putpixel(200+x,200-y,15);
putpixel(200-x,200+y,15);
if(d2>0)
x=x;
y=y-1;
dy=dy-tworxsq;
d2=d2-dy+rxsq;
else
x=x+1;
y=y-1;
dy=dy-tworxsq;
dx=dx+tworysq;
d2=d2+dx-dy+rxsq;
}while(y>0);
getch();
closegraph();
}
2. Implementation of Line, Circle and ellipse Attributes.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<string.h>
void main()
char ch='y';
int gd=DETECT,gm,x1,y1,x2,y2,rad,sa,ea,xrad,yrad,i;
initgraph(&gd,&gm,"");
while(ch=='y')
cleardevice();
setbkcolor(9);
outtextxy(100,150,"1.Line");
outtextxy(100,170,"2.Circle");
outtextxy(100,190,"3.Square");
outtextxy(100,210,"4.Arc");
outtextxy(100,230,"5.Ellipse");
outtextxy(100,250,"6.Rectangle");
outtextxy(100,270,"7.Exit");
ch=getch();
cleardevice();
switch(ch)
case 1:
line(100,200,300,400);
break;
case 2:
circle(200,200,100);
break;
case 3:
setfillstyle(5,4);
bar(100,300,200,400);
break;
case 4:
setfillstyle(5,4);
arc(200,200,100,300,100);
break;
case 5:
setfillstyle(5,4);
fillellipse(100,100,50,100);
break;
case 6:
settextstyle(DEFAULT_FONT,0,2);
outtextxy(120,140,"BIE");
line(100,100,100,300);
line(300,300,100,300);
line(100,100,300,100);
line(300,100,300,300);
break;
case 7:
closegraph();
return;
}
ch='y';
getch();
}
3. Two Dimensional transformations - Translation, Rotation, Scaling, Reflection, Shear.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void translate();
void scale();
void rotate();
void main()
int ch,gd=DETECT,gm;
initgraph(&gd,&gm,"");
do{
setbkcolor(2);
setcolor(9);
rectangle(100,150,150,100);
scanf("%d",&ch);
cleardevice();
switch(ch)
case 1:
translate();
break;
case 2:
scale();
break;
case 3:
rotate();
break;
case 4:
break;
}while(ch!=4);
getch();
closegraph();
void translate()
int tx,ty;
setcolor(2);
scanf("%d %d",&tx,&ty);
cleardevice();
rectangle(100,150,150,100);
rectangle(100+tx,150+ty,150+tx,100+ty);
void scale()
int sx,sy;
setcolor(2);
printf("\nEnter sx,sy:");
rectangle(100,150,150,100);
printf("\nAfter Scaling:");
rectangle(100*sx,150*sy,150*sx,100*sy);
void rotate()
float theta;
int x1,x2,x3,x4,y1,y2,y3,y4;
int ax1,ax2,ax3,ax4,ay1,ay2,ay3,ay4,refx,refy;
scanf("%f",&theta);
theta=theta*(3.14/180);
cleardevice();
setcolor(2);
refx=100;refy=100;
x1=100;y1=100;
x2=150;
y2=100;
x3=150;
y3=150;
x4=100;
y4=150;
ax1=refy+(x1-refx)*cos(theta)-(y1-refy)*sin(theta);
ay1=refy+(x1-refx)*sin(theta)+(y1-refy)*cos(theta);
ax2=refy+(x2-refx)*cos(theta)-(y2-refy)*sin(theta);
ay2=refy+(x2-refx)*sin(theta)+(y2-refy)*cos(theta);
ax3=refy+(x3-refx)*cos(theta)-(y3-refy)*sin(theta);
ay3=refy+(x3-refx)*sin(theta)+(y3-refy)*cos(theta);
ax4=refy+(x4-refx)*cos(theta)-(y4-refy)*sin(theta);
ay4=refy+(x4-refx)*sin(theta)+(y4-refy)*cos(theta);
rectangle(100,150,150,100);
line(ax1,ay1,ax2,ay2);
line(ax2,ay2,ax3,ay3);
line(ax3,ay3,ax4,ay4);
line(ax4,ay4,ax1,ay1);
}
4. Composite 2D Transformations.
5. Cohen Sutherland 2D line clipping and Windowing
6. Sutherland – Hodgeman Polygon clipping Algorithm.
7. Three dimensional transformations - Translation, Rotation, Scaling.
8. Composite 3D transformations.
9. Drawing three dimensional objects and Scenes.
10. Generating Fractal images.