SlideShare uma empresa Scribd logo
1 de 44
GOVT. ENGINEERING COLLEGE, BILASPUR
Computer Graphics labs File
Name : AalhaRam
Branch: Comuter science &engg.(6th
sem)
Roll No. : 3072214301
List of Experiments:
1. Write a programto draw the line using DDA algorithm.
2. Write a programto draw the line using Bresenham’s algorithm.
3. Write a programto draw circle using Bresenham’s algorithm.
4. Write a programto draw circle using mid-point algorithm.
5. Write a programto demonstratedraw ellipse using midpoint algorithm.
6. Write a programRotation of Triangle.
7. Write a programTranslation of Line.
8. Write a programto performscaling of line.
9. Write a programshearing of Rectangle.
10. Write a programto implement boundary –fill algorithm.
11. Write a programto implement flood –fill algorithm.
12. Write a programto implement Bezier curve using four control points.
13. Write a programto implement CohenSutherland line clipping algorithm.
14. Write a programto implement Liang Barsky line clipping algorithm.
15. Write a programto implement face of a cartoon.
EXPERIMENT-1
1. Write aprogram to draw the line using DDA algorithm.
CODING:
#include <graphics.h>/*include the necessary header files*/
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
void draw(intxa,intya,intxb,intyb);
void main()
{
intxa,ya,xb,yb;
clrscr();
printf("LineDDA algorithm");
printf("n Enter the value of xa, ya:");
scanf("%d%d",&xa,&ya);
printf("n Enter the value of xb, yb:");
scanf("%d%d",&xb,&yb);
draw(xa,ya,xb,yb);
}
void draw(intxa,intya,intxb,intyb)
{
intxin,yin,x,y,dx,dy,steps,k; /*request autodetection*/
intgdriver=DETECT,gmode,errorcode; /*initialize graphics andlocal
variables */
initgraph(&gdriver,&gmode, "c:tcbgi") /* readresult of initialization*/
errorcode=graphresult(); /*an error occurred*/
if (errorcode!=grOk)
{
printf("Graphics error: %sn", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
dx=xb-xa;
dy=yb-ya;
if(abs(dx)>abs(dy)) /*if the conditionis satisfied*/
{ /* calculate the value of the conditionvariable*/
steps=abs(dx);
}
else
{
steps=abs(dy);
}
xin=dx/steps;
yin=dy/steps;
x=xa;
y=ya;
putpixel(x,y,1); /* draw the first pixel for the line*/
for(k=1;k<=steps;k++) /*for each value of the conditionvariable, */
{
x=x+xin; /* calculate the values of (x,y) and draw the pixel*/
y=y+yin;
putpixel(x,y,1);
} /* cleanup */
getch();
closegraph();
}
Output:
EXPERIMENT-2
2. Write aprogram to draw the line using Bresenham’s algorithm.
CODING:
#include <graphics.h>/*include the necessary header files*/
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
void draw(intxa, intya, intxb, intyb);
void main()
{
intxa, ya, xb, yb;
clrscr();
printf("Bresenhnams algorithm"); /*get the coordinates of the line*/
printf("n Enter the value of xa, ya:");
scanf("%d%d",&xa,&ya);
printf("n Enter the value of xb, yb:");
scanf("%d%d",&xb,&yb);
draw(xa,ya,xb,yb);
}
void draw(intxa, intya, intxb, intyb)
{
intx,y,dx,dy,xend,p; /*request autodetection*/
intgdriver=DETECT,gmode,errorcode; /*initialize graphics andlocal
variables */
initgraph(&gdriver,&gmode,"c:tcbgi"); /*readresult of initialization*/
errorcode=graphresult(); /*an error occurred*/
if(errorcode!=grOk)
{
printf("Graphics error: %sn", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
dx=xb-xa;
dy=yb-ya;
p=2*dy-dx; /* calculate the value of the conditionvariable*/
if(xa>xb) /* depending on the positionof the coordinates*/
{
x=xb; /* assignthe values for (x,y)*/
y=yb;
xend=xa;
}
else if(xb>xa)
{
x=xa;
y=ya;
xend=xb;
}
putpixel(x,y,1); /* draw the pixel on the screen*/
while(x<xend) /* depending onthe control conditiondraw the pixels*/
{
x=x+1;
if(p<0)
{
p=p+2*dy;
}
else
{
y=y+1;
p=p+2*dy;
}
putpixel(x,y,1);
} /* cleanup */
getch();
closegraph();
}
OUTPUT:
EXPERIMENT NO. 03
3 :Write aprogram to implement Bresenham’s Circlealgorithm.
PROGRAM
# include <stdio.h>
# include <conio.h>
# include <graphics.h>
void circle ( int , int ) ;
void main ( )
{
intx , y , p , r , i ;
intgd=DETECT , gm ;
initgraph( &gd , &gm , “tcbgi”) ;
setbkcolor (WHITE) ;
printf( “Enter the radius = “) ;
scanf ( “%d” , &r) ;
x = 0 ; y = r ;
p = 3 – 2 * r ;
putpixel( x , y , RED) ;
while ( x< = y )
{
if (p < 0)
p + = 4 * x + 6;
else
{
p + = 4 * ( x - y) + 10 ;
y - - ;
}
x ++ ;
circle p ( x , y) ;
}
getch( ) ;
closegraph( ) ;
}
void circle p ( int a , int b)
{
intx , y ;
x = a ; y = b ;
putpixel ( 300+x , 300 + y , 1 ) ;
putpixel( 300 + y , 300 + x , 1 ) ;
putpixel( 300 - x , 300 + y , 1 ) ;
putpixel( 300 + x , 300 - y , 1 ) ;
putpixel( 300 - x , 300 - y , 1 ) ;
putpixel( 300 - y , 300 - x , 1 ) ;
putpixel( 300 + y , 300 - x , 1 ) ;
putpixel( 300 - y , 300 + x , 1 ) ;
}
getch( ) ;
}
OUTPUT:
EXPERIMENT-4
4. Write aprogram to draw circle using mid-point algorithm.
CODING:
#include<stdio.h>/* include the necessary header files*/
#include<conio.h>
#include<math.h>
#include<graphics.h>
main()
{
intgd=DETECT,gin;
intxcenter,ycenter,radius;
intp,x,y,twox,twoy; /*request autodetect*/
initgraph(&gd,&gin,"C:tcbgi");
x=0;
printf("nEnter the radius value:"); /* get the value of the radius and
center values*/
scanf("%d",&radius);
printf("Enter the center values:");
scanf("%d %d",&xcenter,&ycenter);
plotpoints(xcenter,ycenter,x,y); /*call the plotpoints function*/
y=radius;
p=1-radius;
twox=2*x;
twoy=2*y;
printf("nptxtyt2xt2yn");
printf("n%dt%dt%dt%dt%dn",p,x,y,twox,twoy);
while(x<y) /* in the conditional loopcompute the value of the x and y
values*/
{
if(p<0)
x=x+1;
else
{
x=x+1;
y=y-1;
}
if(p<0)
p=p+2*x+1;
else
p=p+2*(x-y)+1;
twox=2*x;
twoy=2*y;
printf("n%dt%dt%dt%dt%dn",p,x,y,twox,twoy);
plotpoints(xcenter,ycenter,x,y);
}
getch();
return 0;
}
intplotpoints(intxcenter, intycenter,intx,int y) /* plot the points of the
circle as per the procedure*/
{
putpixel(xcenter+x,ycenter+y,1);
putpixel(xcenter-x,ycenter+y,1);
putpixel(xcenter+x,ycenter-y,1);
putpixel(xcenter-x,ycenter-y,1);
putpixel(xcenter+y,ycenter+x,1);
putpixel(xcenter-y,ycenter+x,1);
putpixel(xcenter+y,ycenter-x,1);
putpixel(xcenter-y,ycenter-x,1);
}
OUTPUT:
EXPERIMENT-5
5. Write aprogram to draw ellipse using mid-point algorithm.
CODING:
#include<stdio.h>/* include the necessary header files*/
#include<conio.h>
#include<graphics.h>
include<math.h>
#include<stdlib.h>
void plotpoints(int,int,int,int);
void main()
{
intgd=DETECT,gm;
intxcenter,ycenter,rx,ry;
intp,x,y,px,py,rx1,ry1,rx2,ry2;
initgraph(&gd,&gm,"C:TCBGI"); /*request autodetect*/
printf("n Enter the radius :"); /* get the radius and the center values*/
scanf("%d %d",&rx,&ry);
printf("n Enter the xcenter and ycenter values :");
scanf("%d %d",&xcenter,&ycenter);
ry1=ry*ry;
rx1=rx*rx;
ry2=2*ry1;
rx2=2*rx1;
/* Region1 */
x=0;
y=ry;
plotpoints(xcenter,ycenter,x,y); /*for the first region calculate the
conditionparameter*/
p=(ry1-rx1*ry+(0.25*rx1));
px=0;
py=rx2*y;
printf("nxtytptpxtpyn");
printf("n%dt%dt%dt%dt%d",x,y,p,px,py);
while(px<py) /* if this conditionis true, compute values of x and y*/
{
x=x+1;
px=px+ry2;
if(p>=0)
{
y=y-1;
py=py-rx2;
p=p+ry1+px-py;
}
else
p=p+ry1+px;
plotpoints(xcenter,ycenter,x,y); /*call the plotpoints function*/
printf("n%dt%dt%dt%dt%d",x,y,p,px,py);
}
/* Region2 */
printf("n%dt%dt%dt%dt%d",x,y,p,px,py);
printf("nnRegion 2n");
printf("nxtytptpxtpyn"); /* for region2 recalculate the condition
variables*/
p=(ry1*(x+0.5)*(x+0.5)+rx1*(y-1)*(y-1)-rx1*ry1);
while(y>0)
{
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); /*draw the pixels for region2*/
printf("n%dt%dt%dt%dt%d",x,y,p,px,py);
}
getch();
closegraph();
}
void plotpoints(intxcenter,intycenter,intx,inty) /* plot the points of the
circle as per the procedure*/
{
putpixel(xcenter+x,ycenter+y,6);
putpixel(xcenter-x,ycenter+y,6);
putpixel(xcenter+x,ycenter-y,6);
putpixel(xcenter-x,ycenter-y,6);
}
Ouput:
EXPERIMENT-6
6. Write aprogram Rotationof Triangle.
CODING:
#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
int x1,x2,x3,y1,y2,y3,t,tx,sx,sy,shx,shy,ch;
float rx1,rx2,rx3,ry1,ry2,ry3;
float ang,theta;
int main(void)
{
intgdriver = DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode,"C:TCBGI"); /*request for auto
detection*/
errorcode= graphresult();
if(errorcode!= grOk) /* if error occours*/
{
printf("Graphics error: %sn", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
else
{
do{
printf("n1.Translationn2.Reflectionn3.Rotationn4.Scalingn5.Shearingn
");
printf("nEnter Your choice"); /* get the choice from the user*/
scanf("%d",&ch);
switch(ch)
{
printf("n Enter all coordinates values :");
scanf("%d %d %d %d %d %d",&x1,&y1,&x2,&y2,&x3,&y3);
printf("n Before Rotation "); /* get the original coordinates*/
line(x1,y1,x2,y2);
line(rx2,ry2,rx3,ry3);
line(rx3,ry3,rx1,ry1);
}
getch();
closegraph(); /* close the graph*/
return 0;
}
OUTPUT
EXPERIMENT-7
7. Write a program Translation of Line.
CODE
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
#include<process.h>
void draw();
void line();
int x1,x2,y1,y2;
void main(){
intgd=DETECT,gm;
initgraph(&gd,&gm,"..//bgi");
printf("n Enter the first point:");
scanf("%d%d",&x1,&y1);
printf("n Enter the Second point:");
scanf("%d%d",&x2,&y2);
cleardevice();
draw();
line();
getch();
}
void draw()
{
line(x1,y1,x2,y2);
}
void line()
{
intx,y,a1,a2,b1,b2;
printf("Enter the translation vectors:");
scanf("%d%d",&x,&y);
cleardevice();
a1=x1+x;
b1=y1+y;
a2=x2+x;
b2=y2+y;
line(a1,b1,a2,b2);
}
OUTPUT
EXPERIMENT-8
8. Write a program to perform scaling ofline.
CODE
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
#include<process.h>
void draw();
void tri();
int x1,x2,x3,y3,y1,y2,mx,my;
void main(){
intgd=DETECT,gm;
initgraph(&gd,&gm,"..//bgi");
printf("n Enter the firstpoint for the triangle:");
scanf("%d%d",&x1,&y1);
printf("n Enter the Second point for the
triangle:");
scanf("%d%d",&x2,&y2);
printf("n Enter the Third point for the
triangle:");
scanf("%d%d",&x3,&y3);
cleardevice();
draw();
scale();
getch();
}
void draw()
{
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
}
void scale()
{
intx,y,a1,a2,b1,b2,a3,b3;
intmx,my;
printf("Enter the scaling coordinates:");
scanf("%d%d",&x,&y);
mx=(x1+x2+x3)/3;
my=(y1+y2+y3)/3;
cleardevice();
a1=mx+(x1-mx)*x;
b1=my+(y1-my)*y;
a2=mx+(x2-mx)*x;
b2=my+(y2-my)*y;
a3=mx+(x3-mx)*x;
b3=my+(y3-my)*y;
line(a1,b1,a2,b2);
line(a2,b2,a3,b3);
line(a3,b3,a1,b1);
draw();
}
OUTPUT
EXPERIMENT-9
9. Write a program shearing of Rectangle.
CODE
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int x1,y1,x2,y2,x,y;
int main(void){
intgd=DETECT,gm;
clrscr();
initgraph(&gd,&gm,"..//bgi");
printf("ntENter the top left coordinate:");
scanf("%d%d",&x1,&y1);
printf("ntENter the bottom right coordinate:");
scanf("%d%d",&x2,&y2);
printf("n Enter the value of shearing coordinate for x shear:n");
scanf("%d",&x);
printf("n Enter the value of shearing coordinate for y shear:n");
scanf("%d",&y);
cleardevice();
rectangle(x1,y1,x2,y2);
printf("n Now press a key to see shear in x-axis");
getch();
rectangle(x1,y1,x2*x,y2);
printf("n Now press a key to see shear in y-axis");
rectangle(x1,y1,x2,y2*y);
getch();
closegraph();
getch();
}
Output:
EXPERIMENT-10
10.Write aprogramto implement boundary –fill algorithm
include<graphics.h>
#include<dos.h>
void fill_right(intx,int y);
void fill_left(intx,int y);
void main()
{
intgd=DETECT,gm,x,y,n,i;
clrscr();
initgraph(&gd,&gm,"c:turboc3bgi");
printf("*** Boundary Fill algorithm ***");
line (50,50,200,50);
line (200,50,200,300);
line (200,300,50,300);
line (50,300,50,50);
x=100; y=100;
fill_right(x,y);
fill_left(x-1,y);
getch();
}
void fill_right(intx,int y)
{
{
if((getpixel(x,y) != WHITE)&&(getpixel(x,y) !=RED)
{
putpixel(x,y,RED);
fill_right(++x,y); x=x-1;
fill_right(x,y-1);
fill_right(x,y+1);
}
delay(1);
}
void fill_left(intx,int y)
{
if((getpixel(x,y) != WHITE)&&(getpixel(x,y) !=RED))
{
putpixel(x,y,RED);
fill_left(--x,y); x=x+1;
fill_left(x,y-1);
fill_left(x,y+1);
}delay(1);
OUTPUT
EXPERIMENT-11
11. Write a program toimplement flood–fill algorithm.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void boundfill(intxc,intyc,intr,intb)
{
int cur;
cur=getpixel(xc,yc);
if(cur!=b && cur!=r)
{
putpixel(xc,yc,r);
delay(1);
boundfill(xc+1,yc,r,b);
boundfill(xc-1,yc,r,b);
boundfill(xc,yc+1,r,b);
boundfill(xc,yc-1,r,b);
}
}
void main()
{
intgd=DETECT,gm;
initgraph(&gd,&gm,"..bgi");
rectangle(100,100,300,300);
boundfill(105,105,4,WHITE);
getch();
closegraph();
}
getch();
}
OUTPUT:
EXPERIMENT-12
12. Write a program toimplement Bezier curve using four control points.
#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>
#include <math.h>
void bezier (intx[4], int y[4])
{
intgd = DETECT, gm; inti;
double t;
initgraph (&gd, &gm, "..bgi");
for (t = 0.0; t < 1.0; t += 0.0005)
{
double xt = pow (1-t, 3) * x[0] + 3 * t * pow (1-t, 2) * x[1] + 3 * pow (t, 2) *
(1-t) * x[2] + pow (t, 3) * x[3];
double yt = pow (1-t, 3) * y[0] + 3 * t * pow (1-t, 2) * y[1] + 3 * pow
(t, 2) * (1-t) * y[2] + pow (t, 3) * y[3];
putpixel (xt, yt, WHITE);
}
for (i=0; i<4; i++)
putpixel (x[i], y[i], YELLOW);
getch();
closegraph();
return;
}
void main()
{
intx[4], y[4]; inti;
printf ("Enter the x- and y-coordinates of the four control points.n");
for (i=0; i<4; i++) scanf ("%d%d", &x[i], &y[i]); bezier (x, y);
}
closegraph();
}
getch();
}
OUTPUT
EXPERIMENT-13
13. Write a program toimplement CohenSutherlandline clipping
algorithm.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void clip(float,float,float);
inti,j=0,n;
int rx1,rx2,ry1,ry2;
float x1[8],y1[8];
void main()
{
intgd=DETECT,gm;
inti,n;
float x[8],y[8],m;
clrscr();
initgraph(&gd,&gm,"");
printf("coordinates for rectangle : ");
scanf("%d%d%d%d",&rx1,&ry1,&rx2,&ry2);
printf("no. of sides for polygon : ");
scanf("%d",&n);
printf("coordinates : ");
for(i=0;i<n;i++)
{
scanf("%f%f",&x[i],&y[i]);
}
cleardevice();
outtextxy(10,10,"Beforeclipping");
outtextxy(10,470,"Pressany key....");
rectangle(rx1,ry1,rx2,ry2);
for(i=0;i<n-1;i++)
line(x[i],y[i],x[i+1],y[i+1]);
line(x[i],y[i],x[0],y[0]);
getch();
cleardevice();
for(i=0;i<n-1;i++)
{
m=(y[i+1]-y[i])/(x[i+1]-x[i]);
clip(x[i],y[i],m);
}
clip(x[0],y[0],m);
outtextxy(10,10,"After clipping");
outtextxy(10,470,"Pressany key....");
rectangle(rx1,ry1,rx2,ry2);
for(i=0;i<j-1;i++)
line(x1[i],y1[i],x1[i+1],y1[i+1]);
getch();
}
void clip(float e,floatf,floatm)
{
while(e<rx1 e>rx2 f<ry1 f>ry2)
{
if(e<rx1)
{
f+=m*(rx1-e);
e=rx1;
}
else if(e>rx2)
{
f+=m*(rx2-e);
e=rx1;
}
if(f<ry1)
{
e+=(ry1-f)/m;
f=ry1;
}
else if(f>ry2)
{
e+=(ry2-f)/m;
f=ry2;
}
x1[j]=e;
y1[j]=f;
j++;
}
}
OUTPUT
EXPERIMENT-14
14. Write a program toimplement Liang Barsky line clipping algorithm.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
Void main
{
float x1,y1,x2,y2,xmin,xmax,ymin,ymax,dx,dy;
float p[4],q[4],r[4];
float max,min,u1,u2;
float xi,xii,yi,yii;
int gd,gm,i;
gd=DETECT;
initgraph(&gd,&gm,"c:tcbgi");
clrscr();
printf("n enter the line co-ordinates");
printf("n enter 1stx=");
scanf("%f",&x1);
printf("t1sty=");
scanf("%f",&y1);
printf("n enter 2nd x=");
scanf("%f",&x2);
printf("t2nd y=");
scanf("%f",&y2);
printf("n enter window boundry");
printf("n xmin=");
scanf("%f",&xmin);
printf("n ymin=");
scanf("%f",&ymin);
printf("n xmax=");
scanf("%f",&xmax);
printf("n ymax=");
scanf("%f",&ymax)
dx=x2-x1;
dy=y2-y1;
cleardevice();
line(x1,y1,x2,y2);
rectangle(xmin,ymin,xmax,ymax);
p[0]=-dx;
q[0]=x1-xmin;
p[1]=dx;
q[1]=xmax-x1;
p[2]=-dy;
p[2]=-dy;
q[2]=y1-ymin;
p[3]=dy;
q[3]=ymax-y1;
for(i=0;i<4;i++)
{
if(p[i]==0 && q[i]<0)
{
printf("Lineis outside the boundry,itis not a clipping candidaten");
getch();
exit(0);
}
}
for(i=0;i<4;i++)
{
r[i]=q[i]/p[i];
printf("n r[%d]=%f",i,r[i]);
}
max=0;min=1;
for(i=0;i<4;i++)
if(p[i]<0)
{
if(r[i]>max)
max=r[i];
}
else
{
if(r[i]<min)
min=r[i];
}
u1=max;
u2=min;
printf("n u1=%f",u1);
printf("n u2=%f",u2);
if(u1>u2)
{
printf("n line is completely outside");
getch();
exit(0);
}
xi=x1+(u1*dx);
yi=y1+(u1*dy);
xii=x1+(u2*dx);
yii=y1+(u2*dy);
rectangle (xmin, ymin, xmax, ymax);
sector(5);
line(xi,yi,xii,yii);
getch();
closegraph();
}
EXPERIMENT-15
15. Write a program toimplement face of a cartoon.
# include <graphics.h>
# include<conio.h>
#include <stdlib.h>
main()
{
intgd= DETECT, gm,area, temp1, temp2, left=25, top=75;
void*p;
initgraph(&gd, &gm,”C:TCBGI”);
Setcolor(YELLOW);
Circle(50, 100,25);
Sefillstyle(SOLID__FILL,YELLOW);
floodfill(50,100, YELLOW);
Setcolor(BLACK);
Sefillstyle(SOLID__FILL,BLACK);
fillellipse(44, 85, 2, 6);
fillellipse(56, 85, 2, 6);
ellipse(50, 100, 205, 335, 20,9);
ellipse(50, 100, 205, 335, 20,10);
ellipse(50, 100, 205, 335, 20,11);
area= imagesize(left, top, left+ 50, top+50);
p= malloc(area);
setcolor(WHITE);
settextstyle(SANS_SERIF_FONT, HORIZ_DIR,2);
outtextxy(155, 451, “Smiling Face Animation”);
setcolor(BLUE);
rectangle(0,0 , 639,449);
while(!kbhit())
{
Temp1= 1+ randam(588);
{
Temp1= 1+ randam(380);
{
Getimage(left, top, left+ 50, top+ 50);
putimage(left, top, p, XOR_PUT);
putimage(temp1, temp2, p, XOR_PUT);
delay(100);
left= temp1;
top = temp2;
}
getch();
closegraph();
return();
}
OUTPUT

Mais conteúdo relacionado

Mais procurados

Cn os-lp lab manual k.roshan
Cn os-lp lab manual k.roshanCn os-lp lab manual k.roshan
Cn os-lp lab manual k.roshan
riturajj
 

Mais procurados (19)

VTU Data Structures Lab Manual
VTU Data Structures Lab ManualVTU Data Structures Lab Manual
VTU Data Structures Lab Manual
 
Data structures lab manual
Data structures lab manualData structures lab manual
Data structures lab manual
 
How to add an optimization for C# to RyuJIT
How to add an optimization for C# to RyuJITHow to add an optimization for C# to RyuJIT
How to add an optimization for C# to RyuJIT
 
Egor Bogatov - .NET Core intrinsics and other micro-optimizations
Egor Bogatov - .NET Core intrinsics and other micro-optimizationsEgor Bogatov - .NET Core intrinsics and other micro-optimizations
Egor Bogatov - .NET Core intrinsics and other micro-optimizations
 
C program
C programC program
C program
 
Data Structures Practical File
Data Structures Practical File Data Structures Practical File
Data Structures Practical File
 
Computer graphics lab manual
Computer graphics lab manualComputer graphics lab manual
Computer graphics lab manual
 
ADA FILE
ADA FILEADA FILE
ADA FILE
 
Introduction to graphics programming in c
Introduction to graphics programming in cIntroduction to graphics programming in c
Introduction to graphics programming in c
 
C++ file
C++ fileC++ file
C++ file
 
Bartosz Milewski, “Re-discovering Monads in C++”
Bartosz Milewski, “Re-discovering Monads in C++”Bartosz Milewski, “Re-discovering Monads in C++”
Bartosz Milewski, “Re-discovering Monads in C++”
 
C graphics programs file
C graphics programs fileC graphics programs file
C graphics programs file
 
Rainer Grimm, “Functional Programming in C++11”
Rainer Grimm, “Functional Programming in C++11”Rainer Grimm, “Functional Programming in C++11”
Rainer Grimm, “Functional Programming in C++11”
 
Data Structures Using C Practical File
Data Structures Using C Practical File Data Structures Using C Practical File
Data Structures Using C Practical File
 
SPL 6.1 | Advanced problems on Operators and Math.h function in C
SPL 6.1 | Advanced problems on Operators and Math.h function in CSPL 6.1 | Advanced problems on Operators and Math.h function in C
SPL 6.1 | Advanced problems on Operators and Math.h function in C
 
Cn os-lp lab manual k.roshan
Cn os-lp lab manual k.roshanCn os-lp lab manual k.roshan
Cn os-lp lab manual k.roshan
 
Histogram dan Segmentasi 2
Histogram dan Segmentasi 2Histogram dan Segmentasi 2
Histogram dan Segmentasi 2
 
Practical File of C Language
Practical File of C LanguagePractical File of C Language
Practical File of C Language
 
algorithm design file
algorithm design filealgorithm design file
algorithm design file
 

Semelhante a Computer graphics

1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
rushabhshah600
 
Cg my own programs
Cg my own programsCg my own programs
Cg my own programs
Amit Kapoor
 

Semelhante a Computer graphics (20)

Computer graphics lab manual
Computer graphics lab manualComputer graphics lab manual
Computer graphics lab manual
 
Computer graphics
Computer graphics   Computer graphics
Computer graphics
 
Computer graphics
Computer graphics   Computer graphics
Computer graphics
 
Computer graphics lab assignment
Computer graphics lab assignmentComputer graphics lab assignment
Computer graphics lab assignment
 
Assignment on Numerical Method C Code
Assignment on Numerical Method C CodeAssignment on Numerical Method C Code
Assignment on Numerical Method C Code
 
Struct examples
Struct examplesStruct examples
Struct examples
 
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
 
C++ TUTORIAL 9
C++ TUTORIAL 9C++ TUTORIAL 9
C++ TUTORIAL 9
 
Cg my own programs
Cg my own programsCg my own programs
Cg my own programs
 
Computer graphics lab report with code in cpp
Computer graphics lab report with code in cppComputer graphics lab report with code in cpp
Computer graphics lab report with code in cpp
 
Cs580
Cs580Cs580
Cs580
 
SCIPY-SYMPY.pdf
SCIPY-SYMPY.pdfSCIPY-SYMPY.pdf
SCIPY-SYMPY.pdf
 
Histogram dan Segmentasi
Histogram dan SegmentasiHistogram dan Segmentasi
Histogram dan Segmentasi
 
Chapter 7 functions (c)
Chapter 7 functions (c)Chapter 7 functions (c)
Chapter 7 functions (c)
 
Python과 node.js기반 데이터 분석 및 가시화
Python과 node.js기반 데이터 분석 및 가시화Python과 node.js기반 데이터 분석 및 가시화
Python과 node.js기반 데이터 분석 및 가시화
 
DSC program.pdf
DSC program.pdfDSC program.pdf
DSC program.pdf
 
ML with python.pdf
ML with python.pdfML with python.pdf
ML with python.pdf
 
Computer graphics programs in c++
Computer graphics programs in c++Computer graphics programs in c++
Computer graphics programs in c++
 
C++ file
C++ fileC++ file
C++ file
 
C++ file
C++ fileC++ file
C++ file
 

Último

Último (20)

UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and Properties
 
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGMANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 

Computer graphics

  • 1. GOVT. ENGINEERING COLLEGE, BILASPUR Computer Graphics labs File Name : AalhaRam Branch: Comuter science &engg.(6th sem) Roll No. : 3072214301
  • 2. List of Experiments: 1. Write a programto draw the line using DDA algorithm. 2. Write a programto draw the line using Bresenham’s algorithm. 3. Write a programto draw circle using Bresenham’s algorithm. 4. Write a programto draw circle using mid-point algorithm. 5. Write a programto demonstratedraw ellipse using midpoint algorithm. 6. Write a programRotation of Triangle. 7. Write a programTranslation of Line. 8. Write a programto performscaling of line. 9. Write a programshearing of Rectangle. 10. Write a programto implement boundary –fill algorithm. 11. Write a programto implement flood –fill algorithm. 12. Write a programto implement Bezier curve using four control points. 13. Write a programto implement CohenSutherland line clipping algorithm. 14. Write a programto implement Liang Barsky line clipping algorithm. 15. Write a programto implement face of a cartoon.
  • 3. EXPERIMENT-1 1. Write aprogram to draw the line using DDA algorithm. CODING: #include <graphics.h>/*include the necessary header files*/ #include <stdlib.h> #include <stdio.h> #include <conio.h> void draw(intxa,intya,intxb,intyb); void main() { intxa,ya,xb,yb; clrscr(); printf("LineDDA algorithm"); printf("n Enter the value of xa, ya:"); scanf("%d%d",&xa,&ya); printf("n Enter the value of xb, yb:"); scanf("%d%d",&xb,&yb); draw(xa,ya,xb,yb); } void draw(intxa,intya,intxb,intyb) { intxin,yin,x,y,dx,dy,steps,k; /*request autodetection*/ intgdriver=DETECT,gmode,errorcode; /*initialize graphics andlocal variables */ initgraph(&gdriver,&gmode, "c:tcbgi") /* readresult of initialization*/ errorcode=graphresult(); /*an error occurred*/ if (errorcode!=grOk) { printf("Graphics error: %sn", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); } dx=xb-xa; dy=yb-ya; if(abs(dx)>abs(dy)) /*if the conditionis satisfied*/ { /* calculate the value of the conditionvariable*/ steps=abs(dx);
  • 4. } else { steps=abs(dy); } xin=dx/steps; yin=dy/steps; x=xa; y=ya; putpixel(x,y,1); /* draw the first pixel for the line*/ for(k=1;k<=steps;k++) /*for each value of the conditionvariable, */ { x=x+xin; /* calculate the values of (x,y) and draw the pixel*/ y=y+yin; putpixel(x,y,1); } /* cleanup */ getch(); closegraph(); } Output:
  • 5. EXPERIMENT-2 2. Write aprogram to draw the line using Bresenham’s algorithm. CODING: #include <graphics.h>/*include the necessary header files*/ #include <stdlib.h> #include <stdio.h> #include <conio.h> void draw(intxa, intya, intxb, intyb); void main() { intxa, ya, xb, yb; clrscr(); printf("Bresenhnams algorithm"); /*get the coordinates of the line*/ printf("n Enter the value of xa, ya:"); scanf("%d%d",&xa,&ya); printf("n Enter the value of xb, yb:"); scanf("%d%d",&xb,&yb); draw(xa,ya,xb,yb); } void draw(intxa, intya, intxb, intyb) { intx,y,dx,dy,xend,p; /*request autodetection*/ intgdriver=DETECT,gmode,errorcode; /*initialize graphics andlocal variables */ initgraph(&gdriver,&gmode,"c:tcbgi"); /*readresult of initialization*/ errorcode=graphresult(); /*an error occurred*/ if(errorcode!=grOk) { printf("Graphics error: %sn", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); } dx=xb-xa; dy=yb-ya;
  • 6. p=2*dy-dx; /* calculate the value of the conditionvariable*/ if(xa>xb) /* depending on the positionof the coordinates*/ { x=xb; /* assignthe values for (x,y)*/ y=yb; xend=xa; } else if(xb>xa) { x=xa; y=ya; xend=xb; } putpixel(x,y,1); /* draw the pixel on the screen*/ while(x<xend) /* depending onthe control conditiondraw the pixels*/ { x=x+1; if(p<0) { p=p+2*dy; } else { y=y+1; p=p+2*dy; } putpixel(x,y,1); } /* cleanup */ getch(); closegraph(); }
  • 8. EXPERIMENT NO. 03 3 :Write aprogram to implement Bresenham’s Circlealgorithm. PROGRAM # include <stdio.h> # include <conio.h> # include <graphics.h> void circle ( int , int ) ; void main ( ) { intx , y , p , r , i ; intgd=DETECT , gm ; initgraph( &gd , &gm , “tcbgi”) ; setbkcolor (WHITE) ; printf( “Enter the radius = “) ; scanf ( “%d” , &r) ; x = 0 ; y = r ;
  • 9. p = 3 – 2 * r ; putpixel( x , y , RED) ; while ( x< = y ) { if (p < 0) p + = 4 * x + 6; else { p + = 4 * ( x - y) + 10 ; y - - ; } x ++ ;
  • 10. circle p ( x , y) ; } getch( ) ; closegraph( ) ; } void circle p ( int a , int b) { intx , y ; x = a ; y = b ; putpixel ( 300+x , 300 + y , 1 ) ; putpixel( 300 + y , 300 + x , 1 ) ; putpixel( 300 - x , 300 + y , 1 ) ; putpixel( 300 + x , 300 - y , 1 ) ; putpixel( 300 - x , 300 - y , 1 ) ; putpixel( 300 - y , 300 - x , 1 ) ;
  • 11. putpixel( 300 + y , 300 - x , 1 ) ; putpixel( 300 - y , 300 + x , 1 ) ; } getch( ) ; } OUTPUT:
  • 12. EXPERIMENT-4 4. Write aprogram to draw circle using mid-point algorithm. CODING: #include<stdio.h>/* include the necessary header files*/ #include<conio.h> #include<math.h> #include<graphics.h> main() { intgd=DETECT,gin; intxcenter,ycenter,radius; intp,x,y,twox,twoy; /*request autodetect*/ initgraph(&gd,&gin,"C:tcbgi"); x=0; printf("nEnter the radius value:"); /* get the value of the radius and center values*/ scanf("%d",&radius); printf("Enter the center values:"); scanf("%d %d",&xcenter,&ycenter); plotpoints(xcenter,ycenter,x,y); /*call the plotpoints function*/ y=radius; p=1-radius; twox=2*x; twoy=2*y; printf("nptxtyt2xt2yn"); printf("n%dt%dt%dt%dt%dn",p,x,y,twox,twoy); while(x<y) /* in the conditional loopcompute the value of the x and y values*/ { if(p<0) x=x+1; else { x=x+1; y=y-1; }
  • 13. if(p<0) p=p+2*x+1; else p=p+2*(x-y)+1; twox=2*x; twoy=2*y; printf("n%dt%dt%dt%dt%dn",p,x,y,twox,twoy); plotpoints(xcenter,ycenter,x,y); } getch(); return 0; } intplotpoints(intxcenter, intycenter,intx,int y) /* plot the points of the circle as per the procedure*/ { putpixel(xcenter+x,ycenter+y,1); putpixel(xcenter-x,ycenter+y,1); putpixel(xcenter+x,ycenter-y,1); putpixel(xcenter-x,ycenter-y,1); putpixel(xcenter+y,ycenter+x,1); putpixel(xcenter-y,ycenter+x,1); putpixel(xcenter+y,ycenter-x,1); putpixel(xcenter-y,ycenter-x,1); } OUTPUT:
  • 14.
  • 15. EXPERIMENT-5 5. Write aprogram to draw ellipse using mid-point algorithm. CODING: #include<stdio.h>/* include the necessary header files*/ #include<conio.h> #include<graphics.h> include<math.h> #include<stdlib.h> void plotpoints(int,int,int,int); void main() { intgd=DETECT,gm; intxcenter,ycenter,rx,ry; intp,x,y,px,py,rx1,ry1,rx2,ry2; initgraph(&gd,&gm,"C:TCBGI"); /*request autodetect*/ printf("n Enter the radius :"); /* get the radius and the center values*/ scanf("%d %d",&rx,&ry); printf("n Enter the xcenter and ycenter values :"); scanf("%d %d",&xcenter,&ycenter); ry1=ry*ry; rx1=rx*rx; ry2=2*ry1; rx2=2*rx1; /* Region1 */ x=0; y=ry; plotpoints(xcenter,ycenter,x,y); /*for the first region calculate the conditionparameter*/ p=(ry1-rx1*ry+(0.25*rx1)); px=0; py=rx2*y; printf("nxtytptpxtpyn"); printf("n%dt%dt%dt%dt%d",x,y,p,px,py); while(px<py) /* if this conditionis true, compute values of x and y*/ { x=x+1; px=px+ry2;
  • 16. if(p>=0) { y=y-1; py=py-rx2; p=p+ry1+px-py; } else p=p+ry1+px; plotpoints(xcenter,ycenter,x,y); /*call the plotpoints function*/ printf("n%dt%dt%dt%dt%d",x,y,p,px,py); } /* Region2 */ printf("n%dt%dt%dt%dt%d",x,y,p,px,py); printf("nnRegion 2n"); printf("nxtytptpxtpyn"); /* for region2 recalculate the condition variables*/ p=(ry1*(x+0.5)*(x+0.5)+rx1*(y-1)*(y-1)-rx1*ry1); while(y>0) { 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); /*draw the pixels for region2*/ printf("n%dt%dt%dt%dt%d",x,y,p,px,py); } getch(); closegraph(); } void plotpoints(intxcenter,intycenter,intx,inty) /* plot the points of the circle as per the procedure*/ {
  • 18. EXPERIMENT-6 6. Write aprogram Rotationof Triangle. CODING: #include<graphics.h> #include<stdlib.h> #include<stdio.h> #include<conio.h> int x1,x2,x3,y1,y2,y3,t,tx,sx,sy,shx,shy,ch; float rx1,rx2,rx3,ry1,ry2,ry3; float ang,theta; int main(void) { intgdriver = DETECT, gmode, errorcode; initgraph(&gdriver, &gmode,"C:TCBGI"); /*request for auto detection*/ errorcode= graphresult(); if(errorcode!= grOk) /* if error occours*/ { printf("Graphics error: %sn", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); } else { do{ printf("n1.Translationn2.Reflectionn3.Rotationn4.Scalingn5.Shearingn "); printf("nEnter Your choice"); /* get the choice from the user*/ scanf("%d",&ch); switch(ch) { printf("n Enter all coordinates values :"); scanf("%d %d %d %d %d %d",&x1,&y1,&x2,&y2,&x3,&y3); printf("n Before Rotation "); /* get the original coordinates*/ line(x1,y1,x2,y2); line(rx2,ry2,rx3,ry3);
  • 20. EXPERIMENT-7 7. Write a program Translation of Line. CODE #include<stdio.h> #include<conio.h> #include<math.h> #include<graphics.h> #include<process.h> void draw(); void line(); int x1,x2,y1,y2; void main(){ intgd=DETECT,gm; initgraph(&gd,&gm,"..//bgi"); printf("n Enter the first point:"); scanf("%d%d",&x1,&y1); printf("n Enter the Second point:"); scanf("%d%d",&x2,&y2); cleardevice(); draw(); line();
  • 21. getch(); } void draw() { line(x1,y1,x2,y2); } void line() { intx,y,a1,a2,b1,b2; printf("Enter the translation vectors:"); scanf("%d%d",&x,&y); cleardevice(); a1=x1+x; b1=y1+y; a2=x2+x; b2=y2+y; line(a1,b1,a2,b2); } OUTPUT
  • 22.
  • 23. EXPERIMENT-8 8. Write a program to perform scaling ofline. CODE #include<stdio.h> #include<conio.h> #include<math.h> #include<graphics.h> #include<process.h> void draw(); void tri(); int x1,x2,x3,y3,y1,y2,mx,my; void main(){ intgd=DETECT,gm; initgraph(&gd,&gm,"..//bgi"); printf("n Enter the firstpoint for the triangle:"); scanf("%d%d",&x1,&y1); printf("n Enter the Second point for the triangle:"); scanf("%d%d",&x2,&y2); printf("n Enter the Third point for the triangle:"); scanf("%d%d",&x3,&y3);
  • 26. EXPERIMENT-9 9. Write a program shearing of Rectangle. CODE #include<graphics.h> #include<stdio.h> #include<conio.h> #include<stdlib.h> int x1,y1,x2,y2,x,y; int main(void){ intgd=DETECT,gm; clrscr(); initgraph(&gd,&gm,"..//bgi"); printf("ntENter the top left coordinate:"); scanf("%d%d",&x1,&y1); printf("ntENter the bottom right coordinate:"); scanf("%d%d",&x2,&y2); printf("n Enter the value of shearing coordinate for x shear:n"); scanf("%d",&x); printf("n Enter the value of shearing coordinate for y shear:n"); scanf("%d",&y); cleardevice(); rectangle(x1,y1,x2,y2);
  • 27. printf("n Now press a key to see shear in x-axis"); getch(); rectangle(x1,y1,x2*x,y2); printf("n Now press a key to see shear in y-axis"); rectangle(x1,y1,x2,y2*y); getch(); closegraph(); getch(); } Output:
  • 28. EXPERIMENT-10 10.Write aprogramto implement boundary –fill algorithm include<graphics.h> #include<dos.h> void fill_right(intx,int y); void fill_left(intx,int y); void main() { intgd=DETECT,gm,x,y,n,i; clrscr(); initgraph(&gd,&gm,"c:turboc3bgi"); printf("*** Boundary Fill algorithm ***"); line (50,50,200,50); line (200,50,200,300); line (200,300,50,300); line (50,300,50,50); x=100; y=100; fill_right(x,y); fill_left(x-1,y); getch(); } void fill_right(intx,int y) { {
  • 29. if((getpixel(x,y) != WHITE)&&(getpixel(x,y) !=RED) { putpixel(x,y,RED); fill_right(++x,y); x=x-1; fill_right(x,y-1); fill_right(x,y+1); } delay(1); } void fill_left(intx,int y) { if((getpixel(x,y) != WHITE)&&(getpixel(x,y) !=RED)) { putpixel(x,y,RED); fill_left(--x,y); x=x+1; fill_left(x,y-1); fill_left(x,y+1); }delay(1); OUTPUT
  • 30. EXPERIMENT-11 11. Write a program toimplement flood–fill algorithm. #include<stdio.h> #include<conio.h> #include<graphics.h> void boundfill(intxc,intyc,intr,intb) { int cur; cur=getpixel(xc,yc); if(cur!=b && cur!=r) { putpixel(xc,yc,r); delay(1); boundfill(xc+1,yc,r,b); boundfill(xc-1,yc,r,b); boundfill(xc,yc+1,r,b); boundfill(xc,yc-1,r,b); } } void main() { intgd=DETECT,gm; initgraph(&gd,&gm,"..bgi");
  • 32. EXPERIMENT-12 12. Write a program toimplement Bezier curve using four control points. #include <stdio.h> #include <stdlib.h> #include <graphics.h> #include <math.h> void bezier (intx[4], int y[4]) { intgd = DETECT, gm; inti; double t; initgraph (&gd, &gm, "..bgi"); for (t = 0.0; t < 1.0; t += 0.0005) { double xt = pow (1-t, 3) * x[0] + 3 * t * pow (1-t, 2) * x[1] + 3 * pow (t, 2) * (1-t) * x[2] + pow (t, 3) * x[3]; double yt = pow (1-t, 3) * y[0] + 3 * t * pow (1-t, 2) * y[1] + 3 * pow (t, 2) * (1-t) * y[2] + pow (t, 3) * y[3]; putpixel (xt, yt, WHITE); } for (i=0; i<4; i++) putpixel (x[i], y[i], YELLOW); getch(); closegraph(); return; }
  • 33. void main() { intx[4], y[4]; inti; printf ("Enter the x- and y-coordinates of the four control points.n"); for (i=0; i<4; i++) scanf ("%d%d", &x[i], &y[i]); bezier (x, y); } closegraph(); } getch(); } OUTPUT
  • 34. EXPERIMENT-13 13. Write a program toimplement CohenSutherlandline clipping algorithm. #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> void clip(float,float,float); inti,j=0,n; int rx1,rx2,ry1,ry2; float x1[8],y1[8]; void main() { intgd=DETECT,gm; inti,n; float x[8],y[8],m; clrscr(); initgraph(&gd,&gm,""); printf("coordinates for rectangle : "); scanf("%d%d%d%d",&rx1,&ry1,&rx2,&ry2); printf("no. of sides for polygon : "); scanf("%d",&n); printf("coordinates : ");
  • 36. line(x1[i],y1[i],x1[i+1],y1[i+1]); getch(); } void clip(float e,floatf,floatm) { while(e<rx1 e>rx2 f<ry1 f>ry2) { if(e<rx1) { f+=m*(rx1-e); e=rx1; } else if(e>rx2) { f+=m*(rx2-e); e=rx1; } if(f<ry1) { e+=(ry1-f)/m; f=ry1; } else if(f>ry2)
  • 38. EXPERIMENT-14 14. Write a program toimplement Liang Barsky line clipping algorithm. #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> Void main { float x1,y1,x2,y2,xmin,xmax,ymin,ymax,dx,dy; float p[4],q[4],r[4]; float max,min,u1,u2; float xi,xii,yi,yii; int gd,gm,i; gd=DETECT; initgraph(&gd,&gm,"c:tcbgi"); clrscr(); printf("n enter the line co-ordinates"); printf("n enter 1stx="); scanf("%f",&x1); printf("t1sty="); scanf("%f",&y1); printf("n enter 2nd x="); scanf("%f",&x2); printf("t2nd y=");
  • 39. scanf("%f",&y2); printf("n enter window boundry"); printf("n xmin="); scanf("%f",&xmin); printf("n ymin="); scanf("%f",&ymin); printf("n xmax="); scanf("%f",&xmax); printf("n ymax="); scanf("%f",&ymax) dx=x2-x1; dy=y2-y1; cleardevice(); line(x1,y1,x2,y2); rectangle(xmin,ymin,xmax,ymax); p[0]=-dx; q[0]=x1-xmin; p[1]=dx; q[1]=xmax-x1; p[2]=-dy; p[2]=-dy; q[2]=y1-ymin; p[3]=dy; q[3]=ymax-y1; for(i=0;i<4;i++)
  • 40. { if(p[i]==0 && q[i]<0) { printf("Lineis outside the boundry,itis not a clipping candidaten"); getch(); exit(0); } } for(i=0;i<4;i++) { r[i]=q[i]/p[i]; printf("n r[%d]=%f",i,r[i]); } max=0;min=1; for(i=0;i<4;i++) if(p[i]<0) { if(r[i]>max) max=r[i]; } else { if(r[i]<min) min=r[i]; }
  • 41. u1=max; u2=min; printf("n u1=%f",u1); printf("n u2=%f",u2); if(u1>u2) { printf("n line is completely outside"); getch(); exit(0); } xi=x1+(u1*dx); yi=y1+(u1*dy); xii=x1+(u2*dx); yii=y1+(u2*dy); rectangle (xmin, ymin, xmax, ymax); sector(5); line(xi,yi,xii,yii); getch(); closegraph(); }
  • 42.
  • 43. EXPERIMENT-15 15. Write a program toimplement face of a cartoon. # include <graphics.h> # include<conio.h> #include <stdlib.h> main() { intgd= DETECT, gm,area, temp1, temp2, left=25, top=75; void*p; initgraph(&gd, &gm,”C:TCBGI”); Setcolor(YELLOW); Circle(50, 100,25); Sefillstyle(SOLID__FILL,YELLOW); floodfill(50,100, YELLOW); Setcolor(BLACK); Sefillstyle(SOLID__FILL,BLACK); fillellipse(44, 85, 2, 6); fillellipse(56, 85, 2, 6); ellipse(50, 100, 205, 335, 20,9); ellipse(50, 100, 205, 335, 20,10); ellipse(50, 100, 205, 335, 20,11); area= imagesize(left, top, left+ 50, top+50); p= malloc(area); setcolor(WHITE); settextstyle(SANS_SERIF_FONT, HORIZ_DIR,2); outtextxy(155, 451, “Smiling Face Animation”); setcolor(BLUE); rectangle(0,0 , 639,449); while(!kbhit()) { Temp1= 1+ randam(588);
  • 44. { Temp1= 1+ randam(380); { Getimage(left, top, left+ 50, top+ 50); putimage(left, top, p, XOR_PUT); putimage(temp1, temp2, p, XOR_PUT); delay(100); left= temp1; top = temp2; } getch(); closegraph(); return(); } OUTPUT