Computer Graphics Lab practice 3rd year Computer Science
First install dev c++ in your computer as follows:-
Download and install DEV C++ to your default directory (you can use code: blocks or
other editors)
Download the following files(graphics.h, libbgi.a) to the directories mentioned:-
graphics.h to Directory:- C:\Dev-Cpp\include
libbgi.a to Directory:- download to C:\Dev-Cpp\lib)
Create your Project as:
STEP 1: Open DEV C++ Compiler
Launch DEV C++ compiler.
STEP 2: Creating New Project
i. Go to File>New>Project as shown in following figure:
ii. Create New Project "DialogBox" will appear select "empty project"
and name your project in the space provided. Select Language
C or C++ according to your need. Press Ok and select the
location where you want to save.
STEP 4: Set linker parameters
Navigate to "Project" menu and choose "Project Options". A dialogbox
will appear than select "Parameters" option and type following in
"Linker" field.
-lbgi
-lgdi32
-lcomdlg32
-luuid
-loleaut32
-lole32
Press OK, you are now able to use graphics.h functions in your code.
STEP 5: Testing sample Program
In new versions of dev c++ compiler automatically adds one source file to
project. If there is no any existing source file simply add new file By
chossing new file option from file menu. Type the following code and
save the file. I saved file as "main.cpp" its your chooice whatever you
name it.
#include<graphics.h>
int main( ){
initwindow( 700 , 700 , "MY First Program");
circle(200, 200, 150);
getch();
return 0;
}
view rawexample_1_bgi hosted with ❤ by GitHub
Sample Computer Graphics Programs
Sample graphics rectangle code
#include<graphics.h>
#include<isotream.h>
using namespace std;
int main() {
int gd = DETECT, gm;
initgraph(−gd, −gm, "C:\\TC\\BGI");
rectangle(100,100,200,200);
getch();
closegraph();
return 0;
}
1. DDA(Digital Differential Analyzer ) Algorithm
#include <graphics.h>
#include <iostream.h>
#include <math.h>
#include <dos.h>
void main( )
{
float x,y,x1,y1,x2,y2,dx,dy,step;
int i,gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
cout<<"Enter the value of x1 and y1 : ";
cin>>x1>>y1;
cout<<"Enter the value of x2 and y2: ";
cin>>x2>>y2;
dx=abs(x2-x1);
dy=abs(y2-y1);
if(dx>=dy)
step=dx;
else
step=dy;
dx=dx/step;
dy=dy/step;
x=x1;
y=y1;
i=1;
while(i<=step)
{
putpixel(x,y,5);
x=x+dx;
y=y+dy;
i=i+1;
delay(100);
}
closegraph();
}
2. Bresenham’s Line drawing algorithm
#include<iostream.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;
}
}
int main()
{
int gdriver=DETECT, gmode, error, x0, y0, x1, y1;
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");
cout<<"Enter co-ordinates of first point: ";
cin>>x0>>y0;
cout<<"Enter co-ordinates of second point: ";
cin>>x1>>y1;
drawline(x0, y0, x1, y1);
return 0; }