Graphics in C Prof Abdul Razzaque Anjuman College

Graphics in C Prof. Abdul Razzaque Anjuman College of Engineering & Technology Nagpur ab. razzaque@rediffmail. com

Third Sem B. E (Computer Sci & Engg. ) Advanced C Programming & Logic Design RTM Nagpur University Nagpur 2 Prof. Abdul Razzaque, Anjuman. C. E. T, Nagpur

3 Application �For developing our own games �In making projects, for animation �Image Processing Easy �No complex logic �Std Library Function under Prof. Abdul Razzaque, Anjuman. C. E. T, Nagpur

Functions of graphics. h � arc � getmaxx � moverel � bar 3 d � getmaxy � moveto � circle � getpixel � outtext � closegraph � getx � outtextxy � drawpoly � gety � putimage � ellipse � graphdefault � putpixel s � imagesize � fillpoly � initgraph � floodfill � line � getcolor � linerel � getimage � getmaxcolor � lineto � fillellipse 4 Prof. Abdul Razzaque, Anjuman. C. E. T, Nagpur � rectangle � sector � setcolor � setfillstyle � setlinestyle � settextstyle

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; } 5 Prof. Abdul Razzaque, Anjuman. C. E. T, Nagpur

�DETECT macro defined in "graphics. h" header file �gd graphics driver gm graphics mode �Initgraph function automatically decides an � appropriate graphics driver and mode such that maximum screen resolution is set � closegraph function closes the graphics mode 6 Prof. Abdul Razzaque, Anjuman. C. E. T, Nagpur

initgraph() Declaration void initgraph(int *graphdriver, int *graphmode, char *pathtodriver); For writing any graphics program in C we have to call the initgraph function that will initialize the graphics mode on the computer. Initgraph initializes the graphics system by loading the graphics driver from disk then putting the system into graphics mode. Initgraph also resets all graphics settings (color, palette, current position, viewport, etc. ) to their defaults. 7 Example Prof. Abdul Razzaque, Anjuman. C. E. T, Nagpur int gd = DETECT, gm;

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. 8 Prof. Abdul Razzaque, Anjuman. C. E. T, Nagpur

line() Declaration void line(int x 1, int y 1, int x 2, int y 2); Example #include <graphics. h> #include <conio. h> main() { int gd = DETECT, gm; initgraph(&gd, &gm, "C: \TC\BGI"); line(100, 200, 200); getch(); closegraph(); return 0; } 9 Prof. Abdul Razzaque, Anjuman. C. E. T, Nagpur

lineto() Declaration void lineto (int x, int y); Example #include <graphics. h> #include <conio. h> main() { int gd = DETECT, gm; initgraph(&gd, &gm, "C: \TC\BGI"); moveto(100, 100); lineto(200, 200); getch(); closegraph(); return 0; } 10 Prof. Abdul Razzaque, Anjuman. C. E. T, Nagpur

linerel() Declaration void linerel (int x, int y); Example #include <graphics. h> #include <conio. h> main() { int gd = DETECT, gm; initgraph(&gd, &gm, "C: \TC\BGI"); moveto(250, 250); linerel(100, 100); getch(); closegraph(); return 0; } 11 Prof. Abdul Razzaque, Anjuman. C. E. T, Nagpur

moveto() Declaration void lineto (int x, int y); Example #include <graphics. h> #include <conio. h> main() { int gd = DETECT, gm; initgraph(&gd, &gm, "C: \TC\BGI"); moveto(100, 100); lineto(200, 200); getch(); closegraph(); return 0; } 12 Prof. Abdul Razzaque, Anjuman. C. E. T, Nagpur

moverel() Declaration void linerel (int x, int y); Example #include <graphics. h> #include <conio. h> main() { int gd = DETECT, gm; initgraph(&gd, &gm, "C: \TC\BGI"); moveto(250, 250); linerel(100, 100); getch(); closegraph(); return 0; } 13 Prof. Abdul Razzaque, Anjuman. C. E. T, Nagpur

arc() Declaration void arc(int x, int y, int stangle, int endangle, int radius); Example #include <graphics. h> #include <conio. h> main() { int gd = DETECT, gm; initgraph(&gd, &gm, "C: \TC\BGI"); arc(100, 0, 135, 50); getch(); closegraph(); return 0; } 14 Prof. Abdul Razzaque, Anjuman. C. E. T, Nagpur

circle() Declaration void circle(int x, int y, int r); Where center=(x, y) ; radius=r Example #include<graphics. h> #include<conio. h> main() { int gd = DETECT, gm; initgraph(&gd, &gm, "C: \TC\BGI"); circle(100, 50); getch(); closegraph(); return 0; } 15 Prof. Abdul Razzaque, Anjuman. C. E. T, Nagpur

sector() Sector function draws and fills an elliptical pie slice. Declaration void sector( int x, int y, int stangle, int endangle, int xradius, int yradius); Example #include <graphics. h> #include <conio. h> main() { int gd = DETECT, gm; initgraph(&gd, &gm, "C: \TC\BGI"); sector(100, 0, 135, 25, 35); getch(); closegraph(); return 0; } 16 Prof. Abdul Razzaque, Anjuman. C. E. T, Nagpur

ellipse() Declaration void ellipse(int x, int y, int stangle, int endangle, int xradius, int yradius) Example #include<graphics. h> #include<conio. h> main() { int gd = DETECT, gm; initgraph(&gd, &gm, "C: \TC\BGI"); ellipse(100, 0, 360, 50, 25); getch(); closegraph(); return 0; } 17 Prof. Abdul Razzaque, Anjuman. C. E. T, Nagpur
- Slides: 17