0% found this document useful (0 votes)
75 views27 pages

Lab Manual For Graphics in C++

Uploaded by

yerosan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views27 pages

Lab Manual For Graphics in C++

Uploaded by

yerosan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Computer Graphics Lab manual in Turbo C++ Edited 2007 e.c by Mekuria G.

COMPUTER GRAPHICS LAB

LAB MANUAL

I would like to show others what i am doing (graphics) Page 1


Computer Graphics Lab manual for both software Engineering and Computer Science

1. Hardware or Software Requirement

A. Software requirement: Turbo C/C++

B. Hardware Requirement
a. Mouse
b. Keyboard
c. 256 MB RAM
d. 14'' Color Monitor
e. Intel Chipset 810 Motherboard

2. List of Experiments
a. Study of basic graphics functions defined in "graphics.h".
b. Write a program to draw a line using DDA algorithm.
c. Write a program to draw line through Bresenham's algorithm.
d. Write a program to draw circle through Bresenham's algorithm.
e. Write a program to perform Sutherland line clipping algorithm.
f. Write a program to draw line using midpoint algorithm.
g. Write a program circle using midpoint algorithm.
h. Write a program to draw an ellipse using midpoint algorithm.
i. Write a program to perform 2d scaling, translation and rotation of geometric objects

Computer Graphics Lab manual for Software Engineering and Computer Science. Page 2
Computer Graphics Lab manual for both software Engineering and Computer Science

1. Basic Graphics Functions

1. INITGRAPH
Turbo C++ graphic functions have two categories :Text mode graphic functions and graphic mode
functions. Here we are dealing with graphic mode function. So just forget about text mode function right
now. To switch from text mode to graphic mode, we have function called as ” initgraph ” .

 Declaration: void initgraph(int *graphicdriver, int *graphicmode, char *pathdrive);


 To start graphics system, you must first call initgraph().
 This function initializes the graphic mode.
 It selects the best resolution and direct that value to mode in variable gm.
 The two int variables gd, gm are graphic driver and graphic mode respectively.
 The gm handles value that tells us which resolution and monitor we are using or it is an integer
that specifies the initial graphics mode (unless *graphicdriver=DETECT). If
*graphicdriver=DETECT, initgraph sets *graphicmode to the highest resolution available for the
detected driver.
 The gd specifies the graphic driver to be used. In our program we have gd=DETECT means we
have passed the highest possible value available for the detected driver.
 If you don’t want that value then you have to assign the constant value for gd, gm.
 The ” &” symbol is used for initgraph to pass address of the constants.
 initgraph() also resets all graphic settings (color, current position, palette, viewport, etc) to their
defaults, then reset the graphreslut to 0.
 To use this function you have to include a header file "graphics.h".
 Path ( ” C:\\tc\\bgi”) : It specifies the directory path where initgraph looks for graphics drivers
(*.BGI) first. If files are not there then initgraph will look for the current directory of your
program. If it unable to find within current working directory then it will parse an error. You can
leave it blank ( ” ” ) if the *.BGI files are within the working directory.
 Return value: initgraph always sets the internal error code:

 On success, initgraph sets the code to 0.


 On error, initgraph sets *graphicdriver to -2, -3, -4, -5 and graphresult returns to the same
value.

Computer Graphics Lab manual for Software Engineering and Computer Science. Page 3
Computer Graphics Lab manual for both software Engineering and Computer Science

 Example
#include<iostream.h>
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
int main()
{
int gd=DETECT,gm;
//Auto detect and select appropriate graphics card/driver and graphics mode
initgraph(&gd,&gm,"c:\\tc\\bgi");
cout<<"Auto detect graphics card/driver"<<gd;
cout<<"Auto detect graphics mode"<<gm;
getch();
closegraph();
return 0;
}

2. CLOSEGRAPH
 Declaration :- void closegraph();
 closegraph function closes the graphics mode, deallocates all memory allocated by graphics
system and restores the screen to the mode it was in before you called initgraph.
 The closegraph() switches back the screen from graphics mode to text mode. If you don’t use this
function then you may have undesirable effects.
 Here this function is called after the getch() function as screen shouldn’t switch to text mode till
user hits any key.
 In general closegraph() shutdown the graphics system.
 To use this function you have to include a header file "graphics.h".
 Example
