Practical-1: Aim:-Write A Program To Implement DDA Line Drawing Algorithm. Algorithm
Practical-1: Aim:-Write A Program To Implement DDA Line Drawing Algorithm. Algorithm
PRACTICAL-1
Algorithm:-
dx = x1-x0;
dy = y1-y0;
X = x0;
Y = y0;
{ putpixel ( X , Y , WHITE )
X += Xinc ;
Y += Yinc ;
1
8716116
Code:-
#include<stdio.h>
#include<graphics.h>
#include<math.h>
void main()
{ int gd=DETECT,gm; // gd=graphics driver (detects best graphics driver and assigns it as
default, gm=graphics mode.
int x1,y1,x2,y2,steps,k;
floatxincr,yincr,x,y,dx,dy;
printf("enter x1,y1");
scanf("%d%d",&x1,&y1);
printf("enter x2,y2");
scanf("%d%d",&x2,&y2);
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
steps=abs(dx);
else
steps=abs(dy);
xincr=dx/steps;
yincr=dy/steps;
x=x1;
2
8716116
y=y1;
for(k=1;k<=steps;k++)
x+=xincr;
y+=yincr;
putpixel(round(x),round(y),WHITE);
outtextxy(x1+5,y1-5,"(x1,y1)");
outtextxy(x2+5,y2+5,"(x2,y2)");
getch();
closegraph(); // closes the graph and comes back to previous graphic mode.
float round(float a)
int b=a+0.5;
return b;
3
8716116
PRACTICAL-2
Algorithm:-
Step 1: Declare variable x1, x2, y1, y2, d, i1, i2, dx, dy
Where x1, y1 coordinates of starting point and x2, y2 coordinates of ending point
Step 3: Calculate dx = x2 – x1
dy = y2 – y1
i1 = 2 * dy;
i2 = 2 * (dy - dx)
d = i1 – dx
Step 4: Consider (x,y) as starting point and xendas maximum possible value of x.
If dx < 0
Then x = x2
y = y2
xend= x1
If dx > 0
Then x = x1
y = y1
xend= x2
If x >= xend
Stop
If d < 0
4
8716116
Then d = d + i1
If d >= 0
Then d = d + i2
Increment y = y + 1
Increment x = x + 1
Step 9: Go to step 6
Code:-
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{ int x,y,x1,y1,x2,y2,p,dx,dy;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
scanf("%d",&x1);
scanf("%d",&y1);
scanf("%d",&x2);
5
8716116
scanf("%d",&y2);
x=x1;
y=y1;
dx=x2-x1;
dy=y2-y1;
putpixel(x,y,2);
p=(2*dy-dx);
while(x<=x2)
{ if(p<0)
{ x=x+1;
p=p+2*dy;
else
{ x=x+1;
y=y+1;
p=p+(2*dy)-(2*dx);
putpixel(x,y,7);
getch();
closegraph();
6
8716116
PRACTICAL-3
Algorithm:-
putpixel ( xc + x , yc + y, RED);
putpixel ( xc - x , yc + y, RED);
putpixel ( xc + x , yc - y, RED);
putpixel ( xc - x , yc - y, RED);
putpixel ( xc + y , yc + x, RED);
putpixel ( xc - y , yc + x, RED);
putpixel ( xc + x , yc - x, RED);
putpixel ( xc - x , yc – x, RED);
7
8716116
Code:-
#include<stdio.h>
#include<dos.h>
#include<graphics.h>
{ putpixel(xc+x,yc+y,RED);
putpixel(xc-x,yc+y,RED);
putpixel(xc+x,yc-y,RED);
putpixel(xc-x,yc-y,RED);
putpixel(xc+y,yc-x,RED);
putpixel(xc-y,yc+x,RED);
putpixel(xc-y,yc-x,RED);
putpixel(xc-y,yc-x,RED);
{ int x=0,y=r;
int d=3-2*r;
drawCircle(xc,yc,x,y);
while(y>=x)
{ x++;
if(d>0)
{ y--;
d=d+4*(x-y)+10;
else
d=d+4*x+6;
drawCircle(xc,yc,x,y);
8
8716116
delay(50);
}}
int main()
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
circleBres(xc,yc,r2);
return 0;
getch();
9
8716116
PRACTICAL-4
Aim:- Write a program to draw a decagon whose all vertices are connected with
every other vertex using lines.
Theory:-
line() is the only function used in the code of this program. line() is used to draw a line from a
point (x1,y1) to point (x2,y2) i.e. (x1,y1) and (x2, y2) are the end points of the line. The code
is like :
void line( int x1, int x2, int y1, int y2)
10
8716116
Code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main()
int gd=DETECT,gm;
clrscr();
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
line(120,220,170,270);
line(170,270,220,220);
line(220,220,270,270);
line(270,270,320,220);
line(320,220,320,420);
line(320,420,270,370);
line(270,370,220,420);
line(220,420,170,370);
line(170,370,120,420);
line(120,420,120,220);
getch();
closegraph();
return(0);
11
8716116
PRACTICAL-5
Theory:-
Operations:-
Here, P (x,y) is the original point and P’ (x’,y’) is the transformed point.
Code:-
#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>
12
8716116
void main()
{ intgm;
int gd=DETECT;
int x1,x2,x3,y1,y2,y3,nx1,nx2,nx3,ny1,ny2,ny3,c;
intsx,sy,xt,yt,r;
float t;
initgraph(&gd,&gm,"C:\\turboc3\\bgi");
setcolor(3);
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();
scanf("%d",&c);
switch(c)
{ case 1:
scanf("%d%d",&xt,&yt);
nx1=x1+xt;
13
8716116
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);
getch();
break;
case 2:
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);
14
8716116
getch();
break;
case 3:
scanf("%f%f",&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();
break;
case 4:
break;
default:
closegraph();
15
8716116
PRACTICAL-6
Aim:- write a program to implement the mid point circle drawing algorithm.
Algorithm:-
Step 1: Read the radius (r) of the circle.
Step 2: Initialize starting position as
X=0 y=r
Step 3: calculate initial value or decision parameter as
P=1-r
Step 4: do
{ plot(x,y)
if(d<0)
{ x=x+1
Y=y
D=d+2x+1
}
Else
{ x=x+1
Y=y-1
D=d+2x+2y+1
}while(x<y)
Step 5: determine the symmetry points
Step 6: stop
Code:-
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void drawCircle(int x, int y, int xc, int yc);
void main()
{ int gd = DETECT, gm;
int r, xc, yc, pk, x, y;
16
8716116
17
8716116
PRACTICAL-7
Aim:- write a program to implement the line clipping algorithm.
Theory:-
We will use 4-bits to divide the entire region. These 4 bits represent the Top, Bottom, Right,
and Left of the region as shown in the following figure. Here, the TOP and LEFT bit is set
to 1 because it is the TOP-LEFT corner.
18
8716116
Algorithm:-
Step 1 : Assign a region code for two endpoints of given line.
Step 3 : Else, perform the logical AND operation for both region codes.
Step 3.1 : If the result is not 0000, then given line is completely outside.
Step 3.2.1 : Choose an endpoint of the line that is outside the given rectangle.
Step 3.2.2 : Find the intersection point of the rectangular boundary (based on region
code).
Step 3.2.3 : Replace endpoint with the intersection point and update the region code.
19
8716116
Step 3.2.4 : Repeat step 2 until we find a clipped line either trivially accepted or trivially
rejected.
Code:-
#include <iostream.h>
code |= LEFT;
code |= RIGHT;
code |= BOTTOM;
code |= TOP;
20
8716116
return code;
int accept = 0;
while (true)
accept = 1;
break;
// in same region
break;
else
// rectangle
int code_out;
double x, y;
if (code1 != 0)
code_out = code1;
else
21
8716116
code_out = code2;
// x = x1 + (1 / slope) * (y - y1)
y = y_max;
y = y_min;
x = x_max;
x = x_min;
// by intersection point
if (code_out == code1)
{ x1 = x;
y1 = y;
22
8716116
else
{ x2 = x;
y2 = y;
if (accept==1)
{ cout <<"Line accepted from " << x1 << ", " << y1 << " to "<< x2 << ", " << y2 << endl;
else
// Driver code
int main()
cohenSutherlandClip(5, 5, 7, 7);
cohenSutherlandClip(1, 5, 4, 1);
return 0;
23
8716116
PRACTICAL-8
Aim:- write a program to implement boundary filled algorithm.
Algorithm:-
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);
}
}
Code:-
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<graphics.h>
{ int current;
current=getpixel(x,y);
if((current!=boundary)&&(current!=fill))
{ setcolor(fill);
putpixel(x,y,fill);
delay(5);
boundaryfill(x+1,y,fill,boundary);
boundaryfill(x-1,y,fill,boundary);
24
8716116
boundaryfill(x,y+1,fill,boundary);
boundaryfill(x,y-1,fill,boundary);
void main()
{ int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\Turboc3\\BGI");
setcolor(10);
rectangle(250,200,310,260);
boundaryfill(280,250,12,10);
getch();
25
8716116
PRACTICAL-9
Aim:- Draw Hut using C.
Theory:-
Setcolor():- The header file graphics.h contains setcolor() function which is used to
set the current drawing color to the new color.
Syntax :- void setcolor(int color);
rectangle():- Rectangle draws a rectangle in the current line style, thickness, and
drawing color. (left,top) is the upper left corner of the rectangle, and (right,bottom) is
its lower right corner.
Syntax:- void rectangle(int left, int top, int right, int bottom);
Code:-
#include<graphics.h>
#include<conio.h>
int main()
{ int gd = DETECT,gm;
26
8716116
/* Draw Hut */
setcolor(WHITE);
rectangle(150,180,250,300);
rectangle(250,180,420,300);
rectangle(180,250,220,300);
line(200,100,150,180);
line(200,100,250,180);
line(200,100,370,100);
line(370,100,420,180);
/* Fill colours */
setfillstyle(SOLID_FILL, BROWN);
setfillstyle(SLASH_FILL, BLUE);
setfillstyle(HATCH_FILL, GREEN);
getch();
closegraph();
return 0;
27
8716116
PRACTICAL-10
Aim:- write a program to perform the polygon clipping algorithm.
Theory:
A polygon boundary processed with a line clipper may be displayed as a series of
unconnected line segments, depending on the orientation of the polygon to the cIipping
window. What we reaIly want to display is a bounded area after clipping. For polygon
clipping, we require an algorithm that wiIl generate one or more closed areas that are then
scan converted for the appropriate area fill. The output of a polygon clipper should be a
sequence of vertices that defines the clipped polygon boundaries.
Code:-
#include <stdio.h>
#include <graphics.h>
#include <conio.h>
#include <math.h>
#include <process.h>
#define TRUE 1
#define FALSE 0
28
8716116
BOTTOM = 0x2,
RIGHT = 0x4,
LEFT = 0x8
};
float xmin,xmax,ymin,ymax;
{ outcode outcode0,outcode1,outcodeOut;
outcode0 = CompOutCode(x0,y0);
outcode1 = CompOutCode(x1,y1);
do
{ if(!(outcode0|outcode1))
{ accept = TRUE;
done = TRUE;
else
done = TRUE;
else
{ float x,y;
outcodeOut = outcode0?outcode0:outcode1;
{ x = x0+(x1-x0)*(ymax-y0)/(y1-y0);
y = ymax;
else
29
8716116
{ x = x0+(x1-x0)*(ymin-y0)/(y1-y0);
y = ymin;
else
{ y = y0+(y1-y0)*(xmax-x0)/(x1-x0);
x = xmax;
else
{ y = y0+(y1-y0)*(xmin-x0)/(x1-x0);
x = xmin;
if(outcodeOut==outcode0)
x0 = x;
y0 = y;
outcode0 = CompOutCode(x0,y0);
else
{ x1 = x;
y1 = y;
outcode1 = CompOutCode(x1,y1);
}while(done==FALSE);
if(accept)
30
8716116
line(x0,y0,x1,y1);
rectangle(xmin,ymin,xmax,ymax);
{ outcode code = 0;
if(y>ymax)
code|=TOP;
else
if(y<ymin)
code|=BOTTOM;
if(x>xmax)
code|=RIGHT;
else
if(x<xmin)
code|=LEFT;
return code;
void main( )
{ float x1,y1,x2,y2;
clrscr( );
scanf("%d",&n);
for(i=0;i<2*n;i++)
31
8716116
{ scanf("%d",&poly[i]);
poly[2*n]=poly[0];
poly[2*n+1]=poly[1];
scanf("%f%f%f%f",&xmin,&ymin,&xmax,&ymax);
drawpoly(n+1,poly);
rectangle(xmin,ymin,xmax,ymax);
getch( );
cleardevice( );
for(i=0;i<n;i++)
clip(poly[2*i],poly[(2*i)+1],poly[(2*i)+2],poly[(2*i)+3]);
getch( );
restorecrtmode( );
32
8716116
PRACTICAL-11(A)
Aim:- Program in c for printing a rectangle with the help of DDA algorithm.
Code:-
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
void drawline(int x1,int y1,int x2,int y2)
{int dx,dy,m,s;
float xi,yi,x,y;
dx = x2 - x1;
dy = y2 - y1;
if (abs(dx) > abs(dy))
s = abs(dx);
else
s = abs(dy);
xi = dx / (float) s;
yi = dy / (float) s;
x = x1;
y = y1;
putpixel(x1, y1, WHITE);
for (m = 0; m < s; m++)
{ x += xi;
y += yi;
putpixel(x, y, WHITE);
}
}
void main()
{ int gd = DETECT, gm = DETECT;
clrscr();
initgraph(&gd, &gm, "C:\\turboC3\\bgi");
drawline(150,450,450,450);
33
8716116
drawline(450,450,450,250);
drawline(450,250,150,250);
drawline(150,250,150,450);
getch();
}
34
8716116
PRACTICAL-11(B)
Aim:- Program in C for printing a rectangle with the help of Bresenham’s line
drawing algorithm.
code:-
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void lineBres(int,int,int,int);
void setPixel(int,int);
void main()
{ int x,y,length,breadth;
int driver=DETECT,mode;
clrscr();
printf("Enter the co-ordinates of the lower left corner (a b): ");
scanf("%d %d",&x,&y);
printf("Enter the length of the rectangle: ");
scanf("%d",&length);
printf("Enter the breadth of the rectangle: ");
scanf("%d",&breadth);
getch();
initgraph(&driver,&mode,"C:\\TURBOC3\\BGI");
lineBres(x,y,x+length,y);
lineBres(x+length,y,x+length,y-breadth);
lineBres(x+length,y-breadth,x,y-breadth);
lineBres(x,y-breadth,x,y);
getch();
closegraph();
}
void lineBres(int x1,int y1,int x2,int y2)
{ float error,m;
int x,y;
x=x1;
y=y1;
35
8716116
if(x1==x2)
{while(y!=y2)
{ if(y2-y1>0)
++y;
else
--y;
putpixel(x,y,2);
}
}
else
{ m=(float)(y2-y1)/(x2-x1);
error=0;
putpixel(x,y,2);
while(x!=x2)
{ error+=m;
if(error>.5)
{
if(x2-x1>0)
y+=1;
else
y-=1;
--error;
}
if(x2-x1>0)
++x;
else
--x;
putpixel(x,y,2);
}
}
}
36
8716116
37