0% found this document useful (0 votes)
56 views6 pages

Unit - 3.b PWC Graphics

The document introduces C graphics programming using functions from the graphics.h header file. It provides sample code to initialize graphics mode using initgraph and examples drawing basic shapes like circles, rectangles, ellipses, and concentric circles. Key functions introduced are initgraph(), getch(), closegraph(), circle(), rectangle(), bar(), ellipse(), and setcolor(). More graphics functions and explanations can be found online at the provided URL.

Uploaded by

Guru Rockzz
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)
56 views6 pages

Unit - 3.b PWC Graphics

The document introduces C graphics programming using functions from the graphics.h header file. It provides sample code to initialize graphics mode using initgraph and examples drawing basic shapes like circles, rectangles, ellipses, and concentric circles. Key functions introduced are initgraph(), getch(), closegraph(), circle(), rectangle(), bar(), ellipse(), and setcolor(). More graphics functions and explanations can be found online at the provided URL.

Uploaded by

Guru Rockzz
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/ 6

16GET26 – C Programming – I CSE A – 2018 Batch - C Graphics

Introduction:

 To initialize graphics mode we use initgraph function in our program.


 initgraph function is present in "graphics.h" header file, so every graphics
program should include "graphics.h" header file.

Sample graphics code


#include<graphics.h>
#include<conio.h>

int main()
{
int gd = DETECT, gm;

initgraph(&gd, &gm, "C:\\TC\\BGI");

getch();
closegraph();
return 0;
}

To begin with we have declared two variables of int type gd and gm for graphics driver
and graphics mode respectively, you can choose any other variable name as well.
DETECT is a macro defined in "graphics.h" header file, then we have passed three
arguments to initgraph function first is the address of gd, second is the address of gm
and third is the path where your BGI files are present (you have to adjust this
accordingly where you Turbo C compiler is installed).
Initgraph function automatically decides an appropriate graphics driver and mode such
that maximum screen resolution is set, getch helps us to wait until a key is pressed,
closegraph function closes the graphics mode, and finally return statement returns a
value 0 to main indicating successful execution of the program. After you have
understood initgraph function then you can use functions to draw shapes such as circle,
line , rectangle, etc.

C graphics functions

1. arc- void arc(int x, int y, int stangle, int endangle, int radius);

2. bar - void bar(int left, int top, int right, int bottom);

3. bar3d - void bar3d(int left, int top, int right, int bottom, int depth, int topflag);

4. circle- void circle(int x, int y, int radius);

5. cleardevice - void cleardevice();


16GET26 – C Programming – I CSE A – 2018 Batch - C Graphics

6. closegraph - void closegraph();

7. drawpoly - void drawpoly( int num, int *polypoints );

8. ellipse - void ellipse(int x, int y, int stangle, int endangle, int xradius, int yradius);

9. fillellipse - void fillellipse(int x, int y, int xradius, int yradius);

10. floodfill- void floodfill(int x, int y, int border);

11. getbkcolor - int getbkcolor();

12. getcolor - int getcolor();

13. getmaxx - int getmaxx();

14. getmaxy - int getmaxy();

15. getx - int getx();

16. gety - int gety();

17. line - void line(int x1, int y1, int x2, int y2);

18. outtext - void outtext(char *string);

19. outtextxy - void outtextxy(int x, int y, char *string);

20. putpixel - void putpixel(int x, int y, int color);

21. rectangle - void rectangle(int left, int top, int right, int bottom);

22. setbkcolor - void setbkcolor(int color);

23. setcolor - void setcolor(int color);

24. setlinestyle - void setlinestyle( int linestyle, unsigned upattern, int thickness );

25. textheight - int textheight(char *string);

More functions and explanation available at :

https://www.programmingsimplified.com/c/graphics.h#functions
16GET26 – C Programming – I CSE A – 2018 Batch - C Graphics

Example programs:

1. Draw circle using graphics

void main()

int gd = DETECT,gm;

int x ,y ,radius=80;

initgraph(&gd, &gm, "C:\\TC\\BGI");

/* Initialize center of circle with center of screen */

x = getmaxx()/2;

y = getmaxy()/2;

outtextxy(x-100, 50, "CIRCLE Using Graphics in C");

/* Draw circle on screen */

circle(x, y, radius);

getch();

closegraph();

Output:
16GET26 – C Programming – I CSE A – 2018 Batch - C Graphics

2. Draw rectangle and bar using graphics

void main()

int gd = DETECT,gm;

initgraph(&gd, &gm, "C:\\TC\\BGI");

/* Draw rectangle on screen */

rectangle(150, 50, 400, 150);

/* Draw Bar on screen */

bar(150, 200, 400, 350);

getch();

closegraph();

Output:
16GET26 – C Programming – I CSE A – 2018 Batch - C Graphics

3. Draw an ellipse using graphics

void main(){

int gd = DETECT,gm;

int x ,y;

initgraph(&gd, &gm, "X:\\TC\\BGI");

/* Initialize center of ellipse with center of screen */

x = getmaxx()/2;

y = getmaxy()/2;

outtextxy(x-100, 50, "ELLIPSE Using Graphics in C");

/* Draw ellipse on screen */

ellipse(x, y, 0, 360, 120, 60);

getch();

closegraph();

Output:
16GET26 – C Programming – I CSE A – 2018 Batch - C Graphics

4. Draw concentric circles using graphics

void main() {

int gd = DETECT,gm, x ,y;

initgraph(&gd, &gm, "C:\\TC\\BGI");

/* Initialize center of circle with center of screen */

x = getmaxx()/2;

y = getmaxy()/2;

outtextxy(240, 50, "Concentric Circles");

/* Draw circles on screen */

setcolor(RED);

circle(x, y, 30);

setcolor(GREEN);

circle(x, y, 50);

setcolor(YELLOW);

circle(x, y, 70);

setcolor(BLUE);

circle(x, y, 90);

getch();

closegraph();

Output:

You might also like