#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
outtext("Press any key to close the graphics mode...");
getch();
closegraph();
return 0;
}

Computer Graphics Lab manual for Software Engineering and Computer Science. Page 4
Computer Graphics Lab manual for both software Engineering and Computer Science

3. GETCH
 getch( ) function gets a character from console but does not echo it on screen. This is used to
pause the screen till user hits any key.
 To use this function you have to include a header file "conio.h".

4. CLEARDEVICE
 Declaration :- void cleardevice();
 cleardevice function clears the screen in graphics mode and sets the current position to upper left
hand corner of screen or (0,0).
 Clearing the screen consists of filling the screen with current background color.
 Also do not use clrscr in graphics mode instead use cleardevice.
 Example
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
main(){
cout<<"Press any key to clear the screen.";
getch();
clrscr();
cout<<"This appears after clearing the screen.";
cout<<"Press any key to exit...";
getch();
return 0;
}
 In the above program first we display the message "Press any key to clear the screen." using cout
and then ask the user to press a key. When user will press a key screen will be cleared and
another message will be printed.

5. GETCHE
 getche function prompts the user to press a character and that character is printed on screen.
 Example
#include<stdio.h>
#include<conio.h>
main(){
cout<<"Waiting for a character to be pressed from the keyboard to exit.";
getche();
return 0;
}
Run this program and press a character. Then view the user screen (Alt+F5) if using turbo c. You
will find the character printed on the screen if you pressed a printable character. Try pressing
enter or tab key (non printable) characters also.

Computer Graphics Lab manual for Software Engineering and Computer Science. Page 5
Computer Graphics Lab manual for both software Engineering and Computer Science

6. PUTPIXEL
 putpixel function plots a pixel at location (x, y) of specified color.
 Declaration :- void putpixel(int x, int y, int color);
 For example if we want to draw a GREEN color pixel at (35, 45) then we will write putpixel(35,
35, GREEN);
 In our c++ program, putpixel function can be used to draw circles, lines and ellipses using various
algorithms.
 Example

#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
putpixel(25, 25, RED);
getch();
closegraph();
return 0;
}
Output of this program will be a RED pixel on screen at (25, 25) . Try to spot that pixel with your eyes at
left top portion of your computer screen.

7. ARC
 Declaration :- void arc(int x, int y, int stangle, int endangle, int radius);
 arc function is used to draw an arc with center (x,y) and stangle specifies starting angle, endangle
specifies the end angle and last parameter specifies the radius of the arc.
 arc function can also be used to draw a circle but for that starting angle and end angle should be 0
and 360 respectively.
 Example
#include <graphics.h>
#include <conio.h>
main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
arc(100, 100, 0, 135, 50);
getch();
closegraph();
return 0;
}
In the above program (100,100) are coordinates of center of arc, 0 is the starting angle, 135 is the end
angle and 50 specifies the radius of the arc.

Computer Graphics Lab manual for Software Engineering and Computer Science. Page 6
Computer Graphics Lab manual for both software Engineering and Computer Science

8. CIRCLE
 Declaration :- void circle(int x, int y, int radius);
 Circle function is used to draw a circle with center (x,y) and third parameter specifies the radius
of the circle.
 The code given below draws a circle.
#include<graphics.h>
#include<conio.h>
main(){
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
circle(100, 100, 50);
getch();
closegraph();
return 0;}

In the above program (100, 100) are coordinates of center of the circle and 50 is the radius of circle.

9. DRAWPOLY
 Drawpoly function is used to draw polygons i.e. triangle, rectangle, pentagon, hexagon etc.
 Declaration :- void drawpoly( int num, int *polypoints );
 num indicates (n+1) number of points where n is the number of vertices in a polygon, polypoints
points to a sequence of (n*2) integers . Each pair of integers gives x and y coordinates of a point
on the polygon. We specify (n+1) points as first point coordinates should be equal to (n+1) th to
draw a complete figure.
 To understand more clearly we will draw a triangle using drawpoly, consider for example the
array :-
int points[] = { 320, 150, 420, 300, 250, 300, 320, 150}; points array contains coordinates of
triangle which are (320, 150), (420, 300) and (250, 300). Note that last point(320, 150) in array is
same as first. See the program below and then its output, it will further clear your understanding.
 Example
