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);
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;
}
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*/
{
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();
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)
{
{
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 : ");
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=");