Lab Mannual
Lab Mannual
Lab Mannual
LAB MANUAL
Semester : SIXTH
Sub Code :
Subject : Computer Graphics Lab
Prepared By
Ranjeet Singh
Asst. Prof.
CSE
Buddha Institute of Technology, Gorakhpur -Department of Computer Science & Engg.
INDEX
1) initgraph()
initgraph() function initializes the graphics mode and clears the screen.
2) detectgraph()
Detectgraph function determines the graphics hardware in the system, if the function finds a
graphics adapter then it returns the highest graphics mode that the adapter supports.
3) closegraph()
closegraph() function switches back the screen from graphcs mode to text mode. It clears the
screen also.
A graphics program should have a closegraph function at the end of graphics. Otherwise DOS
screen will not go to text mode after running the program.
4) putpixel()
putpixel function plots a pixel at location (x, y) of specified color.
5) line()
line function is used to draw a line from a point(x1,y1) to point(x2,y2) i.e. (x1,y1) and (x2,y2)
are end points of the line.
6)rectangle()
Rectangle function is used to draw a rectangle. Coordinates of left top and right bottom corner
are required to draw the rectangle. left specifies the X-coordinate of top left corner, top specifies
the Y-coordinate of top left corner, right specifies the X-coordinate of right bottom corner,
bottom specifies the Y-coordinate of right bottom corner.
7) circle()
circle function is used to draw a circle with center (x,y) and third parameter specifies the radius
of the circle.
8)ellipse()
Ellipse is used to draw an ellipse (x,y) are coordinates of center of the ellipse, stangle is the
starting angle, end angle is the ending angle, and fifth and sixth parameters specifies the X and
Y radius of the ellipse. To draw a complete ellipse strangles and end angle should be 0 and 360
respectively.
Objective-1
void lineDDA (int xa, int ya, int xb, int yb)
{
int dx = xb - xa, dy = yb - ya, steps, k;
float xIncrement, yIncrement, x = xa, y = ya;
if (fabs (dx) > fabs (dy))
steps = fabs (dx) ;
else steps = fabs (dy);
xIncrement = float(dx) / float (steps);
yIncrement = float(dy) / float (steps);
putpixel(round(x), round(y),60);
for (k=0; k<steps; k++) {
x += xIncrement;
y += yIncrement;
putpixel(round(x), round(y),60);
}
}
Objective:2
Write a C program for BRESENHAM’S Line Drawing Algorithm
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
dx=x1-x0;
dy=y1-y0;
x=x0;
y=y0;
p=2*dy-dx;
while(x<x1)
{
if(p>=0)
{
putpixel(x,y,7);
y=y+1;
p=p+2*dy-2*dx;
}
else
{
putpixel(x,y,7);
p=p+2*dy;
}
x=x+1;
}
}
void main()
{
int gdriver=DETECT, gmode, error, x0, y0, x1, y1;
Objective:3
Write a C program for MID-POINT CIRCLE Drawing Algorithm
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void circlefun(int xcenter, int ycenter,int x,int y);
void main()
{
int x=0,radius,xcenter,ycenter;
int y,p,gd=DETECT,gm;
clrscr();
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("\n ENTER THE XCENTER &YCENTER:");
scanf("%d%d",&xcenter,&ycenter);
printf("\n ENTER THE RADIUS:");
scanf("%d",&radius);
y=radius;
circlefun(xcenter,ycenter,x,y);
p=1-radius;
while(x<y)
{
if(p<0)
x=x+1;
else
{
x=x+1;
y=y-1;
}
if(p<0)
p=p+2*x+1;
else
p=p+2*(x-y)+1;
circlefun(xcenter,ycenter,x,y);
delay(200);
}
getch();
closegraph();
}
void circlefun(int xcenter,int ycenter,int x,int y)
{
putpixel(xcenter+x,ycenter+y,1);
putpixel(xcenter-x,ycenter+y,1);
putpixel(xcenter+x,ycenter-y,1);
putpixel(xcenter-x,ycenter-y,1);
putpixel(xcenter+y,ycenter+x,1);
putpixel(xcenter-y,ycenter+x,1);
putpixel(xcenter+y,ycenter-x,1);
putpixel(xcenter-y,ycenter-x,1);
}