Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lab Mannual

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

BUDDHA INSTITUTE OF TECHNOLOGY

Department of Computer Science & Engineering

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

Sl. Experiment / Program Page


1 To write a C program for Line Drawing
Using DDA Algorithm
2

Study of Basic Graphics Functions

Session: Jan- July. 2019 Lab Manual –Computer Graphics Page 1


Buddha Institute of Technology, Gorakhpur -Department of Computer Science & Engg.

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

Session: Jan- July. 2019 Lab Manual –Computer Graphics Page 2


Buddha Institute of Technology, Gorakhpur -Department of Computer Science & Engg.

To write a C program for Line Drawing Using DDA Algorithm


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void lineDDA(int, int, int, int);
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
lineDDA(0,0,145,159);
getch();
closegraph();
}
int round (const float a) {return int (a+0.5);}

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

Session: Jan- July. 2019 Lab Manual –Computer Graphics Page 3


Buddha Institute of Technology, Gorakhpur -Department of Computer Science & Engg.

Objective:2
Write a C program for BRESENHAM’S Line Drawing Algorithm
#include<stdio.h>
#include<conio.h>
#include<graphics.h>

void drawline(int x0, int y0, int x1, int y1)


{
int dx, dy, p, x, y;

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;

Session: Jan- July. 2019 Lab Manual –Computer Graphics Page 4


Buddha Institute of Technology, Gorakhpur -Department of Computer Science & Engg.

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

printf("Enter co-ordinates of first point: ");


scanf("%d%d", &x0, &y0);

printf("Enter co-ordinates of second point: ");


scanf("%d%d", &x1, &y1);
drawline(x0, y0, x1, y1);
getch();
closegraph();
}

Session: Jan- July. 2019 Lab Manual –Computer Graphics Page 5


Buddha Institute of Technology, Gorakhpur -Department of Computer Science & Engg.

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

Session: Jan- July. 2019 Lab Manual –Computer Graphics Page 6


Buddha Institute of Technology, Gorakhpur -Department of Computer Science & Engg.

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

Session: Jan- July. 2019 Lab Manual –Computer Graphics Page 7

You might also like