#include <graphics.h>
#include <conio.h>
main(){
int gd=DETECT,gm,points[]={320,150,420,300,250,300,320,150};
initgraph(&gd, &gm, "C:\\TC\\BGI");
drawpoly(4, points);
getch();
closegraph();
return 0;}

10. ELLIPSE
 Declarations of ellipse function :- void ellipse(int x, int y, int stangle, int endangle, int xradius, int
yradius);
 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.

Computer Graphics Lab manual for Software Engineering and Computer Science. Page 7
Computer Graphics Lab manual for both software Engineering and Computer Science

 Example
#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
ellipse(100, 100, 0, 360, 50, 25);
getch();
closegraph();
return 0;
}

11. FILLELLIPSE
 Declaration of fillellipse function :- void fillellipse(int x, int y, int xradius, int yradius);
 x and y are coordinates of center of the ellipse, xradius and yradius are x and y radius of ellipse
respectively.
 Example
#include <graphics.h>
#include <conio.h>
int main(){
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
fillellipse(100, 100, 50, 25);
getch();
closegraph();
return 0;}

12. FLOODFILL
 Declaration :- void floodfill(int x, int y, int border);
 floodfill function is used to fill an enclosed area. Current fill pattern and fill color is used to fill
the area.
 (x, y) is any point on the screen if (x,y) lies inside the area then inside will be filled otherwise
outside will be filled,border specifies the color of boundary of area.
 To change fill pattern and fill color use setfillstyle. Code given below draws a circle and then fills
it.
 Example
#include <graphics.h>
#include <conio.h>
main(){
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
setcolor(RED);
circle(100,100,50);
floodfill(100,100,RED); getch();
closegraph(); return 0; }

Computer Graphics Lab manual for Software Engineering and Computer Science. Page 8
Computer Graphics Lab manual for both software Engineering and Computer Science

In the above program a circle is drawn in RED color. Point (100,100) lies inside the circle as it is the
center of circle, third argument to floodfill is RED which is color of boundary of circle. So the output of
above program will be a circle filled with WHITE color as it is the default fill color.

13. SETFILLSTYLE
 setfillstyle function sets the current fill pattern and fill color.
 Declaration :- void setfillstyle( int pattern, int color);
 Different fill styles:
enum fill_styles
{
EMPTY_FILL,
SOLID_FILL,
LINE_FILL,
LTSLASH_FILL,
SLASH_FILL,
BKSLASH_FILL,
LTBKSLASH_FILL,
HATCH_FILL,
XHATCH_FILL,
INTERLEAVE_FILL,
WIDE_DOT_FILL,
CLOSE_DOT_FILL,
USER_FILL
};
 Example
#include<graphics.h>
#include<conio.h>
main(){
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
setfillstyle(XHATCH_FILL, RED);
circle(100, 100, 50);
floodfill(100, 100, WHITE);
getch();
closegraph();
return 0;}

14. SETLINESTYLE
 Declaration: void setlinestyle( int linestyle, unsigned upattern, int thickness );
 Available line styles:
enum line_styles
{
SOLID_LINE,
DOTTED_LINE,
CENTER_LINE,
DASHED_LINE,
USERBIT_LINE
};

Computer Graphics Lab manual for Software Engineering and Computer Science. Page 9
Computer Graphics Lab manual for both software Engineering and Computer Science

 Example
#include <graphics.h>
main()
{
int gd = DETECT, gm, c , x = 100, y = 50;
initgraph(&gd, &gm, "C:\\TC\\BGI");
for ( c = 0 ; c < 5 ; c++ )
{
setlinestyle(c, 0, 2);
line(x, y, x+200, y);
y = y + 25;
}
getch();
closegraph();
return 0;
}

15. SETTEXTSTYLE
 Settextstyle function is used to change the way in which text appears, using it we can modify the
size of text, change direction of text and change the font of text.
 Declaration :- void settextstyle( int font, int direction, int charsize);
 font argument specifies the font of text, Direction can be HORIZ_DIR (Left to right) or
VERT_DIR (Bottom to top).

 Different fonts
enum font_names
{
DEFAULT_FONT,
TRIPLEX_FONT,
SMALL_FONT,
SANS_SERIF_FONT,
GOTHIC_FONT,
SCRIPT_FONT,
SIMPLEX_FONT,
TRIPLEX_SCR_FONT,
COMPLEX_FONT,
EUROPEAN_FONT,
BOLD_FONT
};

 Example
