You are on page 1of 41

Name:Neepun Sunil Patil

Div:B id:vu1f2021108

Batch:B

Subject:CG

PRACTICAL NO. 1
TITLE: Implementation of DDA Line and Bresenham’s Line Drawing Algorithm.

OBJECTIVE : To study and implement DDA Line drawing Algorithm.

PRE-REQUISITE :
Knowledge of

• Point Plotting Methods

• 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

C) Dash-Dot pattern 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)

3. Calculate length If (|dx| >=|dy|) length= |dx| else

length = |dy|

4. Calculate increment X_inc = (x2 – x1 )/ length Y_inc =( y2 – y1) / length

5.Plotting all the points in the line


i=1

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);

2. if(abs(y2-y1) > length) then length ← abs(y2-y1);

3. x increment ← (x2-x1) / length;

4. y increment ← (y2-y1) / length;

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()

int x1, y1, x2, y2,length,i,dx,dy;

float x, y,x_inc,y_inc;

int gd=DETECT,gm; // gd mrans graph driver


initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");

printf("Enter the initial Point \n");

scanf("%d %d",&x1,&y1);

printf("Enter the Final Point\n");


scanf("%d %d",&x2,&y2);

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

•Point Plotting Methods

• Graphics Initializations

• Bresenham's Line 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

C) Dash-Dot pattern line

BRESENHAM'S LINE DRAWING ALGORITHM:


1. Input the two line endpoints and store the left endpoint in(x0, y0)

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:

If pk < 0, the next point is (xk+1,yk) and


Pk+1=pk+2Δy

Otherwise, the next point is (xk+1,yk+1) and

pk+1=pk +2Δy - 2Δx

5. Repeat step 4 Δx times

6. END

CODE:
#include<stdio.h>

#include<graphics.h>

#include<conio.h>

#include<dos.h>

int main()

int gdriver=DETECT ,gmode; int

x0,y0,x1,y1,x,y,p,k,dx,dy;

initgraph(&gdriver,&gmode,"C:\\TURBOC3\\BGI");

printf("\n\tBresenham's Line Drawing Alogorith\n ");

printf("Enter initial Points\n"); scanf("%d

%d",&x0,&y0);

printf("Enter Final Points\n");

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.

OBJECTIVE : To study and implement Bresenham's Circle algorithm.

PRE-REQUISITE :
Knowledge of

• Point Plotting Methods

• Graphics Initializations

• Bresenham's circle drawing algorithm

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 .

ALGORITHM: BRESENHAM'S CIRCLE ALGORITHM


1. Read the radius (r) of the circle

2. Initialize starting point as x=0 y=r

3. Calculate initial value of decision parameter as d=3-2r

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);

5. Determine symmetry point

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");

printf("\n\tBresenhams circle Drawing Algorithm \n");

printf("Enter the Co-ordinates of center: \n");

scanf("%d%d",&xc,&yc);

printf("Enter the radius of a circle \n");


scanf("%d",&r);

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

TITLE : Implementation of Midpoint Circle Drawing Algorithm

OBJECTIVE : To study and implement midpoint Circle algorithm.

PRE-REQUISITE :
Knowledge of

• Point Plotting Methods

• Graphics Initializations

• Midpoint circle drawing algorithm

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 .

MIDPOINT CIRCLE ALGORITHM :

1. Read the radius (r) of the circle

2. Initialize starting point as x=0 y=r

3. Calculate initial value of decision parameter as p=1.25-r

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);

5. Determine symmetry point

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");

printf("\n\tMidpoint circle Drawing Algorithm \n");

printf("Enter the Co-ordinates of center: \n");

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

TITLE : Implementation of Ellipse Drawing Algorithm

OBJECTIVE : To study and Implement Ellipse Algorithm

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

2. Initialize the graphics mode.

3. Get the center point as (x1,y1)

4. Get the length of semi-major, Semi-minor axes as r1 & r2

5. calculate t=pi/180

6. Initialize i=0

7. compute d= i*t

8. compute x=x1+y1*sin(d), y=y1+r2cos(d)

9. plot(x,y)

10. increment i by 1

11. Repeat steps 6 to 9 until i


CODE:
#include<stdio.h>

#include<graphics.h>

#include<conio.h>

void ell(int xc ,int yc ,int rx ,int ry)

int gd = DETECT ,gm;

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("\n\tMidpoint Ellipse Drawing Algoritm \n");

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

OBJECTIVE : To study and Implement Polygon Filling Algorithm by using


Boundary fill algorithm

PRE-REQUISITE :

Knowledge of polygon filling algorithm and Boundary Fill Algorithm Input


will be the co-ordinate values of the polygon or the circle to fill and the color
intensity with which it is to fill.

ALGORITHM : BOUNDARY FILL ALGORITHM

