Académique Documents
Professionnel Documents
Culture Documents
SUBMITTED TO SUBMITTED BY
Algorithm:
Start.
Declare variables x,y,x1,y1,x2,y2,k,dx,dy,s,xi,yi and also declare gdriver=DETECT,gmode.
Initialize the graphic mode with the path location in TC folder.
Input the two line end-points and store the left end-points in (x1,y1).
Load (x1,y1) into the frame buffer; that is, plot the first point. Put x=x1,y=y1.
Calculate dx=x2-x1 and dy=y2-y1.
If abs(dx) > abs(dy), do s=abs(dx).
Otherwise s= abs(dy).
Then xi=dx/s and yi=dy/s.
Start from k=0 and continuing till k<s,the points will be
o x=x+xi.
o y=y+yi.
Place pixels using putpixel at points (x,y) in specified colour.
Close Graph.
Stop.
Code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
voidlineDDA(int,int,int,int);
void main()
int x1,y1,xn,yn;
intgd=DETECT,gm;
initgraph(&gd,&gm,"");
scanf("%d%d",&x1,&y1);
scanf("%d%d",&xn,&yn);
lineDDA(x1,y1,xn,yn);
getch();
intdx,dy,m,i;
m=(yn-y1)/(xn-x1);
for(i=x1;i<=xn;i++)
if(m<=1)
dx=1;
dy=(m*dx);
else
dy=1;
dx=(dy/m);
x1=x1+dx;
y1=y1+dy;
putpixel(x1,y1,RED);
delay(20);
OUTPUT
PROGRAM NO.3
Write a program to draw a line using Bresenham’s line drawing algorithm
Algorithm:
Start.
Declare variables x,y,x1,y1,x2,y2,p,dx,dy and also declare gdriver=DETECT,gmode.
Initialize the graphic mode with the path location in TC folder.
Input the two line end-points and store the left end-points in (x1,y1).
Load (x1,y1) into the frame buffer; that is, plot the first point put x=x1,y=y1.
Calculate dx=x2-x1 and dy=y2-y1,and obtain the initial value of decision parameter p as:
p=(2dy-dx).
Starting from first point (x,y) perform the following test:
Repeat step 9 while(x<=x2).
If p<0,next point is (x+1,y) and p=(p+2dy).
Otherwise, the next point to plot is (x+1,y+1) and p=(p+2dy-2dx).
Place pixels using putpixel at points (x,y) in specified colour.
Close Graph.
Stop
Code:
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
#include<dos.h>
voidlineBres(int,int,int,int);
void main()
int x1,y1,xn,yn;
intgd=DETECT,gm;
initgraph(&gd,&gm,"");
getch();
int dx = xn-x1,dy=yn-y1;
int di = 2*dy-dx;
putpixel(x1,y1,RED);
while(x1<xn)
x1++;
if(di<0)
di=di+ds;
else
y1++;
di=di+dt;
putpixel(x1,y1,RED);
delay(20);
}
PRACTICAL NO. 7
Write a program to draw a circle using midpoint circle drawing algorithm
Algorithm:
1. Input radius r and circle centre (xc, yc), then set the coordinates for the first point on the circumference of a
circle centred on the origin as:
5
p r
0
4
3. Starting with k = 0 at each position xk, perform the following test. If pk < 0, the next point along the circle
centred on (0, 0) is (xk+1, yk) and:
pk 1 p 2xk 1 1
Otherwise the next point along the circle is (xk+1, yk-1) and:
p p 2x 2y
k 1 k 1 k
1
1
k
4. Determine symmetry points in the other seven octants
5. Move each calculated pixel position (x,y)onto the circular path centred at (xc, yc)to plot
the coordinate values:
6. Repeat steps 3 to 5 until x >= y
x x x y y y
c c
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
voidcirclemidpoint(int,int,int);
voiddrawcircle(int,int,int,int);
void main()
{
intxc,yc,r;
intgd=DETECT,gm;
initgraph(&gd,&gm,"");
printf("Enter center coordinates of the circle: ");
scanf("%d%d",&xc,&yc);
printf("Enter radius of the circle: ");
scanf("%d",&r);
circlemidpoint(xc,yc,r);
getch();
}
voidcirclemidpoint(intxc,intyc,int r)
{
int x=0,y=r;
int p=1-r;
while(x<y)
{
drawcircle(xc,yc,x,y);
x++;
if(p<0)
{
p=p+2*x+1;
}
else
{
y--;
p=p+2*(x-y)+1;
}
drawcircle(xc,yc,x,y);
delay(50);
}
}
voiddrawcircle(intxc,intyc,intx,int y)
{
putpixel(xc+x,yc+y,RED);
putpixel(xc-x,yc+y,BLUE);
putpixel(xc+x,yc-y,GREEN);
putpixel(xc-x,yc-y,RED);
putpixel(xc+y,yc+xGREEN);
putpixel(xc-y,yc+x,YELLOW);
putpixel(xc+y,yc-x, YELLOW);
putpixel(xc-y,yc-x, YELLOW);
}
OUTPUT
P RACTICAL N O. 7
Write a program to draw a ellipse using midpoint ellipse drawing algorithm
#include <graphics.h>
#include <conio.h>
void main()
{
clrscr();
int gd = DETECT, gm;
int start_angle = 0;
int end_angle = 360;
ellipse(x, y, start_angle,
end_angle, x_rad, y_rad);
getch();
}
P RACTICAL N O. 9
Write a program to fill a circle using Boundary Fill Algorithm
#include<graphics.h>
#include<math.h>
#include<conio.h>
voiddcircle(inth,intk,int r);
voiddpixel(intx,inty,inth,int k);
void main()
intgd=DETECT,gm;
initgraph(&gd,&gm,"");
setbkcolor(YELLOW);
dcircle(30,30,27);
cfill(30,30,BLUE,RED);
getch();
closegraph();
voiddcircle(inth,intk,int r)
inty,i;
for(i=0;i<=r;i++)
y=sqrt((r*r-(i)*(i)));
dpixel(i,y,h,k);
}
voiddpixel(intx,inty,inth,int k)
putpixel(x+h,y+k,RED);
putpixel(y+h,x+k,RED);
putpixel(-y+h,x+k,RED);
putpixel(-x+h,y+k,RED);
putpixel(-x+h,-y+k,RED);
putpixel(-y+h,-x+k,RED);
putpixel(y+h,-x+k,RED);
putpixel(x+h,-y+k,RED);
int current;
current=getpixel(x,y);
if(current!=bcolor&& current!=fcolor)
putpixel(x,y,fcolor);
cfill(x+1,y,BLUE,RED);
cfill(x-1,y,BLUE,RED);
cfill(x,y+1,BLUE,RED);
cfill(x,y-1,BLUE,RED);
}
}
OUTPUT
P RACTICAL N O. 10
Write a program to fill a circle using Flood Fill Algorithm
#include<graphics.h>
#include<math.h>
#include<conio.h>
voiddcircle(inth,intk,int r);
voiddpixel(intx,inty,inth,int k);
void main()
intgd=DETECT,gm;
initgraph(&gd,&gm,"");
setbkcolor(YELLOW);
dcircle(30,30,27);
ffill(30,30,YELLOW,BLACK);
getch();
closegraph();
voiddcircle(inth,intk,int r)
inty,i;
for(i=0;i<=r;i++)
y=sqrt((r*r-(i)*(i)));
dpixel(i,y,h,k);
voiddpixel(intx,inty,inth,int k)
putpixel(x+h,y+k,RED);
putpixel(y+h,x+k,RED);
putpixel(-y+h,x+k,RED);
putpixel(-x+h,y+k,RED);
putpixel(-x+h,-y+k,RED);
putpixel(-y+h,-x+k,RED);
putpixel(y+h,-x+k,RED);
putpixel(x+h,-y+k,RED);
if(getpixel(x,y)==bcolor)
putpixel(x,y,fcolor);
ffill(x+1,y,YELLOW,BLACK);
ffill(x-1,y,YELLOW,BLACK);
ffill(x,y+1,YELLOW,BLACK);
ffill(x,y-1,YELLOW,BLACK);
}
OUTPUT