#include <graphics.h>
#include <conio.h>
main()
{
int gd = DETECT, gm, x = 25, y = 25, font = 0;
initgraph(&gd,&gm,"C:\\TC\\BGI");
for (font = 0; font <= 10; font++)
{

Computer Graphics Lab manual for Software Engineering and Computer Science. Page 10
Computer Graphics Lab manual for both software Engineering and Computer Science

settextstyle(font, HORIZ_DIR, 1);


outtextxy(x, y, "Text with different fonts");
y = y + 25;
}
getch();
closegraph();
return 0;
}

16. SETVIEWPORT
 setviewport function sets the current viewport for graphics output.
 Declaration :- void setviewport(int left, int top, int right, int bottom, int clip);
 setviewport function is used to restrict drawing to a particular portion on the screen.
 For example setviewport(100 , 100, 200, 200, 1); will restrict our drawing activity inside the
rectangle(100,100, 200, 200).
 left, top, right, bottom are the coordinates of main diagonal of rectangle in which we wish to
restrict our drawing. Also note that the point (left, top) becomes the new origin.
 Example
#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm, midx, midy;
initgraph(&gd, &gm, "C:\\TC\\BGI");
midx = getmaxx()/2;
midy = getmaxy()/2;
setviewport(midx - 50, midy - 50, midx + 50, midy + 50, 1);
circle(50, 50, 55);
getch();
closegraph();
return 0;
}

17. DELAY
 delay function is used to suspend execution of a program for a particular time.
 Declaration :- void delay(unsigned int);
 Here unsigned int is the number of milliseconds ( remember 1 second = 1000 milliseconds ). To
use delay function in your program you should include the dos.h header file.
 Example
#include<dos.h>
#include<iostream.h>
main(){
cout<<"This c program will exit in 10 seconds.";
delay(10000);
return 0;
}
Above c program exits in ten seconds, after the printf function is executed the program waits for 10000
milliseconds or 10 seconds and then program termination occurs.

Computer Graphics Lab manual for Software Engineering and Computer Science. Page 11
Computer Graphics Lab manual for both software Engineering and Computer Science

18. SOUND
 Sound function produces the sound of a specified frequency.
 Used for adding music to C or C++ program, try to use some random values in loop, vary delay
and enjoy.
 Declaration:- void sound(unsigned frequency);
 Example
#include<dos.h>
main()
{
int a;
for ( a = 200 ; a <= 1000 ; a = a + 20 )
{
sound(a);
delay(25);
}
nosound();
return 0;
}

19. KBHIT
 kbhit function is used to determine if a key has been pressed or not.
 To use kbhit function in your program you should include the header file "conio.h". If a key has
been pressed then it returns a non zero value otherwise returns zero.
 Declaration : int kbhit();
 Example
#include <stdio.h>
#include <conio.h>
main()
{
while (!kbhit())
cout<<"You haven't pressed a key.";
return 0;
}
As long as in the above program user doesn't presses a key kbhit() return zero and (!0) i.e. 1 the condition
in while loop is true and "You haven't pressed a key." will be printed again and again. As a key is pressed
from the keyboard the condition in while loop become false as now kbhit() will return a non-zero value
and ( !(non-zero) = 0), so the control will come out of the while loop.

 How to use arrow keys in C++ using getch() function.


#include<graphics.h>
#include<process.h>
#include<dos.h>
#include<conio.h>
void main()
{
int i=250,j=250,x=0,y=-1,ch,gd=DETECT,gm;

Computer Graphics Lab manual for Software Engineering and Computer Science. Page 12
Computer Graphics Lab manual for both software Engineering and Computer Science

initgraph(&gd,&gm,”c:\turboc3\bgi”);
while(1)
//infinite loop

{
circle(i,j,30);
outtextxy(400,400,”Press Esc to Exit…..”);
if(kbhit())
//check if a key is pressed
{
ch=getch();
if(ch==72) //move upward
{
x=0;
y=-1;
}

if(ch==75) //move left


{
x=-1;
y=0;

if(ch==77) //move right


{
x=1;
y=0;
}

if(ch==80) //move downward


{
x=0;
y=1;
}

if(ch==27)//exit when esc pressed


exit(0);
}
i=i+x;
j=j+y;

delay(10);
cleardevice();
}
}

Computer Graphics Lab manual for Software Engineering and Computer Science. Page 13
Computer Graphics Lab manual for both software Engineering and Computer Science

20. BAR
 Declaration :- void bar(int left, int top, int right, int bottom);
 Bar function is used to draw a 2-dimensional, rectangular filled in bar .
 Coordinates of left top and right bottom corner are required to draw the bar. 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.
 Current fill pattern and fill color is used to fill the bar. To change fill pattern and fill color
use setfillstyle.
 Example
#include <graphics.h>
#include <conio.h>
main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
bar(100, 100, 200, 200);
getch();
closegraph();
return 0;
}