(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

3. set_pixel (x, y, fill_color)

4. call the algorithm recursively for points (x+1,y)

5. call the algorithm recursively for points (x-11,y)

6. call the algorithm recursively for points (x,y+1)

7. call the algorithm recursively for points (x,y-1)

8. End
CODE:
#include<stdio.h>

#include<graphics.h>

#include<conio.h>

#include<dos.h>

void boundaryFill4(int x , int y ,int fill_color,int boundary_color)

if(getpixel(x,y) != boundary_color && getpixel(x,y) != fill_color)

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()

int gd =DETECT ,gm ;

int x = 250 , y =200 , radius =50 ;

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.

ALGORITHM: FLOOD FILL ALGORITHM :

(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.

1 present_color=getcolor()// a function which returns the current color of (x,y)

2 if present_color <> background and if present_color <> fill_color then repeat


steps 3-7

3 set_pixel (x,y, fill_color)

4 call the algorithm recursively for points (x+1,y)

5 call the algorithm recursively for points (x+1,y)

6 call the algorithm recursively for points (x+1,y)

7 call the algorithm recursively for points (x+1,y)

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 gd= DETECT ,gm;

int x =51 ,y = 51;

int newcolor = 12;

int oldcolor = 0;

int left ,right;

int top = left =50;

int bottom = right =300;


initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
rectangle(left,top,right,bottom);

flood(x,y,newcolor,oldcolor);

closegraph();

getch();

return 0;

OUTPUT:
PRACTICAL NO.6

TITLE : Implementation of Bezier Curve.

OBJECTIVE : To study and Implement Bezier Curve.

PRE-REQUISITE : Knowledge of
a)Bezier Curve Algorithm.

ALGORITHM : Bezier Curve

1.Get Four control points say A(x A ,y A ), B(x B ,y


B ), C(x C ,y C ), D(x D ,y D )

2. Divide the curves represented by point A,B,C and D in two


sections

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

X ABCD = (X ABC +X BCD )/2

Y ABCD = (Y ABC +Y BCD )/2

3. Repeat the step 2 for section A, AB, ABC, and ABCD and section
ABCD, BCD, CD and D

4. Repeat step 3 until we have sections so short that they can be


replaced

by straight lines

5. Replace small sections by straight lines

6. End

CODE:

#include<stdio.h>

#include<math.h>

#include<stdlib.h>

#include<graphics.h>

#include<conio.h>

void bez(int x[4],int y[4]);

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++){

printf("\n %d X Co-ordinate and y Co-ordinate:",m+1);


scanf("%d%d",&x[m],&y[m]);

bez(x,y);

getch();

void bez(int x[4],int y[4]){

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

TITLE : Implementation of 2D transformation: Translation, Scaling,


Rotation.

OBJECTIVE : To Implement set of 2D Transformation on Polygon i.e


Translation, Rotation and Scaling.

PRE-REQUISITE : Knowledge of

Basic 2D Transformation on Polygon:

1)Translation

2)Rotation

3)Scaling

DESCRIPTION:

Transformation allows us to uniformly alter the entire picture. The


geometric transformation considered here-translation, scaling and
rotation are expressed in terms of matrix multiplication of Homogenous
coordinates are considered to uniformly treat the transactions.
Scaling Transformations:

A 2D point can be scaled by multiplication of the coordinate

values(x,y) by scaling factors Sx and Sy to produce the transformed

coordinates(x’,y’).

Matrix format:

[x’,y’]=[x y]

Translation Transformations:

A 2D point can be translated by adding the coordinate values(x,y)


by Translation

distances tx and ty to produce the transformed coordinates(x&#39;y&#39;).

Matrix format:

[x’,y’]= [x y] [t x t y ]

Rotation Transformation:

A 2D point can be rotated by repositioning it along a circular path in the

xy plane. We specify the rotation angle θ and the position of the rotation

point about which the object is to be rotated. Multiplication of the

coordinate values(x,y) by rotation matrix produce the transformed

coordinate values(x&#39;,y&#39;).

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");

printf("\nProgram For Basic Transactions");

printf("\nEnter Points of Triangle");

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");

printf("\nEnter Your Choice:");

scanf("%d",&c);

switch(c){

case 1:printf("\nEnter The Translation Factor:");


scanf("%d%d",&xt,&yt);
nx1=x1+xt;

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);

case 2:printf("\nEnter the angle of


rotation"); scanf("%d",&r);

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();

case 3:printf("\nEnter The Scaling Factor");


scanf("%d%d",&sx,&sy);

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;

default:printf("\nEnter Correct Choice");

closegraph();

}
OUTPUT:
PRACTICAL NO.8

TITLE : Implementation of 2D transformation: Reflection, shear.

DESCRIPTION:

Transformation allows us to uniformly alter the entire picture. The

geometric transformation considered here-translation, scaling and

rotation are expressed in terms of matrix multiplication of Homogenous

coordinates are considered to uniformly treat the transactions.

Scaling Transformations:

A 2D point can be scaled by multiplication of the coordinate values(x,y)

by scaling factors Sx and Sy to produce the transformed

coordinates(x’,y’).

Matrix format:

[x’,y’]=[x y]

Translation Transformations:

A 2D point can be translated by adding the coordinate values(x,y)


by Translation

distances tx and ty to produce the transformed coordinates(x&#39;y&#39;).

Matrix format:

[x’,y’]= [x y] [t x t y ]
Rotation Transformation:

A 2D point can be rotated by repositioning it along a circular path in the

xy plane. We specify the rotation angle θ and the position of the rotation

point about which the object is to be rotated. Multiplication of the

coordinate values(x,y) by rotation matrix produce the transformed

coordinate values(x&#39;,y&#39;).

Matrix Format:

[x’,y’]=[x y]

OUTPUT:

Case 1)
Case 2)

You might also like