Professional Documents
Culture Documents
Div:B id:vu1f2021108
Batch:B
Subject:CG
PRACTICAL NO. 1
TITLE: Implementation of DDA Line and Bresenham’s Line Drawing Algorithm.
PRE-REQUISITE :
Knowledge of
• Graphics Initializations
• DDA Algorithm.
Input will be two endpoints (x1, y1) and (x2, y2) and the output will be any
one of The following line type based on the option selected by the user.
A) Basic line
B) Dotted line
DDA ALGORITHM:
1. Read the line end points (x1,y1) and (x2,y2) such that they are not equal
2. Calculate Δx and Δy
dx =(x2 – x1)
dy = ( y2 – y1)
length = |dy|
while(i<=length)
{ plot(integer x, integer
y) X= x+ X_inc
Y= y + Y_inc
i=i+1
PSEUDO CODE:
1. length ←abs(x2-x1);
5. x ←x+0.5; y ←y+0.5;
6. for i ←1 to length follow steps 7 to 9
7. plot(trunc(x),trunc(y));
8. x ←x + x increment;
9. y ←y + y increment;
10. End.
CODE:
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>
void main()
float x, y,x_inc,y_inc;
scanf("%d %d",&x1,&y1);
dx= x2-x1;
dy= y2-y1;
if(abs(dx)>abs(dy))
length =abs(dx);
else
length =abs(dy);
x_inc = dx/(float)length;
y_inc = dy/(float)length;
//printing graph
x=x1;
y=y1;
for( i =0;i<length;i++)
putpixel(x,y,10);
x=x+x_inc;
y =y+y_inc; delay(30);
getch();
closegraph();
OUTPUT:
BRESENHAM'S LINE DRAWING ALGORITHM
OBJECTIVE : To study and Implement Bresenham's Line drawing Algorithm.
PRE-REQUISITE :
Knowledge of
• Graphics Initializations
Input will be two endpoints (x1, y1) and (x2, y2) and the output will be any
one of the following line type based on the option selected by the user.
A) Basic line
B) Dotted line
2. Load (x0, y0) into the frame buffer; that is, plot the first point.
3. Calculate constants Δx, Δy, 2Δy and 2Δy - 2Δx, and obtain the starting value
for the decision parameter as: P0=2Δy - Δx
4. At each xk, the next point the line, starting at k=0, perform the following
test:
6. END
CODE:
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
int main()
x0,y0,x1,y1,x,y,p,k,dx,dy;
initgraph(&gdriver,&gmode,"C:\\TURBOC3\\BGI");
%d",&x0,&y0);
scanf("%d %d",&x1,&y1);
dx= x1-x;
dy=y1-y0;
x=x0;
y=y0;
p=2*dy-dx;
for(k=0;k<dx;+k++)
delay(10);
if(p>=0)
putpixel(x,y,7);
dy = y1-y0;
x=x0;
y=y0;
p=p+2*dy-2*dx;
else
putpixel(x,y,7);
p=p+2*dy;
x=x+1;
getch();
closegraph();
return 0;
}
OUTPUT :
PRACTICAL NO. 2
TITLE : Implementation of Bresenham’s Circle Drawing Algorithm.
PRE-REQUISITE :
Knowledge of
• Graphics Initializations
Input to the program will be centre and radius of the circle. Based on the
choice of user the output will be circle drawn by using the selected algorithm .
4. do {
Plot(x,y)
if(d<0)
x=x+1
y=y
d=d+4x+6
Else
x=x+1
y=y-1
d=d+4(x-y)+10
} While (x<y);
6. END
CODE:
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
void main()
int gdriver=DETECT,gmode,xc,yc,r,p,x,y;
initgraph(&gdriver,&gmode,"C:\\TURBOC3\\BGI");
scanf("%d%d",&xc,&yc);
p=3-2*r;
x=0;
y=r;
while(x<=y)
putpixel(xc+x,yc+y,RED);
putpixel(xc+y,yc+x,RED);
putpixel(xc-x,yc+y,RED);
putpixel(xc+y,yc-x,RED);
putpixel(xc-x,yc-y,RED);
putpixel(xc-y,yc-x,RED);
putpixel(xc+x,yc-y,RED);
putpixel(xc-y,yc+x,RED);
if(p<0)
x =x+1;
p=p+4*x+6;
else
x=x+1;
y=y-1;
p=p+4*(x-y)+10;
getch();
OUTPUT:
PRACTICAL NO. 3
PRE-REQUISITE :
Knowledge of
• Graphics Initializations
Input to the program will be centre and radius of the circle. Based on the
choice of user the output will be circle drawn by using the selected algorithm .
4. do {
Plot(x, y)
If (p<0)
x=x+1
y=y
p=p+2x+1
Else
x=x+1
y=y-1
p=p+2(x-y)+1
While (x<y);
6. END.
CODE:
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
void main()
int gdriver=DETECT,gmode,xc,yc,r,p,x,y;
initgraph(&gdriver,&gmode,"C:\\TURBOC3\\BGI");
scanf("%d%d",&xc,&yc);
printf("Enter the radius of a circle \n");
scanf("%d",&r);
p=1-r;
x=0;
y=r;
while(x<=y)
putpixel(xc+x,yc+y,RED);
putpixel(xc+y,yc+x,RED);
putpixel(xc-x,yc+y,RED);
putpixel(xc+y,yc-x,RED);
putpixel(xc-x,yc-y,RED);
putpixel(xc-y,yc-x,RED);
putpixel(xc+x,yc-y,RED);
putpixel(xc-y,yc+x,RED);
if(p<0)
x =x+1;
p=p+2*x+1;
else
x=x+1;
y=y-1;
p=p+2*x+1-2*y;
getch();
OUTPUT:
PRACTICAL NO.4
PRE-REQUISITE :
Knowledge of Ellipse Algorithm Input will be the coordinate of center and the
major and minor axis Length in pixels.
ELLIPSE ALGORITHM:
1. Start
5. calculate t=pi/180
6. Initialize i=0
7. compute d= i*t
9. plot(x,y)
10. increment i by 1
#include<graphics.h>
#include<conio.h>
int x,y,p;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
x =0;
y =ry;
p=(ry*ry)-(rx*rx*ry)+((rx*rx)/4);
while((2*x*ry*ry)<(2*y*rx*rx))
putpixel(xc+x,yc-y,WHITE);
putpixel(xc-x,yc+y,WHITE);
putpixel(xc+x,yc+y,WHITE);
putpixel(xc-x,yc-y,WHITE);
if(p<0)
x=x+1;
p=p+(2*ry*ry*x)+(ry*ry);
else
x=x+1;
y=y-1;
p=p+(2*ry*ry*x+ry*ry)-(2*rx*rx*y);
p=((float)x+0.5)*((float)x+0.5)*ry*ry+(y-1)*(y-1)*rx*rx-rx*rx*ry*ry;
while(y>=0)
putpixel(xc+x,yc-y,WHITE);
putpixel(xc-x,yc+y,WHITE);
putpixel(xc+x,yc+y,WHITE);
putpixel(xc-x,yc-y,WHITE);
if(p>0)
y=y-1;
p=p-(2*rx*rx*y)+(rx*rx);
else
{
y=y-1;
x=x+1;
p=p+(2*ry*ry*x)-(2*rx*rx*y)-(rx*rx);
void main()
int xc,yc,rx,ry;
clrscr();
printf("Enter xc:");
scanf("%d",&xc);
printf("Enter yc:");
scanf("%d",&yc);
printf("Enter rx:");
scanf("%d",&rx);
printf("Enter ry:");
scanf("%d",&ry);
ell(xc,yc,rx,ry);
getch();
closegraph();
}
OUTPUT:
PRACTICAL NO. 5
TITLE : Implementation of Area fill Algorithm: Boundary fill, Flood fill
PRE-REQUISITE :
(x, y) are the interior points, boundary is the boundary color and fill color is
the color to be filled. Following is a recursive method for boundary fill.
1. Present_color = getcolor () //a function which returns the color of (x, y)
2. If present color <> boundary and if present_color < > fill_color then repeat
steps 3-7
8. End
CODE:
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
putpixel(x,y,fill_color);
boundaryFill4(x+1,y,fill_color,boundary_color);
boundaryFill4(x,y+1,fill_color,boundary_color);
boundaryFill4(x-1,y,fill_color,boundary_color);
boundaryFill4(x,y-1,fill_color,boundary_color); }
int main()
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
circle(x,y,radius);
boundaryFill4(x,y,6,15);
delay(50000);
getch();
closegraph();
return 0;
OUTPUT:
POLYGON FILLING ALGORITHM:- FLOOD FILL ALGORITHM Input will be or
the co-ordinate values of the polygonthe circle to fill And the color intensity
with which it is to fill.
(x,y) are the interior points, background is the current colour inside the
polygon boundary color and fill_color is the color to be filled. Following is
a recursively method for Flood fill Algorithm.
8 End
CODE:
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
void flood(int x , int y,int new_col ,int old_col)
if(getpixel(x,y) == old_col)
putpixel(x,y,new_col);
flood(x+1,y,new_col,old_col);
flood(x-1,y,new_col,old_col);
flood(x,y+1,new_col,old_col);
flood(x,y-1,new_col,old_col);
int main()
int oldcolor = 0;
flood(x,y,newcolor,oldcolor);
closegraph();
getch();
return 0;
OUTPUT:
PRACTICAL NO.6
PRE-REQUISITE : Knowledge of
a)Bezier Curve Algorithm.
XAB=(XA+XB)/2
YAB=(YA+YB)/2
XBC=(XB+XC)/2
YBC=(YB+YC)/2
XCD=(XC+XD)/2
YCD=(YC+YD)/2
X ABC = (X AB +X BC )/2
Y ABC = (Y AB +Y BC )/2
X BCD = (X BC +X CD )/2
Y BCD = (Y BC +Y CD )/2
3. Repeat the step 2 for section A, AB, ABC, and ABCD and section
ABCD, BCD, CD and D
by straight lines
6. End
CODE:
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<graphics.h>
#include<conio.h>
void main(){
int x[4],y[4],m;
clrscr();
//int gd=DETECT,gm;
//initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("\t\tBEZIER CURVE\t\t\n");
printf("\nEnter Four Control Points:
"); for(m=0;m<4;m++){
bez(x,y);
getch();
int m;
float xs,ys;
float u;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
//float xs,ys;
cleardevice();
//float u;
for(u=0.0;u<1.0;u=u+0.0005){
xs=(x[0]*pow(1-u,3))+(x[1]*3*u*pow(1-u,2))+(x[2]*3*u*u*(1-
u))+(x[3]*pow(u,3));
ys=(y[0]*pow(1-u,3))+(y[1]*3*u*pow(1-u,2))+
(y[2]*3*u*u*(1-u))+(y[3]*pow(u,3));
putpixel(xs,ys,GREEN);
for(m=0;m<4;m++){
putpixel(x[m],y[m],YELLOW);
OUTPUT:
PRACTICAL NO.7
PRE-REQUISITE : Knowledge of
1)Translation
2)Rotation
3)Scaling
DESCRIPTION:
coordinates(x’,y’).
Matrix format:
[x’,y’]=[x y]
Translation Transformations:
Matrix format:
[x’,y’]= [x y] [t x t y ]
Rotation Transformation:
xy plane. We specify the rotation angle θ and the position of the rotation
coordinate values(x',y').
Matrix Format:
[x’,y’]=[x y]
CODE:
#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
void main(){
int gm;
int gd=DETECT;
int x1,x2,x3,y1,y2,y3,nx1,nx2,nx3,ny1,ny2,ny3,c;
int sx,sy,xt,yt,r;
float t;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
setcolor(1);
scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
getch();
printf("\n1)Translation\n2)Rotation\n3)Scalling\n4)Exit");
scanf("%d",&c);
switch(c){
ny1=y1+yt;
nx2=x2+xt;
ny2=y2+yt;
nx3=x3+xt;
ny3=y3+yt;
line(nx1,ny1,nx2,ny2);
line(nx2,ny2,nx3,ny3);
line(nx3,ny3,nx1,ny1);
t=3.14*r/180;
nx1=abs(x1*cos(t)-y1*sin(t));
ny1=abs(x1*sin(t)+y1*cos(t));
nx2=abs(x2*cos(t)-y2*sin(t));
ny2=abs(x2*sin(t)+y2*cos(t));
nx3=abs(x3*cos(t)-y3*sin(t));
ny3=abs(x3*sin(t)+y3*cos(t));
line(nx1,ny1,nx2,ny2);
line(nx2,ny2,nx3,ny3);
line(nx3,ny3,nx1,ny1);
getch();
nx1=x1*sx;
ny1=y2*sy;
nx2=x2*sx;
ny2=y2*sy;
nx3=x3*sx;
ny3=y3*sy;
line(nx1,ny1,nx2,ny2);
line(nx2,ny2,nx3,ny3);
line(nx3,ny3,nx1,ny1);
getch();
case 4:break;
closegraph();
}
OUTPUT:
PRACTICAL NO.8
DESCRIPTION:
Scaling Transformations:
coordinates(x’,y’).
Matrix format:
[x’,y’]=[x y]
Translation Transformations:
Matrix format:
[x’,y’]= [x y] [t x t y ]
Rotation Transformation:
xy plane. We specify the rotation angle θ and the position of the rotation
coordinate values(x',y').
Matrix Format:
[x’,y’]=[x y]
OUTPUT:
Case 1)
Case 2)