21. BAR3D
 Declaration :- void bar3d(int left, int top, int right, int bottom, int depth, int topflag);
 bar3d function is used to draw a 2-dimensional, rectangular filled in bar .
 Coordinates of left top and right bottom corner of bar are required to draw the bar. 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.
 depth specifies the depth of bar in pixels.
 topflag determines whether a 3 dimensional top is put on the bar or not ( if it is non-zero then it is
put otherwise not ).
 Current fill pattern and fill color is used to fill the bar. To change fill pattern and fill color use
setfillstyle.
 Example
#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
bar3d(100, 100, 200, 200, 20, 1);
getch();
closegraph();
return 0;
}

Computer Graphics Lab manual for Software Engineering and Computer Science. Page 14
Computer Graphics Lab manual for both software Engineering and Computer Science

22. FILLPOLY
 Fillpoly function draws and fills a polygon.
 It require same arguments as drawpoly.
 Declaration :- void fillpoly( int num, int *polypoints );
 fillpoly fills using current fill pattern and color which can be changed using setfillstyle.

 Example
#include <graphics.h>
#include <conio.h>
main()
{
int gd=DETECT,gm,points[]={320,150,440,340,230,340,320,150};
initgraph(&gd, &gm, "C:\\TC\\BGI");
fillpoly(4, points);
getch();
closegraph();
return 0;
}

23. GETIMAGE
 getimage function saves a bit image of specified region into memory, region can be any rectangle.
 Declaration:- void getimage(int left, int top, int right, int bottom, void *bitmap);
 getimage copies an image from screen to memory. Left, top, right, and bottom define the area of
the screen from which the rectangle is to be copied, bitmap points to the area in memory where
the bit image is stored.
 Example
#include<graphics.h>
#include<conio.h>
#include<stdlib.h>
main(){
int gd = DETECT, gm, area, temp1, temp2, left = 25, top = 75;
void *p;
initgraph(&gd,&gm,"C:\\TC\\BGI");
setcolor(YELLOW);
circle(50,100,25);
setfillstyle(SOLID_FILL,YELLOW);
floodfill(50,100,YELLOW);
setcolor(BLACK);
setfillstyle(SOLID_FILL,BLACK);
fillellipse(44,85,2,6);
fillellipse(56,85,2,6);
ellipse(50,100,205,335,20,9);
ellipse(50,100,205,335,20,10);
ellipse(50,100,205,335,20,11);
area = imagesize(left, top, left + 50, top + 50);
p = malloc(area);
setcolor(WHITE);

Computer Graphics Lab manual for Software Engineering and Computer Science. Page 15
Computer Graphics Lab manual for both software Engineering and Computer Science

settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
outtextxy(155,451,"Smiling Face Animation")
setcolor(BLUE);
rectangle(0,0,639,449);
while(!kbhit())
{
temp1 = 1 + random ( 588 );
temp2 = 1 + random ( 380 );
getimage(left, top, left + 50, top + 50, p);
putimage(left, top, p, XOR_PUT);
putimage(temp1 , temp2, p, XOR_PUT);
delay(100); left = temp1;
top = temp2;
}
getch();
closegraph();
return 0;
}

24. LINE
 Declaration: void line(int x1,int y1, int x2, int y2);
 It is used to draw a line from (x1, y1) to (x2, y2).
 Example
#include<graphics.h>
#include<conio.h>
Void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
line(100,100,400,400);
getch();
closegaph();
}

