APC Unit-5 Notes - 240605 - 000530
APC Unit-5 Notes - 240605 - 000530
To start with graphics programming, Turbo C is a good choice. Even though DOS has its own
limitations, it is having a large number of useful functions and is easy to program. To implement
graphics algorithms, to give graphical display of statistics, To view signals from any source, we
can use C graphics. Here is a article to start programming with Turbo C. 'Run and Learn' is our
method. We have used source codes throughout the explanations. Just execute them to understand
what is happening.
1. Firstly let me tell you what the output of this program is.
2. This program initializes graphics mode and then closes it after a key is pressed.
3. To begin with we have declared two variables of int type gd and gm for graphics
driver and graphics mode respectively.
5. Then we have passed three arguments to initgraph function 1st is the address of gd, 2nd is
the address of gm and 3rd is the path where your BGI files are present ( you have to adjust
this accordingly where you turbo compiler is installed).
6. 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
your program
7. After you have understood initgraph function then you can use functions to draw shapes
such as circle, line , rectangle etc, then you can learn how to change colors and fonts using
suitable functions, then you can go for functions such as getimage, putimage etc for doing
animation.
2. Program---Printing Textin Graphics Using
Outtextxy Function
#include<graphics.h>
#include<stdio.h> int main(void)
{
Explanation
outtextxy(x,y,"Hello World");
Printf Prints Text on Screen in “Text Mode” while outtextxy() function Prints Text onto Screen in
“Graphics Mode“.
This Function Accepts 3 Parameters. Syntax:
outtextxy(x,y,"Hello World");
#include<graphics.h>
#include<conio.h> void main()
{
#include<stdio.h> #include<conio.h>
#include<graphics.h> void main()
{
getch();
closegraph();
/*Here a sample program to illustrate how to use BARS which are used for visual
statistics */
#include<graphics.h> main() {
int gd=DETECT,gm,maxx,maxy,x,y,button; initgraph(&gd,&gm,"");
line(80,150,200,150);
line(80,150,80,50);
settextstyle(1,HORIZ_DIR,1);
outtextxy(100,153,"<-X axis");
settextstyle(1,VERT_DIR,1); outtextxy(60,50,"<-Y
axis"); bar(100,100,120,150); bar(130,120,150,150);
getch(); closegraph();
}
#include<stdio.h> #include<conio.h>
#include<graphics.h> void main() {
int gd = DETECT, gm;
closegraph();
Important Functions
3. setcolor(color);
8. setfillstyle(pattern, color)
1. Text Formatting
5. settextjustify(horizontal, vertical)
7. Font: (0-11)
BLACK: 0
BLUE: 1
GREEN: 2
CYAN: 3
RED: 4
MAGENTA: 5
BROWN: 6
LIGHTGRAY: 7
DARKGRAY: 8
LIGHTBLUE: 9
LIGHTGREEN: 10
LIGHTCYAN: 11
LIGHTRED: 12
LIGHTMAGENTA: 13
YELLOW: 14
WHITE: 15
Important Functions (Messages)
1. Text Output
3. setcolor(index);
7. outtextxy(x,y,”message”);
4. answer = ismouseclick(kind)
6. (if you don’t do this you can’t get the next mouse click!)
7. clearmouseclick(kind);
9. x = mousex(); y = mousey();
8. Program --- Moving On( concentric circles )
circle(100,100,50);
rectangle(50+i,275,150+i,400);
rectangle(150+i,350,200+i,400); circle(75+i,410,10);
circle(175+i,410,10); setcolor(j);
delay(100); if( i == 420 )
break;
if ( j == 15 ) j = 2;
cleardevice(); // clear screen
int gd,gm;
gd=DETECT;
while(!kbhit())
putpixel(random(439)+101, random(279)+101,random(16));
setcolor(random(16)); circle(320,240,random(100));
}
getch(); closegraph();
}
Program for Circle Animation
#include<graphics.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
#include<dos.h>
void main()
{
int gd = DETECT, gm;
int x, y, maxx, maxy;
int xdir = 1, ydir = 1;
int color = 1;
initgraph(&gd, &gm, "c:\\tc\\bgi");
maxx = getmaxx();
maxy = getmaxy();
x = 25;
y = 45;
randomize();
while(!kbhit())
{
cleardevice();
rectangle(0, 20, maxx, maxy-20);
outtextxy(maxx/2-80, 0, "Moving Ball");
outtextxy(maxx/2-80, maxy-15, "Press any key to exit...");
setcolor(WHITE);
setfillstyle(SOLID_FILL, color);
circle(x, y, 25);
floodfill(x, y, WHITE);
delay(40);
x = x + (xdir * 5);
y = y + (ydir * 5);
if(x >= maxx - 25 || x <= 25)
{
xdir *= -1;
color = random(15) + 1;
}
if(y >= maxy - 45 || y <= 45)
{
ydir *= -1;
color = random(15) + 1;
}
}
closegraph();
restorecrtmode();
}
Program for Traffic Signal
// Driver Code
void main()
{
clrscr();
int gd = DETECT, gm, midx, midy;
midx = getmaxx() / 2;
midy = getmaxy() / 2;
// rectangle lines
rectangle(midx - 30, midy - 80, midx + 30, midy + 80);
// Delay of 2 sec
delay(2000);
graphdefaults();
cleardevice();
setcolor(WHITE);
setcolor(WHITE);
getch();
}
https://www.programmingsimplified.com/c/graphics.h