Introduction to Graphics
Programming in
LAB-1 C
Date:31/1/2024
Contents
❏Setting Up graphics using MinGW
❏Basic Idea About Graphics Programming in C
❏Some functions in Graphics
❏Working Example
❏ Computer Graphics is one of the most powerful and interesting aspect of
computers.
❏ There are many things we can do in graphics apart from drawing figures of
various shapes.
❏ All video games, animation, multimedia works using computer graphics.
❑ The Default output mode of C language programs is “Text” mode.
❑ We have to switch to “Graphic” mode before drawing any graphical shape like line,
rectangle, circle etc.
x,y
(0,0) ( 640 , 0 )
X+
Y+
640 x 480
( 0 , 480 ) ( 640 , 480 )
Graphics in C
❑ First of all, we must include the ”graphics.h” header file in our source program
❑ MinGW compiler doesn’t provides inbuilt facility to run graphics.h library.
❑ So you are not able to run graphics in C language
❑ To run "graphics.h" library in Windows, following are some simple steps.
Installing library
❑Copy "graphics.h" and winbgim.h files in "include" folder of your compiler directory
❑Copy "libbgi.a" to "lib" folder of your compiler directory
❑In codeblocks open "Settings >> Compiler >>linker settings" click "Add" button in link
libraries part and browse and select "libbgi.a" file
❑In right part (i.e. other linker options) paste commands "-lbgi -lgdi32 -lcomdlg32 -luuid -
loleaut32 -lole32"
❑Click OK
Here is a sample program to test it's working or not. sample.cpp
#include<graphics.h>
main() {
int gd=DETECT,gm;
initgraph(&gd,&gm,NULL);
getch();
closegraph();
}
If you get following screen then graphics.h is working
Initializing Graphics Mode
❏ The initgraph function is used to switch the output from text mode to
graphics mode
❏ The initgraph function takes three arguments
❏ Syntax: –
initgraph(&graphics_driver,&graphics_mode,Path _to_driver);
❏ Example:
If you are working on Turbo C, use the path “c:\\tc\\bgi”
initgraph(&dr, &md, NULL );
Graphics Driver Directory Path of Graphics
Type Driver
Initial Graphics
Mode
Closing Graphics Mode
❏ Graphics mode must be closed at the end
❏ For that function closegraph() is used
❏ Syntax: –
closegraph();
Some Functions in
“graphics.h”
line()
❏ line function is used to draw a line from a point(x1,y1) to
point(x2,y2)
❏ Syntax :-
void line(int x1, int y1, int x2, int y2);
❏ Remarks
➔ line draws a line from (x1, y1) to (x2, y2) using the current
color
circle()
❏ Circle function is used to draw a circle with center (x,y) and third
parameter specifies the radius of the circle.
❏ Syntax :-
void circle(int x, int y, int radius);
● (x , y) -> Center point of circle
● radius-> Radius of circle
rectangle()
❏ Coordinates of left top and right bottom corner are required to
draw the rectangle.
❏ Syntax:-
void rectangle(int left, int top, int right, int bottom);
putpixel() and getpixel()
❏ getpixel() returns the color of pixel present at point(x, y)
❏ Syntax: –
int getpixel(int x, int y);
❏ putpixel plots a pixel at a point(x, y) of specified color
❏ Syntax:-
void putpixel(int x, int y, int color);
Note the Point:
In this, color can be represented using
the name of colors in CAPITAL LETTER
and also using the numbers
Eg:
code 0 means BLACK
code 1 means BLUE
code 2 means GREEN
Working