25. RECTANGLE
 Declaration: void rectangle(int left, int top, int right, int bottom);
 Left specifies the minimum x-coordinate values for the rectangle
 Top specifies the maximum y-coordinate values of the rectangle
 Right specifies the maximum x-coordinate values of the rectangle
 Bottom specifies the minimum y-coordinates of the rectangle corner.

Computer Graphics Lab manual for Software Engineering and Computer Science. Page 16
Computer Graphics Lab manual for both software Engineering and Computer Science

 Example
#include<graphics.h>
#include<conio.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
rectangle(100,100,400,400);
getch();
closegaph();
}

To fill and change filling color of the rectangle use setfillstyle and floodfill function.

#include<graphics.h>
#include<conio.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
setcolor(4);
setfillstyle(8,7);
rectangle(100,100,400,400);
floodfill(200,200,4);
getch();
closegraph();
}
1. DDA (Digital Differential Analyzer) algorithm
 DDA is a scan-conversion line algorithm based on either ∆y or ∆x, using equation (4) or (5).
 DDA line drawing algorithm can be written as:-
1. Input the two line end points (xa, ya) and (xb, yb )
2. Calculate
dx = xb - xa
dy = yb - ya
3. If abs(dx)>=abs(dy) then Length= abs(dx) else Length= abs(dy)
∆x=dx/length
∆y=dy/length
x=xa
y=ya
putpixel(ceil(x), ceil(y))
i=1
4. while (i<=length)
x=x+∆x
y=y+∆y
putpixel(ceil(x), ceil(y))

Computer Graphics Lab manual for Software Engineering and Computer Science. Page 17
Computer Graphics Lab manual for both software Engineering and Computer Science

i=i+1
end while
5. Finish
 C++ program of DDA line drawing algorithm
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
class lineDDA {
private:
int dx,dy,x,y,xi,yi,xa,ya,xb,yb,step,k;
public:
void getdata();
void line(); };
void lineDDA::getdata() {
cout<<"\n\tEnter xa and ya :";
cin>>xa>>ya;
cout<<"\n\tEnter xb and yb :";
cin>>xb>>yb; }
void lineDDA::line() {
dx=abs(xb-xa);
dy=abs(yb-ya);
if(dx>dy)
step=dx;
else
step=dy;
xi=(xb-xa)/step;
yi=(yb-ya)/step;
x=xa;
y=ya;
putpixel(ceil(x),ceil(y),1);
for(k=1;k<=step;k++) {
x+=xi;
y+=yi;
putpixel(ceil(x),ceil(y),1);
k=k+1;
}}
void main() {
lineDDA l;
clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:/TC/BGI");
l.getdata();
l.line();

Computer Graphics Lab manual for Software Engineering and Computer Science. Page 18
Computer Graphics Lab manual for both software Engineering and Computer Science

getch();
closegraph();
}
2. Bresenham's Line Algorithm
 An accurate and efficient raster line generating algorithm, developed by Bresenham, scan
converts lines using only incremental integer calculations that can be adapted to display circles
and other curves.
 Bresenham's line drawing algorithm for |m|<1
1. Input the two endpoints (xa, ya) and (xb, yb) and store the left endpoint in (x0,y0)
2. Load (x0,y0) into the frame buffer: that is , plot the first point
3. Calculate constants ∆x= xb- xa, ∆y= yb- ya, 2∆y, and 2∆y-2∆x and obtain the starting point for
the decision parameter as
P0 = 2∆y-∆x
4. At each Xk along the line , starting at K=0; perform the following test:
If Pk<0 then
Xk+1 = Xk+1 and Yk+1=yk
Pk+1=pk+2∆y
Otherwise, if Pk>=0 then
Xk+1 = Xk+1 and Yk+1=yk+1
Pk+1=pk+2∆y-2∆x
5. Repeat step 4, ∆x times
 C++ program for Bresenham's line drawing algorithm
#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<math.h>
void main() {
clrscr();
int gd=DETECT, gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
int xa,ya,xb,yb,x,y,dx,dy,p,k;
cout<<"Enter the first end points"<<endl;
cin>>xa>>ya;
cout<<"Enter the second endpoints"<<endl;
cin>>xb>>yb;
dx=xb-xa;
dy=yb-ya;
p=(2*dy)-dx;
x=xa;
y=ya;
putpixel(x,y,1);
for(k=1;k<=dx;k++)
{

Computer Graphics Lab manual for Software Engineering and Computer Science. Page 19
Computer Graphics Lab manual for both software Engineering and Computer Science

x+=1;
if(p<0){
p=p+2*dy;
}
else{
y=y+1;
p=p+(2*dy)-(2*dx);
}
putpixel(x,y,1);
}
getch();
closegraph();
}

Bresenham's Circle Algorithm


1. Input radius r and circle center (xc, yc), and obtain the first coordinate on the circumference of a circle
centered on the origin as:
(x0, y0) = (0, r)
2. Calculate the initial value of decision parameter as:
p0= 3-2*r
3. At each xk along along the line, starting at k=0 perform the following test:

If Pk<0 then

Xk+1 = Xk+1 and Yk+1= Yk


Pk+1= Pk+4Xk+1 +6
Otherwise, if Pk>=0 then

Xk+1 = Xk+1 and Yk+1= Yk-1


Pk+1= Pk+ 4(Xk+1- Yk+1 ) +10
4. Determine symmetry points at the other seven octants.

5. Move each calculated pixel position (x, y) onto the circular path centered on (xc. yc) and plot the
coordinate value as:

X= X+Xc and Y= Y+Yc

6. Repeat step 3 to 5 until X>=Y.

 C++ program for Bresenham's circle drawing algorithm


#include<stdlib.h>
#include<graphics.h>

Computer Graphics Lab manual for Software Engineering and Computer Science. Page 20
Computer Graphics Lab manual for both software Engineering and Computer Science

#include<dos.h>
void circ_bre(int x,int y,int rad);
void display(int,int,int,int);
void main()
{
int gd = DETECT, gm, x,y,r;
initgraph(&gd,&gm,"c:\\tc\\bgi");
cleardevice();
cout<<"Bresenhams circle generation algorithm ";
cout<<"\nEnter the center co-ordinates for circle ";
cin>>x>>y;
cout<<"\nEnter the radius of the circle";
cin>>r;
circ_bre(x,y,r);
getch();
closegraph();
}
void circ_bre(int x,int y,int rad)
{
float dp; //initialising the descision parameter.
int x1,y1;
x1 = 0; //initialisng the X,Y cordinates.
y1 = rad;
dp = 3 - 2*rad;
while(x1<=y1)
{
if(dp<=0)
dp += (4 * x1) + 6;
else
{
dp += 4*(x1-y1)+10;
y1--;
}
x1++;
display(x1,y1,x,y);
}
}
void display (int x1,int y1,int x,int y)
{
putpixel(x1+x,y1+y,WHITE); //plotting the pixels.
putpixel(x1+x,y-y1,WHITE);
putpixel(x-x1,y1+y,WHITE);
putpixel(x-x1,y-y1,WHITE);
putpixel(x+y1,y+x1,WHITE);

Computer Graphics Lab manual for Software Engineering and Computer Science. Page 21
Computer Graphics Lab manual for both software Engineering and Computer Science

putpixel(x+y1,y-x1,WHITE);
putpixel(x-y1,y+x1,WHITE);
putpixel(x-y1,y-x1,WHITE);
}

 C++ CODE FOR LINE Clipping


#include<stdio.h>
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void storepoints(int,int,int,int,int,int,int[]);
void main()
{
int gdriver=DETECT,gmode;
int x1,x2,y1,y2,xmax,ymax,xmin,ymin,a[10],b[10],xi1,xi2,yi1,yi2,flag=0;
float m;
int i;
clrscr();
cout<<"Enter the line end points"<<endl;
cout<<"x1=";
cin>>x1;
cout<<"x2=";
cin>>x2;
cout<<"y1=";
cin>>y1;
cout<<"y2=";
cin>>y2;
cout<<"Enter the coordinates of the clip window"<<endl;
cout<<"xmin=";
cin>>xmin;
cout<<"ymin=";
cin>>ymin;
cout<<"xmax=";
cin>>xmax;
cout<<"ymax=";
cin>>ymax;
storepoints(x2,y2,ymax,xmax,xmin,ymin,b);
storepoints(x1,y1,ymax,xmax,xmin,ymin,a);
for(i=1;i<=4;i++)
{
if(a[i]*b[i]==0)
flag=1;

Computer Graphics Lab manual for Software Engineering and Computer Science. Page 22
Computer Graphics Lab manual for both software Engineering and Computer Science

else
flag=0;
}

if(flag==1)
{
m=(y2-y1)/(x2-x1);
xi1=x1;
yi1=y1;
yi2=y2;
xi2=x2;
}
if(a[1]==1)
{
yi1=ymax;
xi1=x1+((1/m)*(yi1-y1));
}
else
{
if(a[2]==1)
{
yi1=ymin;
xi1=x1+((1/m)*(yi1-y1));
}
}
if(a[3]==1)
{
xi1=xmax;
yi1=y1+(m*(xi1-x1));
}
if(a[4]==1)
{
xi1=xmin;
yi1=y1+(m*(xi1-x1));
}
if(b[1]==1)
{
yi2=ymax;

xi2=x2+((1/m)*(yi2-y2));
}
if(b[2]==1)
{
yi2=ymin;

Computer Graphics Lab manual for Software Engineering and Computer Science. Page 23
Computer Graphics Lab manual for both software Engineering and Computer Science

xi2=x2+((1/m)*(yi2-y2));
}
if(b[3]==1)
{
xi2=xmax;
yi2=y2+(m*(xi2-x2));
}
if(b[4]==1)
{
xi2=xmin;
yi2=y2+(m*(xi2-x2));
}
clrscr();
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
delay(10000);
closegraph();
clrscr();
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
line(xi1,yi1,xi2,yi2);
rectangle(xmin,ymin,xmax,ymax);
if(flag==0)
{
cout<<"no clipping is required"<<endl;
}
getch();
closegraph();
}
void storepoints(int x1,int y1, int ymax,int xmax,int xmin,int ymin,int c[10])
{
if((y1-ymax)>0)
c[1]=1;
else
c[1]=0;
if((ymin-y1)>0)
c[2]=1;
else
c[2]=0;
if((x1-xmax)>0)
c[3]=1;
else
c[3]=0;
if((xmin-x1)>0)

Computer Graphics Lab manual for Software Engineering and Computer Science. Page 24
Computer Graphics Lab manual for both software Engineering and Computer Science

c[4]=1;
else
c[4]=0;
}

 Code for drawing cube line by line in sequence.


#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
clrscr();
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
for(int i=16;i>=14; i--)
{
delay(900);
setcolor(i);
line(300,400,400,400);
}
for(int i1=16;i1>=14; i1--)
{
delay(900);
setcolor(i1);
line(400,400,450,350);
}
for(int i2=16;i2>=14; i2--)
{
delay(900);
setcolor(i2);
line(450,350,350,350);
}
for(int i3=16;i3>=14; i3--)
{
delay(900);
setcolor(i3);
line(350,350,300,400);
}

for(int i4=16;i4>=14; i4--)


{

Computer Graphics Lab manual for Software Engineering and Computer Science. Page 25
Computer Graphics Lab manual for both software Engineering and Computer Science

delay(900);
setcolor(i4);
line(300,400,300,300);
}
for(int i5=16;i5>=14; i5--)
{
delay(900);
setcolor(i5);
line(350,350,350,250);
}
for(int i6=16;i6>=14; i6--)
{
delay(900);
setcolor(i6);
line(300,300,350,250);
}
for(int i7=16;i7>=14; i7--)
{
delay(900);
setcolor(i7);
line(350,250,450,250);
}
for(int i8=16;i8>=14; i8--)
{
delay(900);
setcolor(i8);
line(450,250,450,350);
}
for(int i9=16;i9>=14; i9--)
{
delay(900);
setcolor(i9);
line(450,250,400,300);
}
for(int i10=16;i10>=14; i10--)
{
delay(900);
setcolor(i10);
line(400,300,400,400);
}
for(int i11=16;i11>=14; i11--)
{
delay(900);
setcolor(i11);

Computer Graphics Lab manual for Software Engineering and Computer Science. Page 26
Computer Graphics Lab manual for both software Engineering and Computer Science

line(300,300,400,300);
}
getch();
closegraph();
}

There are too many graphics.h, conio.h, and


dos.h function that is useful for graphics.
For more knowledge search and read for
yourself!

Computer Graphics Lab manual for Software Engineering and Computer Science. Page 27

You might also like